Create and update state of entity ID with node red

hi, i would like to create an Entity Id by using an output of a node and update the state.

I run into several problems, home assistant recognizes it as a non-preristannt Entity Id.
for example i would like to create a temperature and door state sensor like this.

Is there a better solution for this?

[{"id":"82bd9549.5f8058","type":"ha-api","z":"de6e1665.9dc598","name":"","server":"a5cd5f92.621be","debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","location":"payload","locationType":"msg","responseType":"json","x":830,"y":1260,"wires":[["f00f27f3.1d45a8"]]},{"id":"790eda44.b92654","type":"function","z":"de6e1665.9dc598","name":"garage_down","func":"msg.entity_id = `binary_sensor.garage_down`;\n\nmsg.payload = {\n    data: {\n        state : msg.payload\n    }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":1260,"wires":[["82bd9549.5f8058","3db592b9.5cf36e"]]},{"id":"f00f27f3.1d45a8","type":"debug","z":"de6e1665.9dc598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":1260,"wires":[]},{"id":"3db592b9.5cf36e","type":"debug","z":"de6e1665.9dc598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":1220,"wires":[]},{"id":"30443fe1.d844e8","type":"ha-api","z":"de6e1665.9dc598","name":"","server":"a5cd5f92.621be","debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","location":"payload","locationType":"msg","responseType":"json","x":830,"y":1100,"wires":[["46ad8eea.16259"]]},{"id":"a856f89b.1a","type":"function","z":"de6e1665.9dc598","name":"garage_up","func":"msg.entity_id = `input_boolean.garage_up`;\n\nmsg.payload = {\n    data: {\n        state : msg.payload\n    }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":618,"y":1100,"wires":[["30443fe1.d844e8","d24234ee.2b3ec"]]},{"id":"46ad8eea.16259","type":"debug","z":"de6e1665.9dc598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":1100,"wires":[]},{"id":"d24234ee.2b3ec","type":"debug","z":"de6e1665.9dc598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":1060,"wires":[]},{"id":"77c0d874.cfdea8","type":"ha-api","z":"de6e1665.9dc598","name":"","server":"a5cd5f92.621be","debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","location":"payload","locationType":"msg","responseType":"json","x":830,"y":1440,"wires":[["c15e6e0a.f962"]]},{"id":"8fe86059.18c678","type":"function","z":"de6e1665.9dc598","name":"garage_temp","func":"msg.entity_id = `sensor.garage_temp`;\n\nmsg.payload = {\n    data: {\n        state : msg.payload\n    }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":618,"y":1440,"wires":[["77c0d874.cfdea8","841aa2f1.0a571"]]},{"id":"c15e6e0a.f962","type":"debug","z":"de6e1665.9dc598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":1440,"wires":[]},{"id":"841aa2f1.0a571","type":"debug","z":"de6e1665.9dc598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":1400,"wires":[]},{"id":"9b7bad45.fd71e8","type":"ha-api","z":"de6e1665.9dc598","name":"","server":"a5cd5f92.621be","debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","location":"payload","locationType":"msg","responseType":"json","x":830,"y":1620,"wires":[["603bfb02.067424"]]},{"id":"5f7e6ac7.ac789c","type":"function","z":"de6e1665.9dc598","name":"garage_huminity","func":"msg.entity_id = `sensor.garage_huminity`;\n\nmsg.payload = {\n    data: {\n        state : msg.payload\n    }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":1620,"wires":[["9b7bad45.fd71e8","5b77130f.41df2c"]]},{"id":"603bfb02.067424","type":"debug","z":"de6e1665.9dc598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":1620,"wires":[]},{"id":"5b77130f.41df2c","type":"debug","z":"de6e1665.9dc598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":1580,"wires":[]},{"id":"9ebbfa92.00f4a8","type":"inject","z":"de6e1665.9dc598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":190,"y":1140,"wires":[["a856f89b.1a"]]},{"id":"1051e430.f19a6c","type":"inject","z":"de6e1665.9dc598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":190,"y":1180,"wires":[["a856f89b.1a"]]},{"id":"de71bdac.ef467","type":"inject","z":"de6e1665.9dc598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":190,"y":1300,"wires":[["790eda44.b92654"]]},{"id":"28151012.dc56b8","type":"inject","z":"de6e1665.9dc598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":190,"y":1340,"wires":[["790eda44.b92654"]]},{"id":"7e32f8e.844b108","type":"inject","z":"de6e1665.9dc598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12.2","payloadType":"num","x":190,"y":1480,"wires":[["8fe86059.18c678"]]},{"id":"1d1a55ff.c05b7a","type":"inject","z":"de6e1665.9dc598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50.5","payloadType":"num","x":190,"y":1660,"wires":[["5f7e6ac7.ac789c"]]},{"id":"a5cd5f92.621be","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

What does “create entity id” mean? IMO entity id cannot exist without entity.

Anyway from what I can see on your screenshot, you want to create sensors rather than entity ids, isn’t it? If so, instead of calling API use sensor node. Connect it to your nodes which provide particular values - job done.
BTW, if you can read humidity or temperature or door state from somewhere… (likely sensors) why you need to create sensors again in HA. But maybe I’m missing some piece of information.

1 Like

Instead of using the API (this creates only temporary entities that are gone after a reload/restart, simply use the entity node. This allows you to create a persistant entitiy based on the values from the messages.

entity

Creates an entity in Home Assistant which can be manipulated from this node.

node-red-contrib-home-assistant-websocket : ha-entity
1 Like

thank you so much!

This node I did not know yet, that makes everything even easier.
Thank you very very much so it is very simple! :slight_smile: