Calculating gross energy in dzVents  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
gerrithkd
Posts: 3
Joined: Sunday 30 August 2020 13:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Calculating gross energy in dzVents

Post by gerrithkd »

Hi everyone,

I'm new to this forum. My Domoticz runs on a RPi-4 with a Conbee II stick.

I try to calculatie the energy (Watts) usage before the energy delivered by my SolarEdge system is subtracted (real or gross energy usage). Tried several commands, but keep getting the same kind of typical newbie problem: syntax error. I tried finding a solution in the dzVents Wiki, but without any succes. I hope someone can give me a hint to solve this.

btw: The SolarEdge API works fine.

I started with an extended P1 Smart meter script. To get the result wanted, I added a virtual device (electric) in Domoticz.

Code: Select all

 -- These devices must be created as Usage (electric):
        local usage = dz.devices('Huidig verbruik')  -- current Usage (available)
        local usageDelivered = dz.devices('Huidige teruglevering')  -- current Return  (available)

        local usagePVdelivered = dz.devices(54) -- current Usage PV production         (added by SolarEdge API - kWh)

        local usageReal = dz.devices(59) -- current Usage PV excluded = gross usage  (added as virtual device)

  -- to calculate  gross usage (with PV-production excluded): 

        usageReal = usagePVdelivered + usage - usageDelivered       -- this does not work!
errors in log:

Code: Select all

 2020-08-26 22:34:00.430 Error: dzVents: Error: (3.0.2) SME 0.1.8: An error occurred when calling event handler ElektriciteitInDetail
2020-08-26 22:34:00.430 Error: dzVents: Error: (3.0.2) SME 0.1.8: ...ipts/dzVents/generated_scripts/ElektriciteitInDetail.lua:81: attempt to perform arithmetic on a table value (local 'usagePVdelivered')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

gerrithkd wrote: Sunday 30 August 2020 15:19 I'm new to this forum. My Domoticz runs on a RPi-4 with a Conbee II stick.
In below block you set usagePVdelivered and usageReal to devices. A device in dzVents is a table containing all attributes and functions for that device.

Code: Select all

        local usagePVdelivered = dz.devices(54) -- current Usage PV production         (added by SolarEdge API - kWh)
        local usageReal = dz.devices(59) -- current Usage PV excluded = gross usage  (added as virtual device)
Here you try to calculate with the tables you set above

Code: Select all

        usageReal = usagePVdelivered + usage - usageDelivered       -- this does not work!
If you are looking for help in this forum on scripts then please share the complete script within code tags.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gerrithkd
Posts: 3
Joined: Sunday 30 August 2020 13:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculating gross energy in dzVents

Post by gerrithkd »

Sorry, this programming language is completly new to me. Learning every day. How can I separate the values wanted from the tables?

This is the complete script, without the additions it works fine:

Code: Select all

--[[ 
    dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
    Please note that initially counter today will show wrong value at the GUI until next day
    To be used for domoticz version >= V4.11305

]] --
local fetchIntervalMins = 1    -- (Integer) Minutes frequency of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)
local ScriptVersion = '0.1.8' -- domoticz > V4.11305 / dzVents >= 2.4.28
 
