This LUA script can be used to add the following functions to Domoticz:
- power outage detect and emergency lights => turns on Leds (connected to devices supplied by 12V + backup battery) in case of blackout (power failure)
- shows import and export electricity power, through red/green leds on Creasol DomBusTH (this small module has the ability to show 1 or more flashes on the red and green LEDs available in the module itself); red LED flashes 1 time if import power <1kW, 2 times if <2kW, ... green led flashes 1 time if export power <1kW, 2 times if power < 2kW, 3 times if power <3kW, ....)
- in case of high power usage, the script sets heat pump to work at lower level to reduce power consumption, or disconnect loads to prevent power cut. In case there are not any load that can be automatically disconnected, send alert through leds/buzzer/siren, and send alert by Telegram
- automatically enable loads in case of extra power from renewable sources, to increase the own-consuming: for example activate an electric heater if temperature is below a TempSet and available power (from photovoltaic?) is greater than the heater power
Although it seems a bit complicted, this diagram can be divided in the following parts:
- connection to the energy meter (with pulsed output, 1 pulse every Wh, 1000 pulses per kWh)
- power outage detection (in this schema, it's connected to the heat pump power line, because if the power to the heat pump is missing during the winter, it can be a real problem!)
- LEDs to be activated in case of power cut (emergency lights)
- red and green LEDs that show import or export power (1 flash=0-1kW, 2 flashes=1-2kW, 3 flashes=2-3kW, ...): this feature is available on Creasol DomBusTH module
- temperature and humdity monitoring
- relay outputs to activate electric appliances in case of extra-power production (from photovoltaic or other renewable energy source)
- heat pump controlling (to modulate its power consumption)
- buzzer in case that power usage is above certain thresholds, so the electricity meter can lead to a power disconnection
Connecting the energy meter
In the block diagram above the Eastron SDM230 power meter is used, but any power meter with pulse outputs for import/export energy can be used for this purpose.
SDM230 has 2 optoisolated pulse outputs, one that emits 1 pulse every 1Wh imported energy, and another one that emits 1 pulse every 1Wh exported energy.
The result that we want to achieve is to have two devices, in Domoticz, showing the imported and exported power.
Using a DomBus module to interface pulse outputs energy meter
In case of using a DomBus RS485 module to interface the energy meter, this is following procedure to configure two DomBus input ports as counters:
- connect the pulse outputs to any available I/O, for example IO8 and IO9 for DomBus12, IO1 and IO2 for DomBusTH or DomBus23. The SDM230 common terminal block should be connected to GND (0V).
- configure Pulse1 output, on SDM230, as EXP kWh (check the manual to know how to do that); Pulse2 is pre-configured as IMP kWh (measure the energy imported from grid)
- configure the Domoticz device connected to Pulse2: press Edit on that device, and replace in the Description field IN_DIGITAL or IN_TWINBUTTON (the default value) with IN_COUNTER,TYPENAME=kWh and save. DomBus12Please note that kWh is CASE SENSITIVE! Now the device will be automatically moved to the Utility panel and configured as energy meter. Edit it again, write a name (for example PowerMeter_Import), and select Usage type.
- if produced energy sDomBus12hould be measured, configure the Domoticz device connected to Pulse1 as before: press Edit on that device, and replace in the Description field IN_DIGITAL or IN_TWINBUTTON with IN_COUNTER,TYPENAME=kWh and save. Now the device will be automatically moved to the Utility panel and configured as energy meter. Edit it again, write a name (for example PowerMeter_Export), and select Return type.
- In case of both import and export devices are created, it's needed to specify on Import device the Unit number of the Export device, and vice versa. So, select Setup -> Devices and write
Domoticz needs up to 5 minutes before updating the energy counter correctly.
Power outage detection and emergency lights
Use any device you prefer to detect if electricity from grid is available or not. In the diagram above, the voltage from a circuit breaker (heat pump circuit breaker, in the example) is sensed by a optoisolated input.
If a wired-bus is used to connect domotic modules, and this is supplied by a backup-battery power supply (13.6V power supply + lead acid battery, or a UPS), it's possible to connect LEDs to the domotic modules and turn on LEDs in case of power outage (as emergency lights). The same can be done on battery-powered devices that have enough energy capacity.
Show current imported / exported power on red / green leds
Maybe the following function is available only on DomBusTH module, that has a special feature so LEDs can be configured in Domoticz as Selector Switch where 0 means OFF, 10=1 pulse, 20=2 pulses, 30=3 pulses, 40=4 pulses, and so on. Check the video that shows DomBusTH module programmed to show the ventilation status on white led and current exported power on green led (and imported power on red led, of course)!
In this case it's possible to get red LED flashes a number of times corresponding to the current imported power, and similarly green LED flashing a number of times corresponding to the exported power. This can be useful in the kitchen to check the available power from renewable sources, or verify that we're not consuming more power than what is permitted by the electricity meter, without the need to mount large display or tablet to show the current power.
DomBusTH also integrates a white light (usable as emergency light), temperature and relative humidity sensors, 4 I/O (that can be connected to pushbutton switches, buzzer, ...) and 2 open-drain outputs (that can be connected to external relays).
Activating loads and appliances automatically
Using any relay module it's possible to activate some appliances when there is excess energy from renewable sources: in this case it's possible to improve the own-consumption and reduce the needed of energy from the grid.
The script is able automatically enable and disable heaters, heated towel rails, dehumidifiers, ....
Limiting electricity power from grid to avoid power cut
The script has 2 thresholds above which the electric power must be reduced within a certain time. In Italy, the electricity grid permits to get
- nominal power + 10% forever
- nominal power + 27% for max 90 minutes
- more than nominal power till 14 kW for max 2 minutes
Power thresholds and max durations can be defined in the configuration file for the script, so when the imported power is above those thresholds the script try to limit the imported power by regulating, for example, the heat pump and/or swithing off some loads.
If there is nothing else to switch off and imported power level is above one of these thresholds, script is able to activate buzzers, leds, sirens or other systems to notify the building people that they have to reduce power consumption. Also, it can send a notification by Telegram.
The following list of commands can be just copied and pasted in linux shell to download and install the scripts. For those who use Windows operating system, installation is quite simple: just download and copy 4 files in the domoticz/scripts/lua directory, and edit those files to match the own configuration. Files are available in https://github.com/CreasolTech/domoticz_lua_scripts
#from linux shell, type the following commands (copy and paste): lines starting with # are comments, and can be ignored
sudo su -
#enter domoticz directory /scripts/lua
if [ -d domoticz_lua_scripts ]; then
#update domoticz_lua_script local repository
if [ $? -ne 0 ]; then
apt install git
#download domoticz_lua_scripts repository with all scripts
git clone https://github.com/CreasolTech/domoticz_lua_scripts.git
cp -i domoticz_lua_scripts/script_device_power.lua domoticz_lua_scripts/config_power.lua .
if [ ! -r globalvariables.lua ]; then cp domoticz_lua_scripts/globalvariables.lua . ; fi
if [ ! -r globalfunctions.lua ]; then cp domoticz_lua_scripts/globalfunctions.lua . ; fi
#now edit config file
Measuring instant power with Eastron SDM230 connected by ModBus
Used to check power from energy meter (SDM120, SDM230, ...) and performs the following actions
1. Send notification when consumed power is above a threshold (to avoid power outage)
2. Enabe/Disable electric heaters or other appliances, to reduced power consumption from the electric grid
3. Emergency lights: turn ON some LED devices in case of power outage, and turn off when power is restored
4. Show on DomBusTH LEDs red and green the produced/consumed power: red LED flashes 1..N times if power consumption is greater than 1..N kW;
green LED flashes 1..M times if photovoltaic produces up to 1..M kW
Destination directory: DOMOTICZ_DIR/scripts/lua
To install the script, type the following commands from the Linux shell, ignoring the comments (starting with #). If you don't know how to do, search the web how to connect to your Linux device using putty or another ssh client.
sudo su -
#install/upgrade curl, a command to fetch files from the web
apt install curl
# go to domoticz directory
# fetch the script
curl -o scripts/lua/script_device_power.lua https://raw.githubusercontent.com/CreasolTech/domoticz_lua_scripts/master/script_device_power.lua
# now, edit the script modifying the first part with your configuration