Script to parse Electricity from P1 Smart Meter reading  [Solved]

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

Moderator: leecollings

JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Script to parse Electricity form P1 Smart Meter reading

Post by JuanUil »

Sorry found it allready, top was not commented out.

But now I get:

Code: Select all

2020-06-14 11:37:00.550 Status: dzVents: Info: SME 0.1.9: ------ Start internal script: Slimme meter:, trigger: "every 1 minutes"
2020-06-14 11:37:00.568 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Power: P1 smart meter energy device adapter
2020-06-14 11:37:00.569 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Verbruik1: Counter device adapter
2020-06-14 11:37:00.569 Status: dzVents: Debug: SME 0.1.9: Set Verbruik1 to:
2020-06-14 11:37:00.570 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Verbruik2: Counter device adapter
2020-06-14 11:37:00.571 Status: dzVents: Debug: SME 0.1.9: Set Verbruik2 to:
2020-06-14 11:37:00.572 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Actueel Verbruik: Counter device adapter
2020-06-14 11:37:00.572 Status: dzVents: Debug: SME 0.1.9: Set Actueel Verbruik to:
2020-06-14 11:37:00.573 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Totaal Verbruik: Counter device adapter
2020-06-14 11:37:00.573 Status: dzVents: Debug: SME 0.1.9: Set Totaal Verbruik to:
2020-06-14 11:37:00.573 Status: dzVents: Info: SME 0.1.9: ------ Finished Slimme meter
2020-06-14 11:37:00.573 Status: dzVents: Info: sqliteDump: ------ Start internal script: Slimme meter ophalen:, trigger: "at 11:37"
2020-06-14 11:37:00.573 Status: dzVents: Info: sqliteDump: ------ Finished Slimme meter ophalen
2020-06-14 11:37:00.574 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-06-14 11:37:00.573 Error: dzVents: Error: (3.0.9) sqliteDump: An error occurred when calling event handler Slimme meter ophalen
2020-06-14 11:37:00.573 Error: dzVents: Error: (3.0.9) sqliteDump: ...ripts/dzVents/generated_scripts/Slimme meter ophalen.lua:55: attempt to concatenate a nil value (global 'allMultimeters')
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to parse Electricity form P1 Smart Meter reading

Post by waaren »

JuanUil wrote: Sunday 14 June 2020 11:37 020-06-14 11:37:00.573 Error: dzVents: Error: (3.0.9) sqliteDump: An error occurred when calling event handler Slimme meter ophalen
2020-06-14 11:37:00.573 Error: dzVents: Error: (3.0.9) sqliteDump: ...ripts/dzVents/generated_scripts/Slimme meter ophalen.lua:55: attempt to concatenate a nil value (global 'allMultimeters')
[/code]
Can you change allMultimeters to allMultiMeters and try again ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JuanUil
Posts: 497
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Script to parse Electricity form P1 Smart Meter reading

Post by JuanUil »

Thnx again.
This did the trick

Code: Select all

