How to catch "JSON parse error" Warning from HTTP request node?

Hello,

Have HTTP request nodes in flows which are requesting by HTTP from ESPEasy nodes. Not all requestable things from ESPEasy reply with proper formatted json yet.

So I wrote the functions which are processing the replys to also be able to work with the not-json-replys. This works all well.

But sadly each http request triggers a warning message which means the log on the SD-card driven raspberry gets written every 10 seconds.

I tried to catch the warnings with a catch-node, but this catch-node only eats errors, but no warnings.

What can I do to avoid the logs?

Thanks. :slight_smile:

Dont use the request node set to parse it. Check and clean up the string first, then parse the json

Why don’t you use MQTT to communicate? It works great with espeasy.

I don't like MQTT as it is an additional edge which may fail.
also I have some self-written php code which uses the same http-endpoints on the ESPs, the available php-mqtt is also not as relieable as I expect.

I tried to process the not-json-replies with json node too, was not sucessfull. is the json-node what you meant @dceejay ?

thank you both! :slight_smile:

Just to let you know, I've been using MQTT to communicate with my ESPEasy devices for several years and it is steady as a rock. I'm using a PiZero W to run the broker (mosquitto) and it has been up for

pi@mqttpizw:~ $ uptime
05:51:35 up 86 days, 13:34, 1 user, load average: 0.19, 0.12, 0.10

and I also have a python program running where NR sends MQTT requests to the python program and it replies and it also has been working wth no issues.

If you look thru the forum you will find many many people using MQTT to communicate because it is simple and reliable. So you might try revisiting it in the future.

