How create HA entities from a HTTP request

Hi,
I am new to Node-Red and I am not sure what is the best way to proceed. I want to request data from a power monitor. I can use the HTTP request and I get the data below. I am only interested on the payload data to create entities to be displayed in Home Assistant. There are 12 fields. 4 for each phase. Any help will be great.

1.- Power factor. The data shows 808 but the actual value is 0.808
2.- Voltage. The data shows 13162 but the actual value is 131.62 V
3.- Current. The data is in mA but I would like to see it in Amps
4.- Power in Watts, but I would like to see it in kW.

{"_msgid":"4c6cf7f.cc71008","payload":"{"data":[[808,13162,4640,492],[808,13167,2568,273],[808,13171,1544,164]]}","topic":"","statusCode":200,"headers":{"access-control-allow-origin":"*","content-type":"application/json; charset=utf-8","content-length":"73","connection":"close","x-node-red-request-node":"8da361f0"},"responseUrl":"http://10.0.3.113:8080/getdata","redirectList":}

I am using Node-RED Current version: 9.0.1 and Node-Red Companion on a Raspberry 3

Home Assistant can get the data itself using a RESTful Sensor. If you then want the attributes as separate sensors, you can use the Template Sensor. Regardless, it's probably a question that would be better answered on the Home Assistant forum.

It depends on where you want to do the work - i use HA but mainly for integrations and Display of front end items, i use NR for all the heavy lifting etc.

If you want to use NR for this then the easiest way would be to do the http requst as above, capture the return data as a JSobject and then reference each of the values through their paths - any required maths for unit conversions could then be easily performed in a function node.

Craig

1 Like

Thank you guys. I used Craig's advise. JS Object to a Split to Create Entities with conversions. It works very well. Thanks

No worries - it would be good for you to mark it as the solution (for others in future) and maybe also to post a working flow for others to learn from whn searching the forum

Craig

Hi Craig,
As a newbee I cannot upload flows, but here is the code. It could use improvements, for example if the HTTP server does not respond, I just do not know enough to do that.
Alberto

[{"id":"a97a7f8.e7da98","type":"tab","label":"Eyedro","disabled":false,"info":""},{"id":"c98ff2fc.e1fb7","type":"http request","z":"a97a7f8.e7da98","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://x.x.x.x:8080/getdata","tls":"","persist":false,"proxy":"","authType":"","x":90,"y":40,"wires":[["3227a94e.1580f6"]]},{"id":"3227a94e.1580f6","type":"json","z":"a97a7f8.e7da98","name":"","property":"payload","action":"","pretty":false,"x":250,"y":40,"wires":[["414c0399.24de2c"]],"info":"payload.data[0][0]"},{"id":"414c0399.24de2c","type":"split","z":"a97a7f8.e7da98","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":250,"y":300,"wires":[["2718e0b.701282","b4893977.24bcb8","233d00c1.17518","c188f42d.c25a68","dd2fe5b5.2c7368","d35beb2e.907118","5755a6b2.ec0898","24fdd142.c2bfbe","b2e2f975.d761d8","58def24c.0ed7dc","5b259799.8e5bd8","7054f9fe.1e8b98"]]},{"id":"2718e0b.701282","type":"ha-entity","z":"a97a7f8.e7da98","name":"PF1","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"PF1"},{"property":"device_class","value":"sesor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload[0][0]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":20,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"b4893977.24bcb8","type":"ha-entity","z":"a97a7f8.e7da98","name":"V1","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"V1"},{"property":"device_class","value":"sesor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload[0][1]/100","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":80,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"233d00c1.17518","type":"ha-entity","z":"a97a7f8.e7da98","name":"C1","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"C1"},{"property":"device_class","value":"sesor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"A"}],"state":"payload[0][2]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":140,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"c188f42d.c25a68","type":"ha-entity","z":"a97a7f8.e7da98","name":"P1","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"P1"},{"property":"device_class","value":"sesor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"kW"}],"state":"payload[0][3]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":200,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"dd2fe5b5.2c7368","type":"ha-entity","z":"a97a7f8.e7da98","name":"PF2","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"PF2"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload[1][0]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":260,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"d35beb2e.907118","type":"ha-entity","z":"a97a7f8.e7da98","name":"V2","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"V2"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload[1][1]/100","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":320,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"5755a6b2.ec0898","type":"ha-entity","z":"a97a7f8.e7da98","name":"C2","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"C2"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"A"}],"state":"payload[1][2]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":380,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"24fdd142.c2bfbe","type":"ha-entity","z":"a97a7f8.e7da98","name":"P2","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"P2"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"kW"}],"state":"payload[1][3]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":440,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"b2e2f975.d761d8","type":"ha-entity","z":"a97a7f8.e7da98","name":"PF3","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"PF3"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload[2][0]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":500,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"58def24c.0ed7dc","type":"ha-entity","z":"a97a7f8.e7da98","name":"C3","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"C3"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"A"}],"state":"payload[2][2]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":620,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"5b259799.8e5bd8","type":"ha-entity","z":"a97a7f8.e7da98","name":"P3","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"P3"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"kW"}],"state":"payload[2][3]/1000","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":680,"wires":[[]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"39f45e7f.e6bee2","type":"inject","z":"a97a7f8.e7da98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"300","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":160,"wires":[["c98ff2fc.e1fb7"]]},{"id":"7054f9fe.1e8b98","type":"ha-entity","z":"a97a7f8.e7da98","name":"V3","server":"bc748184.63e6e","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"V3"},{"property":"device_class","value":"sensor"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"V"}],"state":"payload[2][1]/100","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":450,"y":560,"wires":[["c5e72ea1.7da4e"]],"info":"states('sensor.PF1')\npayload[0][0]"},{"id":"c5e72ea1.7da4e","type":"debug","z":"a97a7f8.e7da98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":560,"wires":[]},{"id":"bc748184.63e6e","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Here is the flow:

And after it updates every 300 sec.

Well done - great work

If you want to catch HTTP errors - then put a switch node after the HTTP request - check what a valid response is - usually it is a 200 series code - if that is in the response - then pass the result onto your JSON node - if not - then loop out to a 2nd output - put a 10 sec delay and go back andmake the http request again

If you wanted to get fancy - you could put a counter in there as well - so if say after 6 loops you still did not have a valid response you could go off to an alert through email/Pushover/telegram etc

Craig

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