2020-06-14 15:11:00.290 Status: dzVents: Info: SME 0.1.9: ------ Start internal script: Slimme meter:, trigger: "every 1 minutes"
2020-06-14 15:11:00.308 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Power: P1 smart meter energy device adapter
2020-06-14 15:11:00.310 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Verbruik1: Counter device adapter
2020-06-14 15:11:00.310 Status: dzVents: Debug: SME 0.1.9: Set Verbruik1 to:
2020-06-14 15:11:00.311 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Verbruik2: Counter device adapter
2020-06-14 15:11:00.311 Status: dzVents: Debug: SME 0.1.9: Set Verbruik2 to:
2020-06-14 15:11:00.312 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Actueel Verbruik: Counter device adapter
2020-06-14 15:11:00.313 Status: dzVents: Debug: SME 0.1.9: Set Actueel Verbruik to:
2020-06-14 15:11:00.314 Status: dzVents: Debug: SME 0.1.9: Processing device-adapter for Totaal Verbruik: Counter device adapter
2020-06-14 15:11:00.314 Status: dzVents: Debug: SME 0.1.9: Set Totaal Verbruik to:
2020-06-14 15:11:00.314 Status: dzVents: Info: SME 0.1.9: ------ Finished Slimme meter
2020-06-14 15:11:00.314 Status: dzVents: Info: sqliteDump: ------ Start internal script: Slimme meter ophalen:, trigger: "at 15:11"
2020-06-14 15:11:00.314 Status: dzVents: Debug: sqliteDump: Executing Command: cp /home/pi/domoticz/domoticz.db safeCopy.db
2020-06-14 15:11:00.356 Status: dzVents: Debug: sqliteDump: ReturnCode: 0
2020-06-14 15:11:00.356 commandOutput:
2020-06-14 15:11:00.356
2020-06-14 15:11:00.356 Status: dzVents: Debug: sqliteDump: Executing Command: rm -f /home/pi/domoticz2excel.csv
2020-06-14 15:11:00.372 Status: dzVents: Debug: sqliteDump: ReturnCode: 0
2020-06-14 15:11:00.372 commandOutput:
2020-06-14 15:11:00.372
2020-06-14 15:11:00.372 Status: dzVents: Debug: sqliteDump: Executing Command: sudo /usr/bin/sqlite3 -header -csv safeCopy.db "select a.id as meter, a.name as naam, b.Value as value, b.counter as counter, from devicestatus a , meter_calendar b where a.id = b.devicerowid and a.id in (44,48);" >> /home/pi/domoticz2excel.csv
2020-06-14 15:11:00.460 Status: dzVents: Debug: sqliteDump: Error ==>> ... but no error message
2020-06-14 15:11:00.460 Status: dzVents: Debug: sqliteDump: Executing Command: sudo /usr/bin/sqlite3 -header -csv safeCopy.db "select a.id as smartMeter, a.name as naam, b.counter1 as consumedLow, b.counter2 as producedLow, b.counter3 as consumedHigh, b.counter2 as producedHigh, b.date as date from devicestatus a , multimeter_calendar b where a.id = b.devicerowid and a.id in (918) order by b.date desc;" >> /home/pi/domoticz2excel.csv
2020-06-14 15:11:00.556 Status: dzVents: Debug: sqliteDump: ReturnCode: 0
2020-06-14 15:11:00.556 commandOutput:
2020-06-14 15:11:00.556
2020-06-14 15:11:00.556 Status: dzVents: Debug: sqliteDump: Executing Command: sudo /usr/bin/sqlite3 -header -csv safeCopy.db "select a.id as temperature, a.name as naam, b.temp_min as min, b.temp_max as max, b.temp_avg as avg, b.date as date from devicestatus a , temperature_calendar b where a.id = b.devicerowid and a.id in (131) order by a.id;" >> /home/pi/domoticz2excel.csv
2020-06-14 15:11:00.665 Status: dzVents: Debug: sqliteDump: ReturnCode: 0
2020-06-14 15:11:00.665 commandOutput:
2020-06-14 15:11:00.665
2020-06-14 15:11:00.665 Status: dzVents: Debug: sqliteDump: Executing Command: rm -f safeCopy.db
2020-06-14 15:11:00.686 Status: dzVents: Debug: sqliteDump: ReturnCode: 0
2020-06-14 15:11:00.686 commandOutput:
2020-06-14 15:11:00.686
2020-06-14 15:11:00.686 Status: dzVents: Debug: sqliteDump: Executing Command: sudo chmod 777 /home/pi/domoticz2excel.csv
2020-06-14 15:11:00.771 Status: dzVents: Debug: sqliteDump: ReturnCode: 0
2020-06-14 15:11:00.771 commandOutput:
2020-06-14 15:11:00.771
2020-06-14 15:11:00.771 Status: dzVents: Info: sqliteDump: ------ Finished Slimme meter ophalen
2020-06-14 15:11:00.772 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua

Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity form P1 Smart Meter reading

