json.lua problem with data from P1 meter

Moderator: leecollings

Post Reply
RNEE
Posts: 7
Joined: Sunday 05 August 2018 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Eindhoven area
Contact:

json.lua problem with data from P1 meter

Post by RNEE »

I could use some help with the Utilty devices.

I bought this P1 meter : https://smart-stuff.nl/product/slimme-m ... -uitlezer/

I flashed DSMR-API firmware on it.
Both original and DSMR-API work well and show data via web interface (very nice piece of home made stuff by Martijn !!)

DSMR-API delivers with a LAU script (https://docs.smart-stuff.nl/v/p1-dongle ... n/domoticz) variables to Domoticz.
Translation via JSON.lua
Data from JSON is coupled tot the device IDX
This works.

RESTer output (partial):

Code: Select all

{
    "timestamp": {
        "value": "240913191538S"
    },
    "energy_delivered_tariff1": {
        "value": 14564.485,
        "unit": "kWh"
    },
    "energy_delivered_tariff2": {
        "value": 10914.064,
        "unit": "kWh"
    },
    "energy_returned_tariff1": {
        "value": 1119.545,
        "unit": "kWh"
    },
    "energy_returned_tariff2": {
        "value": 2752.671,
        "unit": "kWh"
    },
    "electricity_tariff": {
        "value": "0002"
    },
    "power_delivered": {
        "value": 0.977,
        "unit": "kW"
    },
Etc
I made dummy energy devices in Domoticz :
Image

Image



Error in LOG status :
2024-09-13 19:33:32.472 Status: EventSystem: Script event triggered: P1-conversie
2024-09-13 19:34:00.377 Status: LUA: ESP-DSMR Slimme Meter Logger voor leveren stroom & Gas
2024-09-13 19:34:00.378 Status: LUA: Timestamp: 240913193359S
2024-09-13 19:34:00.378 Status: LUA: Energie Verbruik (kWh): 25478.589
2024-09-13 19:34:00.378 Status: LUA: Totaal Verbruik (kW)): 0.389
2024-09-13 19:34:00.379 Status: LUA: ESP-DSMR Slimme Meter Logger voor terugleveren stroom
2024-09-13 19:34:00.379 Status: LUA: Terug Levering (kWh): 3872.247
2024-09-13 19:34:00.379 Status: LUA: Totaal Terug Levering (kW)): 0
2024-09-13 19:34:00.431 Status: Warning: Expecting svalue with at least 5 elements separated by semicolon, 1 elements received ("25478.589"), notification not sent (Hardware: 24 - P1-meter uitlezen, ID: 84238, Unit: 1, Type: FA - P1 Smart Meter, SubType: 1 - Energy)
Script broken ?? --> 2024-09-13 19:34:00.431 Status: EventSystem: Script event triggered: P1-conversie

Time stamp is displayed correct.

Problem has to be in the energy dummy device in Domoticz

Anybody any idea how to tackle this.
FlyingDomotic
Posts: 326
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: json.lua problem with data from P1 meter

Post by FlyingDomotic »

P1 meters require data with 5 different values, separated by a ";".
General format is:
USAGE1;USAGE2;RETURN1;RETURN2;CONS;PROD
Where:
- USAGE1= integer, total cumulative energy usage meter tariff 1 in Wh.
- USAGE2= integer, total cumulative energy usage meter tariff 2 in Wh.
- RETURN1= integer, total cumulative energy return meter tariff 1 in Wh.
- RETURN2= integer, total cumulative energy return meter tariff 2 in Wh.
- CONS= actual usage power (Watt)
- PROD= actual return power (Watt)

Reading the script, virtual devices should not be P1 devices, but simple counters.

You have 2 options:
1) delete all these P1 counters, and create one simple counter for each data. Note in this case that xxx_IDX should be replaced by dummy device IDX (number, 2237 to 2241 in you case), not counter name.
2) delete all these P1 counter and create only one P1 counter, then add in script, just before "return commandArray":

Code: Select all

p1CounterIdx = xxx
counterData =  string.format("%d|0|%d;%d;%d;%d;%d;%d", p1CounterIdx, Energy_Delivered_Tariff1 * 1000, Energy_Delivered_Tariff2 * 1000, Energy_Returned_Tariff1 * 1000, Energy_Returned_Tariff2 * 1000, Power_Delivered * 1000, Power_Returned * 1000)
table.insert (commandArray, {['UpdateDevice'] = counterData})
Replace xxx by IDX of newly created P1 device, probably around 2242.

FYI, multiplication by a thousand is due to the fact that your device is returning kW and KWh while Domoticz needs W and Wh.
RNEE
Posts: 7
Joined: Sunday 05 August 2018 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: Eindhoven area
Contact:

Re: json.lua problem with data from P1 meter

Post by RNEE »

Thank you FlyingDomotic.
I decided tot (re)use a single P1 counter because it can then be used in the Energy Dashboard.

This is the complete (cleaned up) script for those interested :

Code: Select all

-------------------------------------------------------------------------------------------
-- script_time_esp-dsmr-logger.lua		door: Michel Groen
-- aangepast voor : 
-- DSMR-API ver 1.3				door: Henk Koorn
-- P1 meter Counter Domoticz			door: FlyingDomotic
-- Opgeschoond/getest P1 Counter		door: Rnee
-------------------------------------------------------------------------------------------
-- Dit script leest de JSON waardes van DSMR Slimme Meter Logger en vult de P1 counter 
-- (op p1CounterIdx = XXXX ) in Domoticz en kan zo gebruikt worden in het Energie dashboard.
-- -----------------------------------------------------------------------------------------
-- Script 
-- -----------------------------------------------------------------------------------------
local DSMR_IP = "192.168.2.XXX"     -- Ip-Adres ESP-DSMR Slimme Meter Logger of gebruik: 
Firmware      = "DSMR-API" 	     -- Vul in welke firmware je gebruikt. 
domoticz_in_docker = "Nee"              -- Zet Domoticz_in_docker op Ja als Domoticz op Docker is (hoofdlettergevoelig)
-- -----------------------------------------------------------------------------------------
commandArray = {}

if domoticz_in_docker=="Nee" then
    json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
else    
    json = (loadfile "scripts/lua/JSON.lua")()
end

    local jsondata    = assert(io.popen('curl http://'..DSMR_IP..'/api/v2/sm/actual'))
    local jsondevices = jsondata:read('*all')
    jsondata:close()
    
    local jsonCPM = json:decode(jsondevices)

    Energy_Delivered_Tariff1 = tonumber(jsonCPM.energy_delivered_tariff1.value)
    Energy_Delivered_Tariff2 = tonumber(jsonCPM.energy_delivered_tariff2.value)
    Power_Delivered =  jsonCPM.power_delivered.value
    Energy_Returned_Tariff1 = tonumber(jsonCPM.energy_returned_tariff1.value)
    Energy_Returned_Tariff2 = tonumber(jsonCPM.energy_returned_tariff2.value)
    Power_Returned = tonumber(jsonCPM.power_returned.value)
 
    p1CounterIdx = XXXX  -- is device IDX !
    counterData =  string.format("%d|0|%d;%d;%d;%d;%d;%d", p1CounterIdx, Energy_Delivered_Tariff1 * 1000, Energy_Delivered_Tariff2 * 1000, Energy_Returned_Tariff1 * 1000, Energy_Returned_Tariff2 * 1000, Power_Delivered * 1000, Power_Returned * 1000)
    table.insert (commandArray, {['UpdateDevice'] = counterData})

return commandArray
Last edited by RNEE on Saturday 14 September 2024 11:37, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests