Page 1 of 1

Problem with json-httpresponse (Solved)

Posted: Saturday 27 January 2024 21:16
by Backbone
I get data from a web thermostat which I can access from an IP address.

In DZVENTS the code script checks 3 variables.
Only the code line with the temperature is working fine and can be shown in a dummy entry.
The code line with Humidity is not working and do not show any data change in the dummy entry.
The relay code line I commented for a later troubleshooting.
Could there be a simple reason I overlook?
The IDX value is correct.
Error log says only: Error: dzVents: Error: (3.1.8) ESP32: No status provided. Humidity not set

Thanks, Paco


Code: Select all

{

"type":"TH&Relay",
"name":"Thermostaat",
"version":"4",
"relay_error":"",
"th_error":"",
"mac":"",
"relay_high": "0",
"relay_high_level": "1",
"celsius":"17.90",
"fahrenheit":"64.22",
"humidity":"58.00",
"wifi_ssi": "-46"
}

Code: Select all

local scriptVar = 'ESP32'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        local temperature = dz.devices(32)       -- set to id of your virtual temperature sensor
        local humidity = dz.devices(64)          -- set to id of your virtual humidity sensor
        -- local relay = dz.devices(34)             -- set to id of your virtual text device
        if item.isTimer then
            dz.openURL(
            {
                url = '192.168.1.19',
                callback = scriptVar .. '_getJSON',
            })
        elseif item.isHTTPResponse and item.isJSON then
            temperature.updateTemperature(item.json.celsius)
            humidity.updateHumidity(item.json.humidity)
            -- relay.updateText(item.json.relay_high)
            
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, domoticz.LOG_DEBUG)
        end
    end
}

Re: json-httpresponse inconsistent

Posted: Saturday 27 January 2024 21:32
by Tukker
To get some insight of what you receive try this:

Code: Select all

domoticz.utils.dumpTable(item.json)
and watch the log.

Re: json-httpresponse inconsistent

Posted: Saturday 27 January 2024 22:26
by Backbone
If I understanding it correctly by adding it to the script this is the result.

2024-01-27 22:22:00.805 Error: dzVents: Error: (3.1.8) ESP32: No status provided. Humidity not set
2024-01-27 22:22:00.805 Error: dzVents: Error: (3.1.8) ESP32: An error occurred when calling event handler EPS32 T+H+R
2024-01-27 22:22:00.805 Error: dzVents: Error: (3.1.8) ESP32: ...moticz/scripts/dzVents/generated_scripts/EPS32 T+H+R.lua:37: attempt to index a nil value (global 'domoticz')

Paco

Re: json-httpresponse inconsistent

Posted: Sunday 28 January 2024 0:31
by Tukker
I suggest like this:

Code: Select all

-- temperature.updateTemperature(item.json.celsius)
-- humidity.updateHumidity(item.json.humidity)
-- relay.updateText(item.json.relay_high)
domoticz.utils.dumpTable(item.json)
Precede every line that causes trouble with comment signs like --

The script then gives you just the dump and you will be able to find how to select the dat you need.
You will see a dump like this one from wheatherlive

Code: Select all

2024-01-28 00:33:00.203 Status: dzVents: Info: Weerlive DEBUG data: ------ Start internal script: 01_Weerlive: HTTPResponse: "WeerLiveDataRetrieved"
2024-01-28 00:33:00.204 Status: dzVents: > liveweer:
2024-01-28 00:33:00.204 Status: dzVents: > 1:
2024-01-28 00:33:00.204 Status: dzVents: > plaats: Delden
2024-01-28 00:33:00.204 Status: dzVents: > d2windkmh: 11
2024-01-28 00:33:00.204 Status: dzVents: > d0windkmh: 15
2024-01-28 00:33:00.204 Status: dzVents: > d0windrgr: 180
2024-01-28 00:33:00.204 Status: dzVents: > lv: 85
2024-01-28 00:33:00.204 Status: dzVents: > image: wolkennacht
2024-01-28 00:33:00.204 Status: dzVents: > d1windr: Z
2024-01-28 00:33:00.204 Status: dzVents: > d1windk: 2
2024-01-28 00:33:00.204 Status: dzVents: > winds: 2
2024-01-28 00:33:00.204 Status: dzVents: > d1tmin: 0
2024-01-28 00:33:00.204 Status: dzVents: > luchtd: 1034.3
2024-01-28 00:33:00.204 Status: dzVents: > timestamp: 1706397801
2024-01-28 00:33:00.204 Status: dzVents: > d2zon: 40
2024-01-28 00:33:00.204 Status: dzVents: > windrgr: 180
2024-01-28 00:33:00.204 Status: dzVents: > alarm: 0
2024-01-28 00:33:00.204 Status: dzVents: > windr: Zuid
2024-01-28 00:33:00.204 Status: dzVents: > d2neerslag: 10
2024-01-28 00:33:00.204 Status: dzVents: > d2windrgr: 180
2024-01-28 00:33:00.204 Status: dzVents: > d1windrgr: 180
2024-01-28 00:33:00.204 Status: dzVents: > d1tmax: 8
2024-01-28 00:33:00.204 Status: dzVents: > sup: 08:17
2024-01-28 00:33:00.204 Status: dzVents: > temp: 1.1
2024-01-28 00:33:00.204 Status: dzVents: > d1windkmh: 11
2024-01-28 00:33:00.204 Status: dzVents: > zicht: 16
2024-01-28 00:33:00.204 Status: dzVents: > samenv: Licht bewolkt
2024-01-28 00:33:00.204 Status: dzVents: > ldmmhg: 776
2024-01-28 00:33:00.204 Status: dzVents: > d2windk: 2
2024-01-28 00:33:00.204 Status: dzVents: > d2tmin: 5
2024-01-28 00:33:00.204 Status: dzVents: > d2weer: halfbewolkt
2024-01-28 00:33:00.204 Status: dzVents: > d2tmax: 12
2024-01-28 00:33:00.204 Status: dzVents: > d0tmax: 9
2024-01-28 00:33:00.204 Status: dzVents: > d1zon: 50
2024-01-28 00:33:00.204 Status: dzVents: > d1neerslag: 0
2024-01-28 00:33:00.204 Status: dzVents: > d0windknp: 8
2024-01-28 00:33:00.204 Status: dzVents: > d2windr: Z
2024-01-28 00:33:00.204 Status: dzVents: > d2windknp: 6
2024-01-28 00:33:00.204 Status: dzVents: > d1windms: 3
2024-01-28 00:33:00.204 Status: dzVents: > windkmh: 10.8
2024-01-28 00:33:00.204 Status: dzVents: > dauwp: -1.1
2024-01-28 00:33:00.204 Status: dzVents: > d0windk: 3
2024-01-28 00:33:00.204 Status: dzVents: > d1windknp: 6
2024-01-28 00:33:00.204 Status: dzVents: > windms: 3
2024-01-28 00:33:00.204 Status: dzVents: > time: 28-01-2024 00:23
2024-01-28 00:33:00.204 Status: dzVents: > verw: Droog en morgen overdag vrij zonnig
2024-01-28 00:33:00.204 Status: dzVents: > d2windms: 3
2024-01-28 00:33:00.204 Status: dzVents: > d1weer: halfbewolkt
2024-01-28 00:33:00.204 Status: dzVents: > d0tmin: 1
2024-01-28 00:33:00.204 Status: dzVents: > d0zon: 55
2024-01-28 00:33:00.204 Status: dzVents: > alarmtxt:
2024-01-28 00:33:00.205 Status: dzVents: > d0neerslag: 0
2024-01-28 00:33:00.205 Status: dzVents: > d0windr: Z
2024-01-28 00:33:00.205 Status: dzVents: > d0weer: halfbewolkt
2024-01-28 00:33:00.205 Status: dzVents: > d0windms: 4
2024-01-28 00:33:00.205 Status: dzVents: > gtemp: -2.2
2024-01-28 00:33:00.205 Status: dzVents: > windk: 5.8
2024-01-28 00:33:00.205 Status: dzVents: > sunder: 17:12
2024-01-28 00:33:00.205 Status: dzVents: Debug: Weerlive DEBUG data: nil
In this case we receive the temperature like this:

Code: Select all