Post by DRB2019 »

Today i installed a new P1-Wifi adapter to get the data from my smart meter.
I'am running domoticz Version: 4.10717 and dVents Version: 2.4.19.

Which version of the script can i use?
I see so many different versions, i'am a bit confused now.

Thnx for the reply!
Kind regards,
René
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to parse Electricity form P1 Smart Meter reading

Post by waaren »

DRB2019 wrote: Tuesday 28 July 2020 20:22 Which version of the script can i use?
You can try with the version in my post of Monday 08 June 2020 22:58
The two remaining errors are corrected in this one.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity form P1 Smart Meter reading

Post by DRB2019 »

You can try with the version in my post of Monday 08 June 2020 22:58
The two remaining errors are corrected in this one.
I don’t want to export the data to a database, I only want to see the data in domoticz.
I want to see the readings:
Gas
Total power consumption
Usage power 1.8.1
Usage power 1.8.2
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to parse Electricity form P1 Smart Meter reading

Post by waaren »

DRB2019 wrote: Tuesday 28 July 2020 22:04 I don’t want to export the data to a database, I only want to see the data in domoticz.
Is your adapter already connected to domoticz ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by DRB2019 »

Is your adapter already connected to domoticz ?
YES, the adapter is connected to domoticz.
I want to see separate usage (1.8.1 and 1.8.2)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by waaren »

DRB2019 wrote: Wednesday 29 July 2020 9:26
Is your adapter already connected to domoticz ?
YES, the adapter is connected to domoticz.
I want to see separate usage (1.8.1 and 1.8.2)
Can you share to what device names, types and subtypes the meter is reporting now ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by DRB2019 »

Can you share to what device names, types and subtypes the meter is reporting now ?
I will give you the names and devices when I come home from work!
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by DRB2019 »

Code: Select all

I will give you the names and devices when I come home from work!
Below the devices in domoticz.
Snap1.jpg
Snap1.jpg (114.69 KiB) Viewed 1736 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by waaren »

DRB2019 wrote: Wednesday 29 July 2020 17:47 Below the devices in domoticz.
So if the devices are set to active on the devices tab, you should already see something like below in the utility tab. Is this what you want or do you want to see a split in the total high- and total low tariff ?

P1.png
P1.png (26.19 KiB) Viewed 1734 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by DRB2019 »

I prefer to see the split in total high and low.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by waaren »

DRB2019 wrote: Wednesday 29 July 2020 18:17 I prefer to see the split in total high and low.
See below script. Explanation is in the comments of the script

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 on 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_ERROR,  -- set to domoticz.LOG_DEBUG when script does not produce expected results 
                         marker = 'SME '.. ScriptVersion
              },

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

    execute = function(dz, item)

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

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

        local consumed = 218   -- Meter Actual Usage, Virtual device, counter incremental
        local produced = 219   -- Meter Actual Production, Virtual device, counter incremental

        -- These devices must be created as electric Usage
        local usage = dz.devices('usage')  -- current Usage
        local usageDelivered = dz.devices('usageDelivered')  -- current Return

        -- 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

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by DRB2019 »

See below script. Explanation is in the comments of the script
I will try to get the script running.
Thnx!
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by DRB2019 »

[/quote]
See below script. Explanation is in the comments of the script
[/quote]

How to get this script working with Domoticz Version: 4.10717 and dzVents Version: 2.4.19?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by waaren »

DRB2019 wrote: Thursday 30 July 2020 19:13 How to get this script working with Domoticz Version: 4.10717 and dzVents Version: 2.4.19?
Sorry but I don't know. Because of a bug in the domoticz implementation of incremental counter before build 11305 it will not work properly in earlier versions.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
DRB2019
Posts: 24
Joined: Saturday 06 July 2019 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: Script to parse Electricity from P1 Smart Meter reading

Post by DRB2019 »

Ok, I will try the script and see what happens.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest