get power value and gas value from a json

Moderator: leecollings

Post Reply
User avatar
mAiden88
Posts: 56
Joined: Friday 14 October 2016 22:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7286
Location: Almelo
Contact:

get power value and gas value from a json

Post by mAiden88 »

I try to make a LUA script.. Didn't see whats wrong, why Domoticz don't update my sensor.

Code: Select all

commandArray = {}

    powerUsage = uservariables['UV_StroomSensorName']
    ToonIP = uservariables['UV_ToonIP']
    DomoticzIP = uservariables['UV_DomoticzIP']

    json = assert(loadfile "/home/domoticz/scripts/lua/JSON.lua")()  -- For Raspberry
    
    local handle = assert(io.popen(string.format('curl http://%s/happ_pwrusage?action=GetCurrentUsage', ToonIP)))
        local jsonInfo = handle:read('*all')
    handle:close()
    
    jsonInfo = string.gsub(jsonInfo, ",}", "}")
    
    jsonInfo = json:decode(jsonInfo)
    
    powerUsage = tonumber(jsonInfo.powerUsage.value)

    -- Update the power sensor to current usage
    if otherdevices_svalues[powerUsage] ~= powerUsage then  
        print('Updating power sensor to new usage: ' ..powerUsage)
        commandArray[1] = {['UpdateDevice'] = string.format('%s|0|%s', otherdevices_idx[powerUsage], powerUsage)}
    end

--
All uservariables are ok. Link is my Toon Thermostat, give this in the browser:

Code: Select all

{"result":"ok",
"powerUsage": {"value":183, "dayCost":0.97, "avgValue":550.61},
"powerProduction": {"value":0, "dayCost":0.00, "avgValue":0.00},
"gasUsage": {"value":25, "dayCost":0.32,"avgValue":6.22}
}
How can i read this in domoticz, i mean powerUsage value and gasUsage value?
Anyone can build a fast processor. The trick is to build a fast system.
User avatar
jvdz
Posts: 2330
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: get power value and gas value from a json

Post by jvdz »

You are using the variable powerUsage for both the devicename and the actual value, so the deviceupdate first parameter doesn't contain the proper IDX, since the name is changed to the found consumption value.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
mAiden88
Posts: 56
Joined: Friday 14 October 2016 22:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7286
Location: Almelo
Contact:

Re: get power value and gas value from a json

Post by mAiden88 »

jvdz wrote: Sunday 05 August 2018 10:46 You are using the variable powerUsage for both the devicename and the actual value, so the deviceupdate first parameter doesn't contain the proper IDX, since the name is changed to the found consumption value.

Jos
If I understand you correctly, should I change devicename? But still come Domoticz with this error message

Code: Select all

2018-08-05 11:06:34.021 Error: EventSystem: in StroomGasToon: [string "commandArray = {}..."]:21: attempt to concatenate global 'powerUsage' (a nil value)
2018-08-05 11:06:44.054 Error: EventSystem: in StroomGasToon: [string "commandArray = {}..."]:21: attempt to concatenate global 'powerUsage' (a nil value)
Anyone can build a fast processor. The trick is to build a fast system.
User avatar
jvdz
Posts: 2330
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: get power value and gas value from a json

Post by jvdz »

No, the variable you use to store the devicename in your script. Something like this:

Code: Select all

commandArray = {}

    powerUsageName = uservariables['UV_StroomSensorName']
    ToonIP = uservariables['UV_ToonIP']
    DomoticzIP = uservariables['UV_DomoticzIP']

    json = assert(loadfile "/home/domoticz/scripts/lua/JSON.lua")()  -- For Raspberry
    
    local handle = assert(io.popen(string.format('curl http://%s/happ_pwrusage?action=GetCurrentUsage', ToonIP)))
        local jsonInfo = handle:read('*all')
    handle:close()
    
    jsonInfo = string.gsub(jsonInfo, ",}", "}")
    
    jsonInfo = json:decode(jsonInfo)
    
    powerUsage = tonumber(jsonInfo.powerUsage.value)

    -- Update the power sensor to current usage
    if otherdevices_svalues[powerUsage] ~= powerUsage then  
        print('Updating power sensor to new usage: ' ..powerUsage)
        commandArray[1] = {['UpdateDevice'] = string.format('%s|0|%s', otherdevices_idx[powerUsageName], powerUsage)}
    end

--
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
mAiden88
Posts: 56
Joined: Friday 14 October 2016 22:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7286
Location: Almelo
Contact:

Re: get power value and gas value from a json

Post by mAiden88 »

jvdz wrote: Sunday 05 August 2018 11:13 No, the variable you use to store the devicename in your script. Something like this:

Code: Select all

commandArray = {}

    powerUsageName = uservariables['UV_StroomSensorName']
    ToonIP = uservariables['UV_ToonIP']
    DomoticzIP = uservariables['UV_DomoticzIP']

    json = assert(loadfile "/home/domoticz/scripts/lua/JSON.lua")()  -- For Raspberry
    
    local handle = assert(io.popen(string.format('curl http://%s/happ_pwrusage?action=GetCurrentUsage', ToonIP)))
        local jsonInfo = handle:read('*all')
    handle:close()
    
    jsonInfo = string.gsub(jsonInfo, ",}", "}")
    
    jsonInfo = json:decode(jsonInfo)
    
    powerUsage = tonumber(jsonInfo.powerUsage.value)

    -- Update the power sensor to current usage
    if otherdevices_svalues[powerUsage] ~= powerUsage then  
        print('Updating power sensor to new usage: ' ..powerUsage)
        commandArray[1] = {['UpdateDevice'] = string.format('%s|0|%s', otherdevices_idx[powerUsageName], powerUsage)}
    end

--
Jos
Thanks Jos. That one is working. I feel a bit stupid. So small mistake.
Now look if i can convert this one to import gas usage
Anyone can build a fast processor. The trick is to build a fast system.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest