Lua - Electric meter pulse counter

From Domoticz
Jump to: navigation, search

Purpose

Many "dumb" electricity meters do not easily allow the meter readings to be directly obtained, but they do flash an LED once for every Wh of energy consumed.

The purpose of this script is to process the raw counter inputs (which are periodically reset), and feed a virtual meter sensor in Domoticz which tracks the actual meter reading.

Dependencies

You will require a counter sensor which holds the raw count value. This is outside the scope of this example, but there are plenty of such devices on the market — such as the OpenEnergyMonitor.org emonTx devices: http://openenergymonitor.org/emon/modules/emonTxV3

The script also uses a user variable to maintain a record of the zero-point of the counter; the meter reading at the time the raw count was last zero.

Domoticz Setup

Input sensors

In my case, the input sensor is virtual; I haven't yet hooked up the rtl_433 receiver software to Domoticz directly, so I'm feeding values in externally. I just created a 'Counter' virtual sensor, then edited it from the Utility tab can changed its subtype to 'Counter'.

Output sensors

The output sensor is also a counter; this time with subtype KwH which is the default.

Zero-point Variable

Simply add a user variable of type 'integer', and assign the current meter reading for now.

Installation instructions

Having configured the devices as described above, all that remains is to edit the configuration in the script below, and place it into your scripts/lua directory.

Script with comments

------------------------------------------------------------------------------
--
-- Copyright © 2016 David Woodhouse <[email protected]> and released under
-- the GNU General Public License, v2 or later.
--
--
-- Old-style electricity meters often don't allow a direct readout of
-- the meter reading. However, there is often a 1 Wh optical pulse which
-- can be counted. Domoticz needs some help processing this though, as the
-- counter device can be reset to zero at arbitrary times (on restart etc.)
--
-- Maintain a user variable which corresponds to the zero point of the
-- counter. When the raw count updates, update the 'output' meter sensor
-- to the count value plus this base value. Except that if that would be
-- lower than the previous reading in the 'output' meter sensor, that
-- means the counter must have been reset. So set the zero-point variable
-- to the previous value of the sensor.
 
------------------------------------------------------------------------------
-- Raw counter sensor
pulse_counter = 'Meter pulse counter'
 
-- Electric meter virtual sensor
meter_name = 'Electric meter'
meter_id = 29
 
-- User variable containing the counter zero point (Wh)
meter_base = 'Meter pulse start'
 
------------------------------------------------------------------------------
commandArray = {}
 
if (devicechanged[pulse_counter]) then
   pulses = tonumber(otherdevices_svalues[pulse_counter])
   prev_meter = tonumber(otherdevices_svalues[meter_name])
 
   base = tonumber(uservariables[meter_base])
   if (base + pulses < prev_meter) then
     -- Pulse counter seems to have been reset.
     print('Pulse count reset; updating base from ' .. base .. ' to ' .. prev_meter)
     base = prev_meter
     commandArray['Variable:' .. meter_base] = tostring(base)
   end
-- print('Meter reading ' .. base .. ' + ' .. pulses .. ' = ' .. base + pulses)
   commandArray['UpdateDevice'] = meter_id .. "|0|" .. base + pulses
end
 
return commandArray

Example of use (if relevant) i.e. output files / screen displays

Domoticz displaying meter readings Domoticz displaying meter base variable