domoticz.log('Outside temperature is   ' .. item.json.liveweer[1].temp,domoticz.LOG_DEBUG)
The dump informs you how to get the data you want to have.
And don't forget the domoticz.devices magic words like:

Code: Select all

domoticz.devices('NAME_OFYOUR_DEVICE').updateTemperature(item.json.liveweer[1].temp)
or

Code: Select all

domoticz.devices(32).updateTemperature(item.json.liveweer[1].temp)
Oh, last remark: replace my 'domoticz' with your 'dz' because that is how you tell your script to understand.

Re: json-httpresponse inconsistent

Posted: Sunday 28 January 2024 9:52
by Kedi
You have to send status as well. See https://www.domoticz.com/wiki/DzVents:_ ... ity_sensor
And you have to calculate the status.

Re: json-httpresponse inconsistent

Posted: Sunday 28 January 2024 9:57
by habahabahaba
Just rename 'domoticz' to 'dz' (as you call it in "execute = function(dz, item)" at line 37 and 41 )

And then catch other errors :)

Re: json-httpresponse inconsistent

Posted: Sunday 28 January 2024 13:17
by Backbone
OK thanks for all suggestions I got one step further.
Domoticz is replaced with dz where needed.

Now the log spits the correct data for the script as HvdW suggested.

2024-01-28 13:07:00.434 Status: dzVents: Info: ESP32: ------ Start internal script: EPS32 T+H+R:, trigger: "every minute"
2024-01-28 13:07:00.434 Status: dzVents: Debug: ESP32: OpenURL: url = 192.168.1.19
2024-01-28 13:07:00.434 Status: dzVents: Debug: ESP32: OpenURL: method = GET
2024-01-28 13:07:00.434 Status: dzVents: Debug: ESP32: OpenURL: post data = nil
2024-01-28 13:07:00.434 Status: dzVents: Debug: ESP32: OpenURL: headers = nil
2024-01-28 13:07:00.434 Status: dzVents: Debug: ESP32: OpenURL: callback = ESP32_getJSON
2024-01-28 13:07:00.434 Status: dzVents: Info: ESP32: ------ Finished EPS32 T+H+R
2024-01-28 13:07:00.436 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2024-01-28 13:07:00.877 Status: dzVents: Info: Handling httpResponse-events for: "ESP32_getJSON"
2024-01-28 13:07:00.877 Status: dzVents: Info: ESP32: ------ Start internal script: EPS32 T+H+R: HTTPResponse: "ESP32_getJSON"
2024-01-28 13:07:00.878 Status: dzVents: > mac: 24:6F:2
2024-01-28 13:07:00.879 Status: dzVents: > humidity: 51.00
2024-01-28 13:07:00.879 Status: dzVents: > th_error:
2024-01-28 13:07:00.879 Status: dzVents: > relay_high_level: 1
2024-01-28 13:07:00.879 Status: dzVents: > type: TH&Relay
2024-01-28 13:07:00.879 Status: dzVents: > relay_error:
2024-01-28 13:07:00.879 Status: dzVents: > wifi_ssi: -53
2024-01-28 13:07:00.879 Status: dzVents: > fahrenheit: 62.60
2024-01-28 13:07:00.879 Status: dzVents: > celsius: 17.00
2024-01-28 13:07:00.879 Status: dzVents: > version: 4
2024-01-28 13:07:00.879 Status: dzVents: > relay_high: 0
2024-01-28 13:07:00.879 Status: dzVents: > name: Thermostaat
2024-01-28 13:07:00.879 Status: dzVents: Info: ESP32: ------ Finished EPS32 T+H+R
2024-01-28 13:07:04.659 Status: EventSystem: reset all events...
2024-01-28 13:07:04.661 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/EPS32 T+H+R.lua

Then I tried to understand the logic that comes from HvdW his example but it throws an error.
Code used now

Code: Select all

