Two JSON Calls  [Solved]

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

Moderator: leecollings

Post Reply
CronoS
Posts: 135
Joined: Wednesday 15 July 2015 23:40
Target OS: -
Domoticz version:
Contact:

Two JSON Calls

Post by CronoS »

Hi,

I have a quick question. I have a DZVents script that calculate the MonthTotal for my P1 meter by using a Json call. Basically this script works OK
Now, I wanted to add a new script that does a JSON call to my SolarEdge energy meter, to calculate what I use in my house for the month. Basically to start with it, I created a copy of the initial script. I have changed the local httpResponses to a different value so that it is a seperate script. But I am doing something wrong here, because when I enable this script the other script does not find any value's anymore. When I disable this script, the old script works OK. Could it be that two JSON calls around in the same time can be the problem? Or is there something more necessary to make this script unique?

A small snippit of the new script:

Code: Select all

local httpResponses = "monthTotal_Sun"

return {
    on      =   {   
                    timer           =   { "every 1 minutes" },
                    httpResponses   =   {'httpResponses'} 
                },

    logging =   {   
                    level           =   domoticz.LOG_ERROR, -- set to LOG_ERROR when script works as expected
                    marker          =   httpResponse   
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        sunDevice = dz.devices(1055)          -- Replace xxxx with ID of energyDevice you want to track
        monthTotal = dz.devices(1032)        -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
        -- ****************************** No changes required below this line *********************************************
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSONSun(id, period, delay)
            local delay = delay or 0
            local  URLString   =    'http://192.168.0.4:8084/json.htm?type=graph&sensor=counter&range=month&idx=1055'
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses .. "_" .. period}).afterSec(delay)                      
        end
and a snippit of the P1 script:

Code: Select all

local httpResponses = "monthTotal"

return {
    on      =   {   
                    timer           =   { "every 5 minutes" },
                    httpResponses   =   { httpResponses .. "*" } 
                },

    logging =   {   
                    level           =   domoticz.LOG_ERROR, -- set to LOG_ERROR when script works as expected
                    marker          =   httpResponse   
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        usageDevice = dz.devices(1008)          -- Replace xxxx with ID of energyDevice you want to track
        monthTotal = dz.devices(1033)        -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
        -- ****************************** No changes required below this line *********************************************
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSON(id, period, delay)
            local delay = delay or 0
            local  URLString   =    'http://192.168.0.4:8084/json.htm?type=graph&sensor=counter&range=month&idx=1008'
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses .. "_" .. period}).afterSec(delay)      
I also tried somthing to combine it in one script; but really don't know where to start there...
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Two JSON Calls

Post by waaren »

CronoS wrote: Friday 18 September 2020 6:59 Or is there something more necessary to make this script unique?
The callback string of the new script does also trigger the old script and you misplaced some quotes.

Try

Code: Select all

local httpResponses = "sun_monthTotal"

return {
    on      =   {   
                    timer           =   { "every 1 minutes" },
                    httpResponses   =   { httpResponses } 
                },

    logging =   {   
                    level           =   domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
                    marker          =   httpResponses   
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        sunDevice = dz.devices(1055)          -- Replace xxxx with ID of energyDevice you want to track
        monthTotal = dz.devices(1032)        -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
        -- ****************************** No changes required below this line *********************************************
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSONSun(id, period, delay)
            local delay = delay or 0
            local  URLString   =    'http://192.168.0.4:8084/json.htm?type=graph&sensor=counter&range=month&idx=1055'
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses .. "_" .. period}).afterSec(delay)                      
        end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
CronoS
Posts: 135
Joined: Wednesday 15 July 2015 23:40
Target OS: -
Domoticz version:
Contact:

Re: Two JSON Calls

Post by CronoS »

Hi Waaren

Thanks for you're answer; it seems to start and the other script seems to work.
But for some particular reason I do not go into the function. It seems like the "elseif item.ok" is not OK :) Any idea? (I want to calculate later on with this value, so I want to have it in a variable first. )

