Page 1 of 1

httpResponses does not update CustomSensor

Posted: Saturday 01 May 2021 10:44
by besix
Hello
how to change the script below to download and update two sensors in domoticz?

Code: Select all

local scriptVar = 'Fronius'

return
{
    on =
    {
        timer =
        {'at daytime'},

        httpResponses =
        {
        scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = Fronius_Meter,
    },

    execute = function(dz, item)
        
        local IPFronius = dz.variables('UV_FroniusIP').value  -- string variables IPFronius

        if item.isTimer then
            dz.openURL(
            {
                url = 'http://'..IPFronius..'/solar_api/v1/GetPowerFlowRealtimeData.fcgi',
                callback = scriptVar,
            })
            return
        end

        if item.ok and item.isJSON then
            local rt = item.json.Body.Data
            dz.devices('Fronius_P_Grid').updateCustomSensor( (rt.P_Grid and rt.P_Grid.Value ) or 0) 
			dz.devices('Fronius_P_Load').updateCustomSensor( (rt.P_Load and rt.P_Load.Value ) or 0) 
          
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}
The used url return

Code: Select all

Body	
Data	
Inverters	
1	
DT	110
E_Day	63.10000228881836
E_Total	334137
E_Year	334137.3125
P	156
Site	
E_Day	63.10000228881836
E_Total	334137
E_Year	334137.3125
Meter_Location	"load"
Mode	"vague-meter"
P_Akku	null
P_Grid	172.27618674266148 
P_Load	-328.2761867426615 
P_PV	156
rel_Autonomy	47.520961403846734
rel_SelfConsumption	100
Version	"12"
Head	
RequestArguments	{}
Status	
Code	0
Reason	""
UserMessage	""
Timestamp	"2021-05-01T05:50:42+02:00"
Where is the mistake ?

Re: httpResponses does not update CustomSensor

Posted: Saturday 01 May 2021 19:02
by waaren
besix wrote: Saturday 01 May 2021 10:44 Where is the mistake ?
Hard to say based on what you show. Try this and if it does not work, share the log.

Code: Select all

local scriptVar = 'Fronius'

return
{
    on =
    {
        timer =
        {'at daytime'},

        httpResponses =
        {
        scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = Fronius_Meter,
    },

    execute = function(dz, item)
        
        local IPFronius = dz.variables('UV_FroniusIP').value  -- string variables IPFronius

        if item.isTimer then
            dz.openURL(
            {
                url = 'http://'..IPFronius..'/solar_api/v1/GetPowerFlowRealtimeData.fcgi',
                callback = scriptVar,
            })
            return
        end

        if item.ok and item.isJSON then
            local rt = item.json.Body.Data
            dz.utils.dumpTable(rt or item.json)
            dz.devices('Fronius_P_Grid').updateCustomSensor( rt.P_Grid or 0) 
            dz.devices('Fronius_P_Load').updateCustomSensor( (rt.P_Load or 0) 
          
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}

Re: httpResponses does not update CustomSensor

Posted: Saturday 01 May 2021 20:53
by besix
Thanks for showing interest
I corrected one line because I got an error

Code: Select all

 dz.devices('Fronius_P_Grid').updateCustomSensor( rt.P_Grid or 0) 
           dz.devices('Fronius_P_Load').updateCustomSensor( (rt.P_Load or 0) 
changed to

Code: Select all

 dz.devices('Fronius_P_Grid').updateCustomSensor( rt.P_Grid or 0) 
           dz.devices('Fronius_P_Load').updateCustomSensor( rt.P_Load or 0)
and changed the script to other E_Total and E_Day data
Below the log
Spoiler: show

Code: Select all

2021-05-01 20:37:00.629 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-05-01 20:38:00.411 Status: dzVents: Info: ------ Start internal script: Fronius licznik:, trigger: "every minute at 5:20-20:45"
2021-05-01 20:38:00.412 Status: dzVents: Debug: OpenURL: url = http://192.168.0.11/solar_api/v1/GetPowerFlowRealtimeData.fcgi
2021-05-01 20:38:00.412 Status: dzVents: Debug: OpenURL: method = GET
2021-05-01 20:38:00.412 Status: dzVents: Debug: OpenURL: post data = nil
2021-05-01 20:38:00.412 Status: dzVents: Debug: OpenURL: headers = nil
2021-05-01 20:38:00.412 Status: dzVents: Debug: OpenURL: callback = Fronius
2021-05-01 20:38:00.412 Status: dzVents: Info: ------ Finished Fronius licznik
2021-05-01 20:38:00.413 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-05-01 20:38:00.634 Status: dzVents: Info: Handling httpResponse-events for: "Fronius"
2021-05-01 20:38:00.634 Status: dzVents: Info: ------ Start internal script: Fronius licznik: HTTPResponse: "Fronius"
2021-05-01 20:38:00.640 Status: dzVents: > Version: 12
2021-05-01 20:38:00.640 Status: dzVents: > Site:
2021-05-01 20:38:00.640 Status: dzVents: > E_Total: 499997.03125
2021-05-01 20:38:00.640 Status: dzVents: > Mode: produce-only
2021-05-01 20:38:00.640 Status: dzVents: > Meter_Location: unknown
2021-05-01 20:38:00.640 Status: dzVents: > E_Year: 499997.90625
2021-05-01 20:38:00.640 Status: dzVents: > E_Day: 11596
2021-05-01 20:38:00.640 Status: dzVents: > Inverters:
2021-05-01 20:38:00.640 Status: dzVents: > 1:
2021-05-01 20:38:00.640 Status: dzVents: > DT: 122
2021-05-01 20:38:00.640 Status: dzVents: > E_Total: 499997.03125
2021-05-01 20:38:00.640 Status: dzVents: > P: 0
2021-05-01 20:38:00.640 Status: dzVents: > E_Year: 499997.90625
2021-05-01 20:38:00.640 Status: dzVents: > E_Day: 11596
2021-05-01 20:38:00.673 Status: dzVents: Debug: Processing device-adapter for Fronius_E_Total: Custom sensor device adapter
2021-05-01 20:38:00.675 Status: dzVents: Debug: Processing device-adapter for Fronius_E_Day: Custom sensor device adapter]
Your change gives more information but the sensors are updated to 0- zero
the entire script after changing
Spoiler: show

Code: Select all

local scriptVar = 'Fronius'

return
{
    on =
    {
        timer =
        {'every minute at 5:20-20:45'},

        httpResponses =
        {
        scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = Fronius_Meter,
    },

    execute = function(dz, item)
        
        local IPFronius = dz.variables('UV_FroniusIP').value  -- string variables IPFronius

        if item.isTimer then
            dz.openURL(
            {
                url = 'http://'..IPFronius..'/solar_api/v1/GetPowerFlowRealtimeData.fcgi',
                callback = scriptVar,
            })
            return
        end

        if item.ok and item.isJSON then
            local rt = item.json.Body.Data
            dz.utils.dumpTable(rt or item.json)
            dz.devices('Fronius_E_Total').updateCustomSensor( rt.E_Total or 0) 
            dz.devices('Fronius_E_Day').updateCustomSensor( rt.E_Day or 0) 
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}

Re: httpResponses does not update CustomSensor

Posted: Saturday 01 May 2021 21:22
by waaren
besix wrote: Saturday 01 May 2021 20:53 Your change gives more information but the sensors are updated to 0- zero
Can you try this

Code: Select all

return
{
    on =
    {
        timer =
        {'every minute at 5:20-21:45'},

        httpResponses =
        {
        scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = Fronius_Meter,
    },

    execute = function(dz, item)
        
        local IPFronius = dz.variables('UV_FroniusIP').value  -- string variables IPFronius

        if item.isTimer then
            dz.openURL(
            {
                url = 'http://'..IPFronius..'/solar_api/v1/GetPowerFlowRealtimeData.fcgi',
                callback = scriptVar,
            })
            return
        end

        if item.ok and item.isJSON then
            local rt = item.json.Body.Data
            if not(rt) then 
                dz.log('There is no Body.Data in the JSON', dz.LOG_ERROR)
                dz.utils.dumpTable(item.json) 
            else
                dz.devices('Fronius_E_Total').updateCustomSensor( rt[1].E_Total or 0) 
                dz.devices('Fronius_E_Day').updateCustomSensor( rt[1].E_Day or 0) 
            end
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}

Re: httpResponses does not update CustomSensor

Posted: Saturday 01 May 2021 21:44
by besix
Thanks. I'll check it tomorrow because the inverter is already sleeping

Re: httpResponses does not update CustomSensor

Posted: Sunday 02 May 2021 11:18
by besix
@waaren Your suggestion caused an error in the log

Code: Select all

2021-05-02 11:04:00.723 Error: dzVents: Error: (3.1.7) An error occurred when calling event handler Script #1
2021-05-02 11:04:00.723 Error: dzVents: Error: (3.1.7) ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:40: attempt to index a nil value (field '?')
But it brought me to a solution.
Thank you
A working script below

Code: Select all

local scriptVar = 'Fronius'
return
{
    on =
    {
        timer =
        {'every minute'},

        httpResponses =
        {
        scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = Fronius_Meter,
    },

    execute = function(dz, item)
        
        local IPFronius = dz.variables('UV_FroniusIP').value  -- string IPFronius variables

        if item.isTimer then
            dz.openURL(
            {
                url = 'http://'..IPFronius..'/solar_api/v1/GetPowerFlowRealtimeData.fcgi',
                callback = scriptVar,
            })
            return
        end

        if item.ok and item.isJSON then
            local rt = item.json.Body.Data.Site
            if not(rt) then 
                dz.log('There is no Body.Data in the JSON', dz.LOG_ERROR)
                dz.utils.dumpTable(item.json) 
            else
                dz.devices('Fronius_E_Total').updateCustomSensor( rt.E_Total or 0) 
                dz.devices('Fronius_E_Day').updateCustomSensor( rt.E_Day or 0)     
                dz.devices('Pobór_PGE').updateCustomSensor( rt.P_Grid or 0)
                dz.devices('Pobór_Dom').updateCustomSensor( rt.P_Load or 0)
                dz.devices('P_PV').updateCustomSensor( rt.P_PV or 0)
            end
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}