Reading JSON from ESPeasy
Posted: Friday 03 January 2020 21:33
Running Domoticz V4.10717 on a Synology:
Try to create a script for reading the state of a relay contected to an ESP8266 in ESPeasy.
I can read its state by a request in my browser: http://192.168.X.XX/json.
The result is:
I am not very familiar with dzvents so I tried to modify earlier script what is doing a similar, good job: This is my rebuild:
And this is what I see in the log of Domoticz:
Why is the result not as expected?
How can I read the value of Controller 1
And why is the log in ESP different from JSON?
This is the logfile in ESP, made by a simple rule:
Lots of questions for a beginner..
After that this scripts runs oke, Domoticz has to change some switches and the ActiveTimerPlan...
Any help is welcome.. Thanks in advance!
Try to create a script for reading the state of a relay contected to an ESP8266 in ESPeasy.
I can read its state by a request in my browser: http://192.168.X.XX/json.
The result is:
Code: Select all
{"System":{
"Build":20104,
"Git Build":"mega-20191119",
"System Libraries":"ESP82xx Core 2_5_2, NONOS SDK 2.2.1(cfd48f3), LWIP: 2.1.2 PUYA support",
"Plugins":46,
"Plugin description":" [Normal]",
"Local Time":"2020-01-03 20:33:17",
"Unit Number":11,
"Unit Name":"ESP_Alarm",
"Uptime":2119,
"Last Boot Cause":"Manual reboot",
"Reset Reason":"Hardware Watchdog",
"Load":15.40,
"Load LC":3346,
"CPU Eco Mode":"false",
"Heap Max Free Block":16720,
"Heap Fragmentation":15,
"Free RAM":19736
},
"WiFi":{
"Hostname":"ESP-Alarm-11",
"IP Config":"Static",
"IP Address":"192.168.X.X",
"IP Subnet":"255.255.255.0",
"Gateway":"192.168.X.X",
"STA MAC":"68:C6:3A:EA:24:73",
"DNS 1":"192.168.1.1",
"DNS 2":"(IP unset)",
"SSID":"XXXXXXX",
"BSSID":"C0:FF:D4:8D:30:E0",
"Channel":13,
"Connected msec":127151594,
"Last Disconnect Reason":1,
"Last Disconnect Reason str":"(1) Unspecified",
"Number Reconnects":0,
"Force WiFi B/G":"false",
"Restart WiFi Lost Conn":"false",
"Force WiFi No Sleep":"false",
"Periodical send Gratuitous ARP":"false",
"Connection Failure Threshold":0,
"RSSI":-43
},
"Sensors":[
{
"TaskValues": [
{"ValueNumber":1,
"Name":"State",
"NrDecimals":0,
"Value":0
}],
"DataAcquisition": [
{"Controller":1,
"IDX":831,
"Enabled":"true"
},
{"Controller":2,
"IDX":0,
"Enabled":"false"
},
{"Controller":3,
"IDX":0,
"Enabled":"false"
}],
"TaskInterval":0,
"Type":"Switch input - Switch",
"TaskName":"Alarmcontact",
"TaskDeviceNumber":1,
"TaskEnabled":"true",
"TaskNumber":1
}
],
"TTL":60000
}Code: Select all
local Enabled = 'true'
return {
active = true,
logging =
{
level = domoticz.LOG_DEBUG,
marker = "Alarmscript"
},
on = {
timer = { 'every minute' },
httpResponses = { 'Alarmstatus' } -- matches callback string below
},
execute = function(domoticz, item)
if (item.isTimer) then
domoticz.openURL({
url = 'http://192.168.X.XX/json',
method = 'GET',
callback = 'Alarmstatus'
})
elseif (item.isHTTPResponse) then
if (item.ok and item.isJSON) then
if tonumber(item.json.age) < 60 then
domoticz.devices('Alarmcontact').updateCustomSensor(item.json.Controller[1].Enabled)
end
else
domoticz.log('Error fetching Alarmstatus onduidelijk', domoticz.LOG_ERROR)
domoticz.log(item.data, domoticz.LOG_ERROR)
end
end
end
}Code: Select all
2020-01-03 20:43:00.356 Status: dzVents: Info: Alarmscript: ------ Start internal script: Alarm2:, trigger: every minute
2020-01-03 20:43:00.356 Status: dzVents: Debug: Alarmscript: OpenURL: url = http://192.168.X.XX/json
2020-01-03 20:43:00.356 Status: dzVents: Debug: Alarmscript: OpenURL: method = GET
2020-01-03 20:43:00.356 Status: dzVents: Debug: Alarmscript: OpenURL: post data = nil
2020-01-03 20:43:00.356 Status: dzVents: Debug: Alarmscript: OpenURL: headers = nil
2020-01-03 20:43:00.356 Status: dzVents: Debug: Alarmscript: OpenURL: callback = Alarmstatus
2020-01-03 20:43:00.356 Status: dzVents: Info: Alarmscript: ------ Finished Alarm2
2020-01-03 20:43:02.415 Status: dzVents: Info: Handling httpResponse-events for: "Alarmstatus
2020-01-03 20:43:02.416 Status: dzVents: Info: Alarmscript: ------ Start internal script: Alarm2: HTTPResponse: "Alarmstatus"
2020-01-03 20:43:02.418 Status: dzVents: Error (2.4.19): Alarmscript: An error occured when calling event handler Alarm2
2020-01-03 20:43:02.419 Status: dzVents: Error (2.4.19): Alarmscript: ...omoticz/var/scripts/dzVents/generated_scripts/Alarm2.lua:27: attempt to compare nil with number
2020-01-03 20:43:02.419 Status: dzVents: Info: Alarmscript: ------ Finished Alarm2 How can I read the value of Controller 1
And why is the log in ESP different from JSON?
This is the logfile in ESP, made by a simple rule:
Code: Select all
129812201: WD : Uptime 2164 ConnectFailures 0 FreeMem 20888 WiFiStatus 3
129834079: EVENT: Rules#Timer=1
129834098: ACT : SendToHTTP 192.168.x.x,XXXX,/json.htm?type=command¶m=udevice&idx=831&nvalue=0
129834111: Command: sendtohttp
129834143: ACT : SendToHTTP 192.168.x.x,XXXX,/json.htm?type=command¶m=udevice&idx=831&svalue=Off
129834155: Command: sendtohttp
129834183: ACT : timerSet,1,120
129834195: Command: timerset
129839080: EVENT: Clock#Time=Fri,21:18
129842201: WD : Uptime 2164 ConnectFailures 0 FreeMem 21176 WiFiStatus 3
After that this scripts runs oke, Domoticz has to change some switches and the ActiveTimerPlan...
Any help is welcome.. Thanks in advance!