Code: Select all

local httpResponses = "sun_monthTotal"

return {
    on      =   {   
                    timer           =   { "every 1 minutes" },
                    httpResponses   =   { httpResponses } 
                },

    logging =   {   
                    level           =   domoticz.LOG_DEBUG, -- set to LOG_ERROR when script works as expected
                    marker          =   httpResponses  
                },
                
    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        usageDevice = dz.devices(1055)          -- Replace xxxx with ID of energyDevice you want to track
        monthTotal = dz.devices(1032)        -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
        -- ****************************** No changes required below this line *********************************************
        
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSON(id, period, delay)
            local delay = delay or 0
            local  URLString   =    'http://192.168.0.4:8084/json.htm?type=graph&sensor=counter&range=month&idx=1055'
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = httpResponses .. "_" .. period}).afterSec(delay)                      
        end
        
        local function calculateMonthTotal(rt)
            local monthTotal = 0
            logWrite("In function")
            local currentMonth = dz.time.rawDate:sub(1,7)
            for id, result in  ipairs(rt) do 
                if rt[id].d:sub(1,7) == currentMonth then
                    sun_monthTotal = monthTotal +  rt[id].v
                end
            end
            -- return monthTotal * 1000
            return sun_monthTotal
        end    
        
        logWrite(item.data)
        if not item.isHTTPResponse then
            triggerJSON(usageDevice.id, "month")
        elseif item.ok then                                      -- statusCode == 2xx
            logWrite("in elseif")
            monthValue = calculateMonthTotal(item.json.result)
            logWrite("Monthtotal: " .. monthValue)
            -- monthTotal.update(0,calculateMonthTotal(item.json.result))
        else
            logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")"  ,dz.LOG_ERROR)
            logWrite(item.data)
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Two JSON Calls

Post by waaren »


CronoS wrote:it seems to start and the other script seems to work.
But for some particular reason I do not go into the function. It seems like the "elseif item.ok" is not OK :) Any idea? (I want to calculate later on with this value, so I want to have it in a variable first. )
Please also share the loglines from both runs of this script. That will make it easier to help you.





Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
CronoS
Posts: 135
Joined: Wednesday 15 July 2015 23:40
Target OS: -
Domoticz version:
Contact:

Re: Two JSON Calls

Post by CronoS »

Don't know exactly what you mean with both runs of the script. I have this in my log:

Code: Select all

2020-09-18 14:19:00.411 Status: dzVents: Info: sun_monthTotal: ------ Start internal script: Totaal kWh gebruikt maand nieuw:, trigger: "every 1 minutes"
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: Processing device-adapter for SolarEdge Act. Vermogen (AC): kWh device adapter
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: Processing device-adapter for Totaal kWh gekocht maand: Custom sensor device adapter
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: nil
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: OpenURL: url = http://192.168.0.4:8084/json.htm?type=graph&sensor=counter&range=month&idx=1055
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: OpenURL: method = GET
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: OpenURL: post data = nil
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: OpenURL: headers = nil
2020-09-18 14:19:00.418 Status: dzVents: Debug: sun_monthTotal: OpenURL: callback = sun_monthTotal_month
2020-09-18 14:19:00.418 Status: dzVents: Info: sun_monthTotal: ------ Finished Totaal kWh gebruikt maand nieuw
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Two JSON Calls

Post by waaren »


CronoS wrote:Don't know exactly what you mean with both runs of the script.
The script executes every minute because of the time trigger. The second run is triggered by the callback because of the httpResponses trigger.

Please try again after changing

Code: Select all

httpResponses = { httpResponses } 
To

Code: Select all

httpResponses = { httpResponses .. '*'  },



Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
CronoS
Posts: 135
Joined: Wednesday 15 July 2015 23:40
Target OS: -
Domoticz version:
Contact:

Re: Two JSON Calls  [Solved]

Post by CronoS »

Thanks! That seems to work OK! Couldn't do it without you're help!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest