JSon values to virtual sensors  [Solved]

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

Moderator: leecollings

Post Reply
mikeoo
Posts: 110
Joined: Sunday 22 March 2015 7:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

JSon values to virtual sensors

Post by mikeoo »

I am using DSMR-Logger to read the values from my electricity and gas meter and put them in Domoticz.
After a update of DSMR-Logger it is working with RestApi and now i need to make a new script for it but my skills in DZvents are not there yet.

Hope someone would help creating a script for it. Would be nice i you can get logging also.

When i go to URL http://192.168.0.24/api/v1/sm/actual i get:

Code: Select all

{"actual":[

{"name": "timestamp", "value": "200416155148S"},

{"name": "energy_delivered_tariff1", "value": 3037.105, "unit": "kWh"},

{"name": "energy_delivered_tariff2", "value": 1514.748, "unit": "kWh"},

{"name": "energy_returned_tariff1", "value": 0.000, "unit": "kWh"},

{"name": "energy_returned_tariff2", "value": 0.000, "unit": "kWh"},

{"name": "power_delivered", "value": 0.344, "unit": "kW"},

{"name": "power_returned", "value": 0.000, "unit": "kW"},

{"name": "voltage_l1", "value": 227.000, "unit": "V"},

{"name": "current_l1", "value": 1, "unit": "A"},

{"name": "power_delivered_l1", "value": 0.344, "unit": "kW"},

{"name": "power_returned_l1", "value": 0.000, "unit": "kW"},

{"name": "gas_delivered", "value": 1565.780, "unit": "m3"}

]}

What i would like to is to read the url and put the values in the virtual sensors and that they are updating every minute.

The senors i use in Domoticz are

Code: Select all

Timestamp_IDX = 					         -- Dummy = Text
Gas_Delivered_IDX = 4702	 				 -- Dummy = Counter Incremental --> Gas
Energy_Delivered_Tariff1_IDX = 4707  		 -- Dummy = Counter Incremental --> Engergy
Energy_Delivered_Tariff2_IDX = 4706                  -- Dummy = Counter Incremental --> Engergy
Power_Delivered_IDX = 4703  				 -- Dummy - Electric
Voltage_l1_IDX = 4710					 -- Dummy - Voltage
Current_l1_IDX = 4711					 -- Dummy - Amperage
Power_Delivered_l1_IDX = 4705 		         -- Dummy = Electric
Energy_Returned_Tariff1_IDX =  		     	 -- Dummy = Counter Incremental --> Engergy 
Energy_Returned_Tariff2_IDX =   	    	 	 -- Dummy = Counter Incremental --> Engergy
Power_Returned_IDX =  				     	 -- Dummy - Electric 
Power_Returned_l1_IDX =  		 		 -- Dummy = Electric
Found some examples and with a local url i get the see a value. But don't get how the use the URL with live data and use more then one value. Also the url presents the data like above and not like one line with text.

What i tried to create.

Code: Select all

-- dsmr.lua

return { 
            on = {   timer  =   { "every minute" },},
       
        logging    =    {   
                            level       =  domoticz.LOG_DEBUG,   
                            marker      =  "DSMR-Logger" 
                        },    

    execute = function(dz)
        local function logWrite(str,level)             -- Support function for shorthand debug log statements
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
	local url = '{"actual":[{"name": "timestamp", "value": "200416155148S"}, {"name": "energy_delivered_tariff1", "value": 3037.105, "unit": "kWh"}, {"name": "energy_delivered_tariff2", "value": 1514.748, "unit": "kWh"}, {"name": "energy_returned_tariff1", "value": 0.000, "unit": "kWh"}, {"name": "energy_returned_tariff2", "value": 0.000, "unit": "kWh"}, {"name": "power_delivered", "value": 0.344, "unit": "kW"}, {"name": "power_returned", "value": 0.000, "unit": "kW"}, {"name": "voltage_l1", "value": 227.000, "unit": "V"}, {"name": "current_l1", "value": 1, "unit": "A"}, {"name": "power_delivered_l1", "value": 0.344, "unit": "kW"}, {"name": "power_returned_l1", "value": 0.000, "unit": "kW"}, {"name": "gas_delivered", "value": 1565.780, "unit": "m3"} ]}'

--	local url = "http://192.168.0.24/api/v1/sm/actual"

        local rt        = dz.utils.fromJSON(url)        -- convert complete json to table
        local dsmr      = rt.actual[1]                  -- liveweer is a key in table (or "associative array") rt and contains one or more subtables. You want the first one for key, value in pairs(weer) 
	
	for key, value in pairs(dsmr) do
            logWrite(key .. ": " .. tostring(value))
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSon values to virtual sensors

Post by waaren »

mikeoo wrote: Thursday 16 April 2020 16:48 I am using DSMR-Logger to read the values from my electricity and gas meter and put them in Domoticz.
After a update of DSMR-Logger it is working with RestApi and now i need to make a new script for it but my skills in DZvents are not there yet.
I am working on it. Also for another forum member. I expect to have a first version somehere this coming weekend
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
mikeoo
Posts: 110
Joined: Sunday 22 March 2015 7:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: JSon values to virtual sensors

Post by mikeoo »

waaren wrote: Friday 17 April 2020 14:55
mikeoo wrote: Thursday 16 April 2020 16:48 I am using DSMR-Logger to read the values from my electricity and gas meter and put them in Domoticz.
After a update of DSMR-Logger it is working with RestApi and now i need to make a new script for it but my skills in DZvents are not there yet.
I am working on it. Also for another forum member. I expect to have a first version somehere this coming weekend

Thank you, what a great service :D
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSon values to virtual sensors  [Solved]

Post by waaren »

mikeoo wrote: Thursday 16 April 2020 16:48 I am using DSMR-Logger to read the values from my electricity and gas meter and put them in Domoticz.
After a update of DSMR-Logger it is working with RestApi and now i need to make a new script for it but my skills in DZvents are not there yet.
Posted an initial dzVents version here
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest