Lua - Monitor import/export electricity power

From Domoticz
Jump to navigation Jump to search

Purpose

This LUA script can be used to add the following functions to Domoticz:

  1. power outage detect and emergency lights => turns on Leds (connected to devices supplied by 12V + backup battery) in case of blackout (power failure)
  2. 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, ....)
  3. 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
  4. 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

Block diagram

Diagram representing the features added by the script_device_power.lua script

Although it seems a bit complicted, this diagram can be divided in the following parts:

  1. connection to the energy meter (with pulsed output, 1 pulse every Wh, 1000 pulses per kWh)
  2. 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!)
  3. LEDs to be activated in case of power cut (emergency lights)
  4. 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
  5. temperature and humdity monitoring
  6. relay outputs to activate electric appliances in case of extra-power production (from photovoltaic or other renewable energy source)
  7. heat pump controlling (to modulate its power consumption)
  8. 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.

Imported and exported energy
Imported and exported energy


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:

  1. 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).
  2. 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)
  3. 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.
  4. 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.
  5. 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
    How to find out the idx of power meter devices
    How to find out the idx of power meter devices
    down the Unit number corresponding to Import (for example 98), and Export device (for example 99), then go to Utility panel, edit Import device and add to the description ,OPPOSITE=99 (Unit of the Export device) and save; edit Export device and add to the description ,OPPOSITE=98 (Unit of the Import device) and save. In this way, when a pulse is received on Import device, the power on Export device will be immediately set to 0, and vice versa, when a pulse is received on Export device, the power on Import device will be set to 0.

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

DomBusTH module
DomBusTH module

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.

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.

Installation

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
#become root
sudo su -
#enter domoticz directory /scripts/lua
cd ~pi/Domoticz/scripts/lua
if [ -d domoticz_lua_scripts ]; then
  cd domoticz_lua_scripts
  #update domoticz_lua_script local repository
  git pull
  cd ..
else
  which git
  if [ $? -ne 0 ]; then
    #install git
    apt install git
  fi
  #download domoticz_lua_scripts repository with all scripts
  git clone https://github.com/CreasolTech/domoticz_lua_scripts.git
fi
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
nano config_power.lua