Pass msg.payload to set entity_state

In NodeRED I can’t seem to figure out how to pass a dynamic command to an entity. For instance, If I turn off a switch it produces a msg.payload = OFF. I’d like to pass that on to an entity to turn it off. If msg.payload = ON it would turn the entity ON.

There are a lot of HA\NodeRed 101 starter videos that show this being accomplished using a [Switch Node] but I’d like to generate content and pass that on without a switch. Any help would be appreciated…

You need to set the service of the payload in a function node or change node.

[{"id":"02e2a009cbc6ed5b","type":"server-state-changed","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.kokslampa","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":200,"y":660,"wires":[["172ad0e5b1849801"]]},{"id":"172ad0e5b1849801","type":"function","z":"ebaa69a9.649708","name":"","func":"msg.payload = { \"service\": \"turn_\" + msg.payload }\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":660,"wires":[["1b23f207f8e853d5"]]},{"id":"1b23f207f8e853d5","type":"api-call-service","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"switch","service":"","entityId":"switch.hallen","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":660,"y":660,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

That worked perfectly. Thank you!

You can also use mustache templates in the home assistant nodes. To use the value in msg.payload you would use {{payload}}. This is the same for all msg. parts.

To pass the variable on/off here you can use

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/call-service.html

8 Likes