return {

    on =      {
                        timer = { 'every ' .. fetchIntervalMins .. ' minutes' }
              },
             
    logging = {
                       level = domoticz.LOG_DEBUG,    -- Uncomment this line to override the dzVents global logging setting
                       marker = 'SME '.. ScriptVersion
              },

    data = { lastP1 = { initial = {} }},

    execute = function(dz, item)

        -- Add device-names between quotes or device idx without quotes
        local P1  = dz.devices(32) -- Electra, P1 Smart Meter device (idx or "name") (required)

        -- Enter names / idx for devices you want below these comment-lines
        -- These devices must be created as new incremental counters. Script might produce wrong values
        -- when used with existing ones that already contain values
        -- The remaining lines can be removed or commented 
        local usageLow = dz.devices('Verbruik Laag') -- Meter Usage low, Virtual device, counter incremental
        local usageHigh = dz.devices('Verbruik Hoog') -- Meter Usage High, Virtual device, counter incremental
        local returnLow  = dz.devices('Teruglevering Laag')  -- Meter Return Low, Virtual device, counter incremental
        local returnHigh = dz.devices('Teruglevering Hoog')  -- Meter Return High, Virtual device, counter incremental

        -- These devices must be created as Usage (electric)
        local usage = dz.devices('Huidig verbruik')  -- current Usage
        local usageDelivered = dz.devices('Huidige teruglevering')  -- current Return
        local usagePVdelivered = dz.devices(54) -- current Usage PV production -- added to original script
        local usageReal = dz.devices(59) -- current Usage PV excluded          -- added to original script
        
        -- No changes required below this line ---
       
        lastP1 = dz.data.lastP1

        local function updateCounter(dv, value, previousValue )
            if not(dv) then return end
            if not(previousValue) then
                dz.log("No previous data for " .. dv.name .. " yet; skipping this run",dz.LOG_DEBUG)
                return
            end
            if dv.counter ~= 0 then
                value = value - previousValue
            end
            dv.updateCounter(value)
            dz.log("Increment " .. dv.name .. " with: " .. value,dz.LOG_DEBUG)
        end 

        local function updateEnergy(dv, value )
           if not(dv) then return end
           dv.updateEnergy(value)
           dz.log("Set " .. dv.name .. " to: " .. value,dz.LOG_DEBUG)
        end

        -- Update the device
        updateCounter(usageLow, P1.usage1, lastP1.usage1)
        updateCounter(usageHigh, P1.usage2, lastP1.usage2)
        updateCounter(returnLow, P1.return1, lastP1.return1)
        updateCounter(returnHigh, P1.return2, lastP1.return2)

        updateEnergy(usage, P1.usage)
        updateEnergy(usageDelivered, P1.usageDelivered)

        lastP1.usage1 = P1.usage1
        lastP1.usage2 = P1.usage2
        lastP1.return1 = P1.return1
        lastP1.return2 = P1.return2
        
        -- calculate usage with PV-production excluded
       usageReal = usagePVdelivered + usage - usageDelivered -- this does not work!
        
     end
}
---------------------------
The additions result in:
2020-08-26 22:34:00.430 Error: dzVents: Error: (3.0.2) SME 0.1.8: An error occurred when calling event handler ElektriciteitInDetail
2020-08-26 22:34:00.430 Error: dzVents: Error: (3.0.2) SME 0.1.8: ...ipts/dzVents/generated_scripts/ElektriciteitInDetail.lua:81: attempt to perform arithmetic on a table value (local 'usagePVdelivered')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

gerrithkd wrote: Sunday 30 August 2020 22:11 Sorry, this programming language is completely new to me. Learning every day. How can I separate the values wanted from the tables?
Don't know if the formula is correct because I don't understand what you try to calculate but the changes in line 81 should help you understand how to get the values from the devices.
The update of device usageReal is done in function updateEnergy statting at line 60

also check the dzVents wiki and the getting started video.

Code: Select all

--[[ 
    dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
    Please note that initially counter today will show wrong value at the GUI until next day
    To be used for domoticz version >= V4.11305

]] --
local fetchIntervalMins = 1    -- (Integer) Minutes frequency of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)
local ScriptVersion = '0.1.8' -- domoticz > V4.11305 / dzVents >= 2.4.28
 
