Page 1 of 1

get power value and gas value from a json

Posted: Sunday 05 August 2018 10:13
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?

Re: get power value and gas value from a json

Posted: Sunday 05 August 2018 10:46
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

Re: get power value and gas value from a json

Posted: Sunday 05 August 2018 11:09
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)

Re: get power value and gas value from a json

Posted: Sunday 05 August 2018 11:13
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

Re: get power value and gas value from a json

Posted: Sunday 05 August 2018 11:17
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