local scriptVar = 'ESP32'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        --local temperature = dz.devices(32)       -- set to id of your virtual temperature sensor
        --local humidity = dz.devices(64)          -- set to id of your virtual humidity sensor
        --local relay = dz.devices(34)             -- set to id of your virtual text device
        if item.isTimer then
            dz.openURL(
            {
                url = '192.168.1.19',
                callback = scriptVar .. '_getJSON',
            })
    elseif item.isHTTPResponse and item.isJSON then
               domoticz.devices(32).updateTemperature(item.json.celsius)
            -- temperature.updateTemperature(item.json.celsius)
            -- humidity.updateHumidity(item.json.humidity)
            -- relay.updateText(item.json.relay_high)
            dz.utils.dumpTable(item.json)
            
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end
    end
}
It throws this as log...
2024-01-28 13:10:00.450 Status: dzVents: Info: ESP32: ------ Start internal script: EPS32 T+H+R:, trigger: "every minute"
2024-01-28 13:10:00.451 Status: dzVents: Debug: ESP32: OpenURL: url = 192.168.1.19
2024-01-28 13:10:00.451 Status: dzVents: Debug: ESP32: OpenURL: method = GET
2024-01-28 13:10:00.451 Status: dzVents: Debug: ESP32: OpenURL: post data = nil
2024-01-28 13:10:00.451 Status: dzVents: Debug: ESP32: OpenURL: headers = nil
2024-01-28 13:10:00.451 Status: dzVents: Debug: ESP32: OpenURL: callback = ESP32_getJSON
2024-01-28 13:10:00.451 Status: dzVents: Info: ESP32: ------ Finished EPS32 T+H+R
2024-01-28 13:10:00.453 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2024-01-28 13:10:00.863 Status: dzVents: Info: Handling httpResponse-events for: "ESP32_getJSON"
2024-01-28 13:10:00.864 Status: dzVents: Info: ESP32: ------ Start internal script: EPS32 T+H+R: HTTPResponse: "ESP32_getJSON"
2024-01-28 13:10:00.865 Status: dzVents: Info: ESP32: ------ Finished EPS32 T+H+R
2024-01-28 13:10:00.865 Error: dzVents: Error: (3.1.8) ESP32: An error occurred when calling event handler EPS32 T+H+R
2024-01-28 13:10:00.865 Error: dzVents: Error: (3.1.8) ESP32: ...moticz/scripts/dzVents/generated_scripts/EPS32 T+H+R.lua:34: attempt to index a nil value (global 'domoticz')

Line 34 = domoticz.devices(32).updateTemperature(item.json.celsius)

Paco

Re: json-httpresponse inconsistent

Posted: Sunday 28 January 2024 14:08
by habahabahaba
The same error - DOMOTICZ
You had to use DZ!!

Re: json-httpresponse inconsistent

Posted: Sunday 28 January 2024 15:24
by Backbone
Overlooked Domoticz while copying code...>me bad

The temperature codeline runs fine as it did before in the old code but the humidity again errors in the same way.
2024-01-28 15:20:00.891 Error: dzVents: Error: (3.1.8) ESP32: No status provided. Humidity not set

Code: Select all

local scriptVar = 'ESP32'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)
        --local temperature = dz.devices(32)       -- set to id of your virtual temperature sensor
        --local humidity = dz.devices(64)          -- set to id of your virtual humidity sensor
        --local relay = dz.devices(34)             -- set to id of your virtual text device
        if item.isTimer then
            dz.openURL(
            {
                url = '192.168.1.19',
                callback = scriptVar .. '_getJSON',
            })
    elseif item.isHTTPResponse and item.isJSON then
               dz.devices(32).updateTemperature(item.json.celsius)
               dz.devices(64).updateHumidity(item.json.humidity)
            -- humidity.updateHumidity(item.json.humidity)
            -- relay.updateText(item.json.relay_high)
            dz.utils.dumpTable(item.json)
            
        else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end
    end
}