return {

    on =      {
                        timer = { 'every ' .. fetchIntervalMins .. ' minutes' }
              },
             
    logging = {
                       level = domoticz.LOG_DEBUG,    -- Uncomment this line to override the dzVents global logging setting
                       marker = 'SME '.. ScriptVersion
              },

    data = { lastP1 = { initial = {} }},

    execute = function(dz, item)

        -- Add device-names between quotes or device idx without quotes
        local P1  = dz.devices(32) -- Electra, P1 Smart Meter device (idx or "name") (required)

        -- Enter names / idx for devices you want below these comment-lines
        -- These devices must be created as new incremental counters. Script might produce wrong values
        -- when used with existing ones that already contain values
        -- The remaining lines can be removed or commented 
        local usageLow = dz.devices('Verbruik Laag') -- Meter Usage low, Virtual device, counter incremental
        local usageHigh = dz.devices('Verbruik Hoog') -- Meter Usage High, Virtual device, counter incremental
        local returnLow  = dz.devices('Teruglevering Laag')  -- Meter Return Low, Virtual device, counter incremental
        local returnHigh = dz.devices('Teruglevering Hoog')  -- Meter Return High, Virtual device, counter incremental

        -- These devices must be created as Usage (electric)
        local usage = dz.devices('Huidig verbruik')  -- current Usage
        local usageDelivered = dz.devices('Huidige teruglevering')  -- current Return
        local usagePVdelivered = dz.devices(54) -- current Usage PV production -- added to original script
        local usageReal = dz.devices(59) -- current Usage PV excluded          -- added to original script
        
        -- No changes required below this line ---
       
        lastP1 = dz.data.lastP1

        local function updateCounter(dv, value, previousValue )
            if not(dv) then return end
            if not(previousValue) then
                dz.log("No previous data for " .. dv.name .. " yet; skipping this run",dz.LOG_DEBUG)
                return
            end
            if dv.counter ~= 0 then
                value = value - previousValue
            end
            dv.updateCounter(value)
            dz.log("Increment " .. dv.name .. " with: " .. value,dz.LOG_DEBUG)
        end 

        local function updateEnergy(dv, value )
           if not(dv) then return end
           dv.updateEnergy(value)
           dz.log("Set " .. dv.name .. " to: " .. value,dz.LOG_DEBUG)
        end

        -- Update the device
        updateCounter(usageLow, P1.usage1, lastP1.usage1)
        updateCounter(usageHigh, P1.usage2, lastP1.usage2)
        updateCounter(returnLow, P1.return1, lastP1.return1)
        updateCounter(returnHigh, P1.return2, lastP1.return2)

        updateEnergy(usage, P1.usage)
        updateEnergy(usageDelivered, P1.usageDelivered)

        lastP1.usage1 = P1.usage1
        lastP1.usage2 = P1.usage2
        lastP1.return1 = P1.return1
        lastP1.return2 = P1.return2
        
        -- calculate usage with PV-production excluded
        updateEnergy(usageReal, usagePVdelivered.actualWatt + P1.usage - P1.usageDelivered )
        
     end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gerrithkd
Posts: 3
Joined: Sunday 30 August 2020 13:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Calculating gross energy in dzVents  [Solved]

Post by gerrithkd »

Hi Waaren, This works!

Goal of the additions:
If the solar panels produce more than the total energy consumption, 'usage' will show zero.
With the additions to the script, 'usageReal' shows the actual energy consumption as if there were no solar panels.
This makes it easier to locate power consuming devices.

Tnx for your help.
eddieb
Posts: 341
Joined: Wednesday 04 July 2018 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by eddieb »

waaren wrote: Sunday 30 August 2020 23:01
Hi Waaren,

I created the output device as kwh but that does not seem to work
What kind of devices should this all be ?

Eddie
RPI4 Beta / Tasmota / ZigBee2MQTT / P1meter / Haier AC with Node-Red and MQTT / SolarEdge SE3500H modbus_tcp / Opentherm gateway / Plugwise Anna/Smile / ObserverIP weatherstation thru WuDirect
Feeding ADSB https://adsb.im/home
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

eddieb wrote: Thursday 17 September 2020 17:23 I created the output device as kwh but that does not seem to work
What kind of devices should this all be ?
There are more versions of this script. Which one do you you use? Please share and include resulting loglines.
Also please give some more detail on what you mean with output device. The script outputs to incremental counters and to virtual "usage (electric)" sensors
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
eddieb
Posts: 341
Joined: Wednesday 04 July 2018 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by eddieb »

waaren wrote: Thursday 17 September 2020 17:51 There are more versions of this script. Which one do you you use? Please share and include resulting loglines.
Also please give some more detail on what you mean with output device. The script outputs to incremental counters and to virtual "usage (electric)" sensors
I just took the script in this thread. Did not know there were multiple versions.
I got my Solaredge panels a couple of days ago and just wanted to see the "normal" usage again ...
Before I started "delivering" the p1 output was enough for me but now that is not usable for this anymore ...
Any suggestions ?

Eddie
RPI4 Beta / Tasmota / ZigBee2MQTT / P1meter / Haier AC with Node-Red and MQTT / SolarEdge SE3500H modbus_tcp / Opentherm gateway / Plugwise Anna/Smile / ObserverIP weatherstation thru WuDirect
Feeding ADSB https://adsb.im/home
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

eddieb wrote: Thursday 17 September 2020 17:56 I just took the script in this thread. Did not know there were multiple versions.
There are 2 versions of the script in this thread so if yours is not working as expected please share the one you use and double check if you have set the output devices to the required types. Also please include the resulting loglines; it will make it easier to help you.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
eddieb
Posts: 341
Joined: Wednesday 04 July 2018 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by eddieb »

Sorry @Waaren, must have been sleeping ..
I used the scrip you posted, see quoted.
waaren wrote: Sunday 30 August 2020 23:01
Don't know if the formula is correct because I don't understand what you try to calculate but the changes in line 81 should help you understand how to get the values from the devices.
The update of device usageReal is done in function updateEnergy statting at line 60

also check the dzVents wiki and the getting started video.

Code: Select all

--[[ 
    dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
    Please note that initially counter today will show wrong value at the GUI until next day
    To be used for domoticz version >= V4.11305

]] --
local fetchIntervalMins = 1    -- (Integer) Minutes frequency of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)
local ScriptVersion = '0.1.8' -- domoticz > V4.11305 / dzVents >= 2.4.28
 
return {

    on =      {
                        timer = { 'every ' .. fetchIntervalMins .. ' minutes' }
              },
             
    logging = {
                       level = domoticz.LOG_DEBUG,    -- Uncomment this line to override the dzVents global logging setting
                       marker = 'SME '.. ScriptVersion
              },

    data = { lastP1 = { initial = {} }},

    execute = function(dz, item)

        -- Add device-names between quotes or device idx without quotes
        local P1  = dz.devices(32) -- Electra, P1 Smart Meter device (idx or "name") (required)

        -- Enter names / idx for devices you want below these comment-lines
        -- These devices must be created as new incremental counters. Script might produce wrong values
        -- when used with existing ones that already contain values
        -- The remaining lines can be removed or commented 
        local usageLow = dz.devices('Verbruik Laag') -- Meter Usage low, Virtual device, counter incremental
        local usageHigh = dz.devices('Verbruik Hoog') -- Meter Usage High, Virtual device, counter incremental
        local returnLow  = dz.devices('Teruglevering Laag')  -- Meter Return Low, Virtual device, counter incremental
        local returnHigh = dz.devices('Teruglevering Hoog')  -- Meter Return High, Virtual device, counter incremental

        -- These devices must be created as Usage (electric)
        local usage = dz.devices('Huidig verbruik')  -- current Usage
        local usageDelivered = dz.devices('Huidige teruglevering')  -- current Return
        local usagePVdelivered = dz.devices(54) -- current Usage PV production -- added to original script
        local usageReal = dz.devices(59) -- current Usage PV excluded          -- added to original script
        
        -- No changes required below this line ---
       
        lastP1 = dz.data.lastP1

        local function updateCounter(dv, value, previousValue )
            if not(dv) then return end
            if not(previousValue) then
                dz.log("No previous data for " .. dv.name .. " yet; skipping this run",dz.LOG_DEBUG)
                return
            end
            if dv.counter ~= 0 then
                value = value - previousValue
            end
            dv.updateCounter(value)
            dz.log("Increment " .. dv.name .. " with: " .. value,dz.LOG_DEBUG)
        end 

        local function updateEnergy(dv, value )
           if not(dv) then return end
           dv.updateEnergy(value)
           dz.log("Set " .. dv.name .. " to: " .. value,dz.LOG_DEBUG)
        end

        -- Update the device
        updateCounter(usageLow, P1.usage1, lastP1.usage1)
        updateCounter(usageHigh, P1.usage2, lastP1.usage2)
        updateCounter(returnLow, P1.return1, lastP1.return1)
        updateCounter(returnHigh, P1.return2, lastP1.return2)

        updateEnergy(usage, P1.usage)
        updateEnergy(usageDelivered, P1.usageDelivered)

        lastP1.usage1 = P1.usage1
        lastP1.usage2 = P1.usage2
        lastP1.return1 = P1.return1
        lastP1.return2 = P1.return2
        
        -- calculate usage with PV-production excluded
        updateEnergy(usageReal, usagePVdelivered.actualWatt + P1.usage - P1.usageDelivered )
        
     end
}
I tried the usageReal device as kWh meter but that did not work ...
I was looking vor a kWh as counterpart of the p1 kWh meter but now for the real power usage.
Eddie
RPI4 Beta / Tasmota / ZigBee2MQTT / P1meter / Haier AC with Node-Red and MQTT / SolarEdge SE3500H modbus_tcp / Opentherm gateway / Plugwise Anna/Smile / ObserverIP weatherstation thru WuDirect
Feeding ADSB https://adsb.im/home
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