the HTTP node in json mode is the same as http node in string mode plus the json node (or should be :slight_smile: - so as-is would hopefully fail in the same way... but instead of the json node you could use a function node to look for the "not quite right" data and handle it first (and maybe correct it to be json) before passing it on to the json node...

As we can't see examples of your data we can only guess at this point.

here is one flow of an automated dehumidifier.

[{"id":"7d7cb63f8c0a21c6","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"ec15b7d158cc3a11","type":"inject","z":"7d7cb63f8c0a21c6","name":"Entfeuchter check","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/get","payload":"","payloadType":"str","x":150,"y":120,"wires":[["c515f144a8b599f9"]]},{"id":"c515f144a8b599f9","type":"function","z":"7d7cb63f8c0a21c6","name":"MQTTtoHTTP-Entfeuchter","func":"var EntfeuchterWatertankFull = global.get('EntfeuchterWatertankFull') || 0\n\nif (msg.topic == 'smartswitch/Entfeuchter/power/set' && EntfeuchterWatertankFull != 1) {\n    if (msg.payload == '0'){\n        global.set('Entfeuchter', 0);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweroff';\n    } else if (msg.payload == '1'){\n        global.set('Entfeuchter', 1);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweron';\n    } else if (msg.payload == '2') {\n        global.set('Entfeuchter', 2);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweroff';\n    } else if (msg.payload == '3') {\n        global.set('Entfeuchter', 3);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweron';\n    } else {\n        return null;\n    }\n} else if (msg.topic == 'smartswitch/Entfeuchter/power/get') {\n    msg.url = 'http://192.168.10.242/json?view=sensorupdate&tasknr=4'\n} else if (msg.topic == 'smartswitch/Entfeuchter/setreachable') {\n    msg.url = 'http://192.168.10.242/control?cmd=event,setreachable'\n} else {\n    return null;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":40,"wires":[["29705d1aa1f432a2"]]},{"id":"29705d1aa1f432a2","type":"http request","z":"7d7cb63f8c0a21c6","name":"EntfeuchterSwitch","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"credentials":{},"x":490,"y":80,"wires":[["f6c769b1b9828175"]]},{"id":"f6c769b1b9828175","type":"function","z":"7d7cb63f8c0a21c6","name":"SetEntfeuchterState","func":"var Entfeuchter          = global.get('Entfeuchter') || 0;\n\nif (msg.topic == \"smartswitch/Entfeuchter/power/set\" && msg.payload == \"OK\" && msg.statusCode == 200){\n    // switch change state to on or off? check again ...\n    node.warn(\"check new state line 7\")\n    check1 = {}\n    check1.topic = 'smartswitch/Entfeuchter/power/get';\n    check1.payload = '';\n    return [check1, null]\n    //node.send(check1);\n    /*if (Entfeuchter == 0){\n        global.set('Entfeuchter', 1);\n    } else if (Entfeuchter == 2){\n        global.set('Entfeuchter', 3);\n    }*/\n/*} else if (msg.topic == \"smartswitch/Entfeuchter/power/set\" && msg.payload == \"OK\" && msg.statusCode == 200){\n    // switch change state to off\n    node.warn(\"check new state line 19\")\n    check2 = {}\n    check2.topic = 'smartswitch/Entfeuchter/power/get';\n    check2.payload = '';\n    return [check2, null]\n    //node.send(check2);\n    if (Entfeuchter == 1){\n        global.set('Entfeuchter', 0);\n    } else if (Entfeuchter == 3){\n        global.set('Entfeuchter', 2);\n    }*/ \n} else if (msg.topic == \"smartswitch/Entfeuchter/power/get\" && msg.payload.TaskValues[0].Value == 1) {\n    // status request shows switch is on\n    if (Entfeuchter != 1 && Entfeuchter != 3) {\n        global.set('Entfeuchter', 1);\n        /*rectify1 = {}\n        rectify1.topic = 'smartswitch/Entfeuchter/power/set';\n        rectify1.payload = '1';\n        node.send(rectify1);*/\n        //node.warn(\"line 39.\");\n    }\n    setreachable = {}\n    setreachable.topic = 'smartswitch/Entfeuchter/setreachable';\n    setreachable.payload = ''\n    node.send(setreachable);\n} else if (msg.topic == \"smartswitch/Entfeuchter/power/get\" && msg.payload.TaskValues[0].Value == 0) {\n    // status request shows switch is off\n    if (Entfeuchter != 0 && Entfeuchter != 2) {\n        global.set('Entfeuchter', 0);\n        /*rectify2 = {}\n        rectify2.topic = 'smartswitch/Entfeuchter/power/set';\n        rectify2.payload = '0';\n        node.send(rectify2);*/\n        //node.warn(\"line 49.\");\n    }\n    setreachable = {}\n    setreachable.topic = 'smartswitch/Entfeuchter/setreachable';\n    setreachable.payload = ''\n    node.send(setreachable);\n} else {\n    // status request shows unchanged, only announce current runningtime\n}\n\nEntfeuchter = global.get('Entfeuchter') || 0;\n\nvar updatephp = {\n    \"method\" : \"GET\",\n\t\"url\" : \"http://192.168.0.250/apcucollector.php?smartswitch=Entfeuchter&power=\" + Entfeuchter\n};\n\nreturn [null, updatephp]","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":120,"wires":[[],[]]},{"id":"55b6eb296b54f8ba","type":"inject","z":"7d7cb63f8c0a21c6","name":"Auto aus","props":[{"p":"payload","v":"0","vt":"str"},{"p":"topic","v":"smartswitch/Entfeuchter/power/set","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"0","payloadType":"str","x":120,"y":80,"wires":[["c515f144a8b599f9"]]},{"id":"8ce3aaaf5d33ff09","type":"inject","z":"7d7cb63f8c0a21c6","name":"Auto ein","props":[{"p":"payload","v":"1","vt":"str"},{"p":"topic","v":"smartswitch/Entfeuchter/power/set","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"1","payloadType":"str","x":120,"y":40,"wires":[["c515f144a8b599f9"]]},{"id":"84f0bb2ae0fbbdd2","type":"inject","z":"7d7cb63f8c0a21c6","name":"Man ein","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"3","payloadType":"str","x":270,"y":40,"wires":[["c515f144a8b599f9"]]},{"id":"bc4e910fdcf6a235","type":"inject","z":"7d7cb63f8c0a21c6","name":"Man aus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"2","payloadType":"str","x":280,"y":80,"wires":[["c515f144a8b599f9"]]}]

the reply of e.g. http://192.168.10.242/json?view=sensorupdate&tasknr=4 is:

{
"TaskValues": [
{"ValueNumber":1,
"Name":"State",
"NrDecimals":0,
"Value":1
},
{"ValueNumber":2,
"Name":"Unused",
"NrDecimals":0,
"Value":0
},
{"ValueNumber":3,
"Name":"UpdTS",
"NrDecimals":0,
"Value":33873
}],
"TTL":1000,
"TaskEnabled":"true",
"TaskNumber":4
}

or e.g. another valid json-response from another espeasy device (http://192.168.10.243/control?cmd=status,GPIO,2) is:

{
"log": "",
"plugin": 1,
"pin": 2,
"mode": "output",
"state": 1
}

while the reply of all commands starting with "control?cmd=event,..." is only plain "OK" without any json (btw. I also opened a feature request to espeasy to have this fixed: possibility to reply JSON for rule events · Issue #4019 · letscontrolit/ESPEasy · GitHub )

on espeasy I created rules which are quite powerful. So on the espeasy device I measure runtime, etc. which is pushed periodically to my php scripts, which then offers the data combined to a php webpage where I can interact via http directly with the ESP devices.

node red has mostly "only" logic for 10-secondly ask PV inverter (by http) with smartmeter (same by http) for too much sunpower which needs to be distributed over different units in the house (all of them are also http). before getting into node-red I had this all completely in python-scripts, which were much more complicated and unhandy for debugging.

e.g. with the http://192.168.10.242/control?cmd=event,setreachable command I tell every 30 seconds the affected espeasy device that it still has network connectivity. as soon as the esp-device does not get these requests it does power off as at this stage I maybe do not want to consume electricity power as the sun is shaded.

for each of the http://192.168.10.242/control?cmd=event,setreachable calls I get the json parse error.
also the custom event,poweron or event,poweroff are spitting the json parse error. :frowning:

so, it sounds like it is more or less placing a function between the HTTP request node and the follow-up function?

Thanks.

so if if it's just the OK you are trying to remove then a simple switch node to test for that and then the json node to do the parsing of wha's left. - (and /or another path to handle the OK if you need to).

awesome, thanks. works and keeps it simple until espeasy implemented. :slight_smile:
the OK's were already handled in my following function, so I can still use them as they are and just need to switch the OK not to be json-parsed and passed as it is to the existing function. :+1:

result is now this:

[{"id":"cd9f7fccc388b516","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"bb1ad122603257b6","type":"function","z":"cd9f7fccc388b516","name":"MQTTtoHTTP-Entfeuchter","func":"var EntfeuchterWatertankFull = global.get('EntfeuchterWatertankFull') || 0\n\nif (msg.topic == 'smartswitch/Entfeuchter/power/set' && EntfeuchterWatertankFull != 1) {\n    if (msg.payload == '0'){\n        global.set('Entfeuchter', 0);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweroff';\n    } else if (msg.payload == '1'){\n        global.set('Entfeuchter', 1);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweron';\n    } else if (msg.payload == '2') {\n        global.set('Entfeuchter', 2);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweroff';\n    } else if (msg.payload == '3') {\n        global.set('Entfeuchter', 3);\n        msg.url = 'http://192.168.10.242/control?cmd=event,poweron';\n    } else {\n        return null;\n    }\n} else if (msg.topic == 'smartswitch/Entfeuchter/power/get') {\n    msg.url = 'http://192.168.10.242/json?view=sensorupdate&tasknr=4'\n} else if (msg.topic == 'smartswitch/Entfeuchter/setreachable') {\n    msg.url = 'http://192.168.10.242/control?cmd=event,setreachable'\n} else {\n    return null;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":80,"wires":[["5262666b54564c9c"]]},{"id":"5262666b54564c9c","type":"http request","z":"cd9f7fccc388b516","name":"EntfeuchterSwitch","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":490,"y":120,"wires":[["139c456fb8211155"]]},{"id":"139c456fb8211155","type":"switch","z":"cd9f7fccc388b516","name":"OKonly","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OK","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":660,"y":120,"wires":[["68b014f3cd0f9a32"],["0bdf117510427966"]]},{"id":"0bdf117510427966","type":"json","z":"cd9f7fccc388b516","name":"","property":"payload","action":"","pretty":false,"x":450,"y":160,"wires":[["68b014f3cd0f9a32"]]},{"id":"68b014f3cd0f9a32","type":"function","z":"cd9f7fccc388b516","name":"SetEntfeuchterState","func":"var Entfeuchter          = global.get('Entfeuchter') || 0;\n\nif (msg.topic == \"smartswitch/Entfeuchter/power/set\" && msg.payload == \"OK\" && msg.statusCode == 200){\n    // switch change state to on or off? check again ...\n    node.warn(\"check new state line 7\")\n    check1 = {}\n    check1.topic = 'smartswitch/Entfeuchter/power/get';\n    check1.payload = '';\n    return [check1, null]\n    //node.send(check1);\n    /*if (Entfeuchter == 0){\n        global.set('Entfeuchter', 1);\n    } else if (Entfeuchter == 2){\n        global.set('Entfeuchter', 3);\n    }*/\n/*} else if (msg.topic == \"smartswitch/Entfeuchter/power/set\" && msg.payload == \"OK\" && msg.statusCode == 200){\n    // switch change state to off\n    node.warn(\"check new state line 19\")\n    check2 = {}\n    check2.topic = 'smartswitch/Entfeuchter/power/get';\n    check2.payload = '';\n    return [check2, null]\n    //node.send(check2);\n    if (Entfeuchter == 1){\n        global.set('Entfeuchter', 0);\n    } else if (Entfeuchter == 3){\n        global.set('Entfeuchter', 2);\n    }*/ \n} else if (msg.topic == \"smartswitch/Entfeuchter/power/get\" && msg.payload.TaskValues[0].Value == 1) {\n    // status request shows switch is on\n    if (Entfeuchter != 1 && Entfeuchter != 3) {\n        global.set('Entfeuchter', 1);\n        /*rectify1 = {}\n        rectify1.topic = 'smartswitch/Entfeuchter/power/set';\n        rectify1.payload = '1';\n        node.send(rectify1);*/\n        //node.warn(\"line 39.\");\n    }\n    setreachable = {}\n    setreachable.topic = 'smartswitch/Entfeuchter/setreachable';\n    setreachable.payload = ''\n    node.send(setreachable);\n} else if (msg.topic == \"smartswitch/Entfeuchter/power/get\" && msg.payload.TaskValues[0].Value == 0) {\n    // status request shows switch is off\n    if (Entfeuchter != 0 && Entfeuchter != 2) {\n        global.set('Entfeuchter', 0);\n        /*rectify2 = {}\n        rectify2.topic = 'smartswitch/Entfeuchter/power/set';\n        rectify2.payload = '0';\n        node.send(rectify2);*/\n        //node.warn(\"line 49.\");\n    }\n    setreachable = {}\n    setreachable.topic = 'smartswitch/Entfeuchter/setreachable';\n    setreachable.payload = ''\n    node.send(setreachable);\n} else {\n    // status request shows unchanged, only announce current runningtime\n}\n\nEntfeuchter = global.get('Entfeuchter') || 0;\n\nvar updatephp = {\n    \"method\" : \"GET\",\n\t\"url\" : \"http://192.168.0.250/apcucollector.php?smartswitch=Entfeuchter&power=\" + Entfeuchter\n};\n\nreturn [null, updatephp]","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":160,"wires":[[],[]]},{"id":"e37c103d2b755c50","type":"inject","z":"cd9f7fccc388b516","name":"Entfeuchter check","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/get","payload":"","payloadType":"str","x":150,"y":160,"wires":[["bb1ad122603257b6"]]},{"id":"9f0e8e64be6cad0b","type":"inject","z":"cd9f7fccc388b516","name":"Auto aus","props":[{"p":"payload","v":"0","vt":"str"},{"p":"topic","v":"smartswitch/Entfeuchter/power/set","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"0","payloadType":"str","x":120,"y":120,"wires":[["bb1ad122603257b6"]]},{"id":"3f88905ed231f291","type":"inject","z":"cd9f7fccc388b516","name":"Auto ein","props":[{"p":"payload","v":"1","vt":"str"},{"p":"topic","v":"smartswitch/Entfeuchter/power/set","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"1","payloadType":"str","x":120,"y":80,"wires":[["bb1ad122603257b6"]]},{"id":"e2313e2a3eb38f53","type":"inject","z":"cd9f7fccc388b516","name":"Man ein","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"3","payloadType":"str","x":270,"y":80,"wires":[["bb1ad122603257b6"]]},{"id":"ca18f4d4bc2aeae0","type":"inject","z":"cd9f7fccc388b516","name":"Man aus","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"smartswitch/Entfeuchter/power/set","payload":"2","payloadType":"str","x":280,"y":120,"wires":[["bb1ad122603257b6"]]}]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.