error log (see last line)
2024-01-28 15:20:00.245 Status: dzVents: Info: ESP32: ------ Start internal script: EPS32 T+H+R:, trigger: "every minute"
2024-01-28 15:20:00.245 Status: dzVents: Debug: ESP32: OpenURL: url = 192.168.1.19
2024-01-28 15:20:00.245 Status: dzVents: Debug: ESP32: OpenURL: method = GET
2024-01-28 15:20:00.245 Status: dzVents: Debug: ESP32: OpenURL: post data = nil
2024-01-28 15:20:00.245 Status: dzVents: Debug: ESP32: OpenURL: headers = nil
2024-01-28 15:20:00.245 Status: dzVents: Debug: ESP32: OpenURL: callback = ESP32_getJSON
2024-01-28 15:20:00.245 Status: dzVents: Info: ESP32: ------ Finished EPS32 T+H+R
2024-01-28 15:20:00.247 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2024-01-28 15:20:00.886 Status: dzVents: Info: Handling httpResponse-events for: "ESP32_getJSON"
2024-01-28 15:20:00.886 Status: dzVents: Info: ESP32: ------ Start internal script: EPS32 T+H+R: HTTPResponse: "ESP32_getJSON"
2024-01-28 15:20:00.889 Status: dzVents: Debug: ESP32: Processing device-adapter for temp_kamer: Temperature device adapter
2024-01-28 15:20:00.891 Status: dzVents: Debug: ESP32: Processing device-adapter for humidity: Humidty device adapter
2024-01-28 15:20:00.891 Status: dzVents: > th_error:
2024-01-28 15:20:00.891 Status: dzVents: > relay_error:
2024-01-28 15:20:00.891 Status: dzVents: > relay_high_level: 1
2024-01-28 15:20:00.891 Status: dzVents: > name: Thermostaat
2024-01-28 15:20:00.891 Status: dzVents: > humidity: 53.00
2024-01-28 15:20:00.891 Status: dzVents: > wifi_ssi: -54
2024-01-28 15:20:00.891 Status: dzVents: > relay_high: 0
2024-01-28 15:20:00.891 Status: dzVents: > celsius: 17.10
2024-01-28 15:20:00.891 Status: dzVents: > fahrenheit: 62.78
2024-01-28 15:20:00.891 Status: dzVents: > type: TH&Relay
2024-01-28 15:20:00.891 Status: dzVents: > version: 4
2024-01-28 15:20:00.891 Status: dzVents: > mac: 24:6F:28:CB:73:EC
2024-01-28 15:20:00.892 Status: dzVents: Info: ESP32: ------ Finished EPS32 T+H+R
2024-01-28 15:20:00.892 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2024-01-28 15:20:00.891 Error: dzVents: Error: (3.1.8) ESP32: No status provided. Humidity not set

Re: Problem with json-httpresponse

Posted: Sunday 28 January 2024 16:36
by Backbone
The IDX 64 for humidity ESP32 is correct...
Knipsel3.PNG
Knipsel3.PNG (18.03 KiB) Viewed 1629 times
Paco

Re: Problem with json-httpresponse

Posted: Sunday 28 January 2024 16:57
by habahabahaba
Did you read this: "You have to send status as well. See https://www.domoticz.com/wiki/DzVents:_ ... ity_sensor
And you have to calculate the status." by Kedi ?

When you are updatin humidity you have to use
updateHumidity(humidity, status)
Update humidity. status can be domoticz.HUM_NORMAL, HUM_COMFORTABLE, HUM_DRY, HUM_WET. Supports command options.

try
dz.devices(64).updateHumidity(item.json.humidity, status) or
dz.devices(64).update(item.json.humidity, status)

where status can be 0, 1, 2, 3
0 - normal
1 - comfort
2 - dry
3 - wet

Re: Problem with json-httpresponse

Posted: Sunday 28 January 2024 19:04
by Kedi
Could be something simple as from my nodered flows.

Code: Select all

if (HUM < 30) {
    HUM_STAT=2;
} else if((HUM >= 30) && (HUM < 45)) {
    HUM_STAT=0;
} else if((HUM >= 45) && (HUM <= 70)) {
    HUM_STAT=1;
} else if(HUM >= 70) {
    HUM_STAT=3;
}

Re: Problem with json-httpresponse

Posted: Sunday 28 January 2024 20:40
by Backbone
OK, guys thank you for leading me to the working solution.
For my general display purpose the Humidity Virtual device needed only a value to be reported from the thermostat data. That is all I needed.
But what I learned now from you is this device needs a value and a calculated status number to show the humidity in WORDS in the same virtual device. If one is missing it throws an error.
It was my assumption the status was coming automatically from the input data numbers.

I will add the status logic Kedi proposed.
Learned something new today.

Thank you all for the time taken, appreciate it very much.
Was bugging me a few months.

Paco