eddieb wrote: Friday 18 September 2020 7:09 I used the scrip you posted, see quoted.

-- These devices must be created as Usage (electric)
local usageReal = dz.devices(59) -- current Usage PV excluded -- added to original script

I tried the usageReal device as kWh meter but that did not work ...
The usageReal device should be created as a Usage (electric) device.

If your script does not work as expected and you are looking for support on this forum please always include the loglines for the script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
eddieb
Posts: 341
Joined: Wednesday 04 July 2018 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by eddieb »

Tnx Waaren, I changed the device to Usage and now it works.

Any idea what should be changed to get a kWh device ?
RPI4 Beta / Tasmota / ZigBee2MQTT / P1meter / Haier AC with Node-Red and MQTT / SolarEdge SE3500H modbus_tcp / Opentherm gateway / Plugwise Anna/Smile / ObserverIP weatherstation thru WuDirect
Feeding ADSB https://adsb.im/home
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

eddieb wrote: Friday 18 September 2020 8:11 Any idea what should be changed to get a kWh device ?
It depends on the answer of the next question.
What are the device type(s) and device subtype(s) of your "usagePVdelivered" device(s) (the domoticz device(s) showing your solar panel production)?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
eddieb
Posts: 341
Joined: Wednesday 04 July 2018 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by eddieb »

@Waaren, my Solaredge produces the following devices :
Screenshot 2020-09-18 at 09.20.03.png
Screenshot 2020-09-18 at 09.20.03.png (98.59 KiB) Viewed 1972 times
my p1 gives me
Screenshot 2020-09-18 at 09.20.19.png
Screenshot 2020-09-18 at 09.20.19.png (81.1 KiB) Viewed 1972 times
Eddie
RPI4 Beta / Tasmota / ZigBee2MQTT / P1meter / Haier AC with Node-Red and MQTT / SolarEdge SE3500H modbus_tcp / Opentherm gateway / Plugwise Anna/Smile / ObserverIP weatherstation thru WuDirect
Feeding ADSB https://adsb.im/home
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

eddieb wrote: Friday 18 September 2020 9:22 @Waaren, my Solaredge produces the following devices :
Screenshot 2020-09-18 at 09.20.03.png
my p1 gives me
Can you try with this one?

Code: Select all

--[[
    dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
    Please note that initially counter today will show wrong value at the GUI until next day
    To be used for domoticz version >= V4.11305

]] --
local fetchIntervalMins = 1    -- (Integer) Minutes frequency of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)
local ScriptVersion = '0.1.9' -- domoticz > V4.11305 / dzVents >= 2.4.28

return {

    on =      {
                        timer = { 'every ' .. fetchIntervalMins .. ' minutes' }
              },

    logging = {
                       level = domoticz.LOG_DEBUG,    -- Uncomment this line to override the dzVents global logging setting
                       marker = 'SME '.. ScriptVersion
              },

    data = { lastP1 = { initial = {} }},

    execute = function(dz, item)

        -- Add device-names between quotes or device idx without quotes
        local P1  = dz.devices(32) -- Electra, P1 Smart Meter device (idx or "name") (required)
        local solarEnergy = dz.devices(782) -- You solar production kwH device

        -- Enter names / idx for devices you want below these comment-lines
        -- These devices must be created as new incremental counters. Script might produce wrong values
        -- when used with existing ones that already contain values
        -- The remaining lines can be removed or commented
        local usageLow = dz.devices('Verbruik Laag') -- Meter Usage low, Virtual device, counter incremental
        local usageHigh = dz.devices('Verbruik Hoog') -- Meter Usage High, Virtual device, counter incremental
        local returnLow  = dz.devices('Teruglevering Laag')  -- Meter Return Low, Virtual device, counter incremental
        local returnHigh = dz.devices('Teruglevering Hoog')  -- Meter Return High, Virtual device, counter incremental

        -- These devices must be created as Usage (electric)
        local usage = dz.devices('Huidig verbruik')  -- current Usage
        local usageDelivered = dz.devices('Huidige teruglevering')  -- current Return
        local usagePVdelivered = dz.devices(54) -- current Usage PV production -- added to original script
        local usageReal = dz.devices(59) -- current Usage PV excluded          -- added to original script

        -- This devices must be created as electric (usage + counter) Please note that today will not show a correct value on day 1
        local realUsage = dz.devices('realUsage' ) -- Electric (usage + counter)

        -- No changes required below this line ---

        lastP1 = dz.data.lastP1

        local function updateCounter(dv, value, previousValue )
            if not(dv) then return end
            if not(previousValue) then
                dz.log("No previous data for " .. dv.name .. " yet; skipping this run",dz.LOG_DEBUG)
                return
            end
            if dv.counter ~= 0 then
                value = value - previousValue
            end
            dv.updateCounter(value)
            dz.log("Increment " .. dv.name .. " with: " .. value,dz.LOG_DEBUG)
        end

        local function updateEnergy(dv, value )
           if not(dv) then return end
           dv.updateEnergy(value)
           dz.log("Set " .. dv.name .. " to: " .. value,dz.LOG_DEBUG)
        end

        local function updateElectricity(dv, energy, power)
            if not(dv) then return end
            dv.updateElectricity( power, energy )
            dz.log("Set " .. dv.name .. "Energy to: " .. energy .. ' kWH and Power to ' .. power .. ' Watt',dz.LOG_DEBUG)
        end

        -- Update the devices
        updateCounter(usageLow, P1.usage1, lastP1.usage1)
        updateCounter(usageHigh, P1.usage2, lastP1.usage2)
        updateCounter(returnLow, P1.return1, lastP1.return1)
        updateCounter(returnHigh, P1.return2, lastP1.return2)

        updateEnergy(usage, P1.usage)
        updateEnergy(usageDelivered, P1.usageDelivered)

        lastP1.usage1 = P1.usage1
        lastP1.usage2 = P1.usage2
        lastP1.return1 = P1.return1
        lastP1.return2 = P1.return2

        updateElectricity(realUsage, ( solarEnergy.WhTotal + P1.usage1 + P1.usage2 - P1.return1 - P1.return2 ) , ( P1.usage + solarEnergy.actualWatt - P1.usageDelivered )  )

        -- calculate usage with PV-production excluded
        updateEnergy(usageReal, usagePVdelivered.actualWatt + P1.usage - P1.usageDelivered )

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
eddieb
Posts: 341
Joined: Wednesday 04 July 2018 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by eddieb »

waaren wrote: Friday 18 September 2020 19:56
Hi @Waaren, sure, I will try this. The sun went down 10min ago so I won't have results before tomorrow morning ;-)
I will let you know.
RPI4 Beta / Tasmota / ZigBee2MQTT / P1meter / Haier AC with Node-Red and MQTT / SolarEdge SE3500H modbus_tcp / Opentherm gateway / Plugwise Anna/Smile / ObserverIP weatherstation thru WuDirect
Feeding ADSB https://adsb.im/home
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by waaren »

eddieb wrote: Friday 18 September 2020 20:01 The sun went down 10min ago so I won't have results before tomorrow morning ;-)
The script should also work without sunlight :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
eddieb
Posts: 341
Joined: Wednesday 04 July 2018 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Calculating gross energy in dzVents

Post by eddieb »

waaren wrote: Friday 18 September 2020 21:28 The script should also work without sunlight :)
Tnx @waaren. It seems to work fine, just what I wanted !
Screenshot 2020-09-19 at 08.00.28.png
Screenshot 2020-09-19 at 08.00.28.png (273.7 KiB) Viewed 1898 times
SE = SolarEdge
RE = calculated Real

exact code I used below,

tnx,
Eddie

Code: Select all

--[[
    dzVents script to Parse P1 Smart Meter Electricity value into separate Meter Readings.
    Please note that initially counter today will show wrong value at the GUI until next day
    To be used for domoticz version >= V4.11305

]] --
local fetchIntervalMins = 1    -- (Integer) Minutes frequency of this script execution 1 = every minute, 10 = every 10 minutes, etc ) must be one of (1,2,3,4,5,6,10,12,15,20,30)
local ScriptVersion = '0.1.9' -- domoticz > V4.11305 / dzVents >= 2.4.28

return {

    on =      {
                        timer = { 'every ' .. fetchIntervalMins .. ' minutes' }
              },

    logging = {
                       level = domoticz.LOG_DEBUG,    -- Uncomment this line to override the dzVents global logging setting
                       marker = 'SME '.. ScriptVersion
              },

    data = { lastP1 = { initial = {} }},

    execute = function(dz, item)

        -- Add device-names between quotes or device idx without quotes
        local P1  = dz.devices(192) -- Electra, P1 Smart Meter device (idx or "name") (required)
        local solarEnergy = dz.devices(792) -- You solar production kwH device

        -- Enter names / idx for devices you want below these comment-lines
        -- These devices must be created as new incremental counters. Script might produce wrong values
        -- when used with existing ones that already contain values
        -- The remaining lines can be removed or commented
        --- local usageLow = dz.devices('Verbruik Laag') -- Meter Usage low, Virtual device, counter incremental
        --- local usageHigh = dz.devices('Verbruik Hoog') -- Meter Usage High, Virtual device, counter incremental
        --- local returnLow  = dz.devices('Teruglevering Laag')  -- Meter Return Low, Virtual device, counter incremental
        --- local returnHigh = dz.devices('Teruglevering Hoog')  -- Meter Return High, Virtual device, counter incremental

        -- These devices must be created as Usage (electric)
        --- local usage = dz.devices('Huidig verbruik')  -- current Usage
        --- local usageDelivered = dz.devices('Huidige teruglevering')  -- current Return
        --- local usagePVdelivered = dz.devices(792) -- current Usage PV production -- added to original script
        local usageReal = dz.devices(798) -- current Usage PV excluded          -- added to original script

        -- This devices must be created as electric (usage + counter) Please note that today will not show a correct value on day 1
        local realUsage = dz.devices(799) -- Electric (usage + counter)

        -- No changes required below this line ---

        lastP1 = dz.data.lastP1

        local function updateCounter(dv, value, previousValue )
            if not(dv) then return end
            if not(previousValue) then
                dz.log("No previous data for " .. dv.name .. " yet; skipping this run",dz.LOG_DEBUG)
                return
            end
            if dv.counter ~= 0 then
                value = value - previousValue
            end
            dv.updateCounter(value)
            dz.log("Increment " .. dv.name .. " with: " .. value,dz.LOG_DEBUG)
        end

        local function updateEnergy(dv, value )
           if not(dv) then return end
           dv.updateEnergy(value)
           dz.log("Set " .. dv.name .. " to: " .. value,dz.LOG_DEBUG)
        end

        local function updateElectricity(dv, energy, power)
            if not(dv) then return end
            dv.updateElectricity( power, energy )
            dz.log("Set " .. dv.name .. "Energy to: " .. energy .. ' kWH and Power to ' .. power .. ' Watt',dz.LOG_DEBUG)
        end

        -- Update the devices
        updateCounter(usageLow, P1.usage1, lastP1.usage1)
        updateCounter(usageHigh, P1.usage2, lastP1.usage2)
        updateCounter(returnLow, P1.return1, lastP1.return1)
        updateCounter(returnHigh, P1.return2, lastP1.return2)

        updateEnergy(usage, P1.usage)
        updateEnergy(usageDelivered, P1.usageDelivered)

        lastP1.usage1 = P1.usage1
        lastP1.usage2 = P1.usage2
        lastP1.return1 = P1.return1
        lastP1.return2 = P1.return2

        updateElectricity(realUsage, ( solarEnergy.WhTotal + P1.usage1 + P1.usage2 - P1.return1 - P1.return2 ) , ( P1.usage + solarEnergy.actualWatt - P1.usageDelivered )  )

        -- calculate usage with solarEnergy excluded
        updateEnergy(usageReal, solarEnergy.actualWatt + P1.usage - P1.usageDelivered )

    end
}
RPI4 Beta / Tasmota / ZigBee2MQTT / P1meter / Haier AC with Node-Red and MQTT / SolarEdge SE3500H modbus_tcp / Opentherm gateway / Plugwise Anna/Smile / ObserverIP weatherstation thru WuDirect
Feeding ADSB https://adsb.im/home
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest