Node Red Variable for entity

Hi guys,

I am not sure if I am missing something, I am trying to create a simple variable that I can then refer to in my node red for entities. That way I can recycle and also change the entity name in one place only. I can’t see how you would do this?

I was hoping you could just declare a (Flow?) variable in a function node; something like this:

var var_k_play = "media_player.kitchen_2"
flow.set("var_k_play",media_player.kitchen_2);
return var_k_play;

and then call that variable where you have to enter the entity anywhere like this:
{{"var_k_play"}}

but that clearly does not work… any advice would be appreciated. I’ve trawled youtube and posts here but have not found an answer.


I’m no expert on flow variables but I think you would need a flow.get function before your media player node to pass the message in and possibly change your flow.set with:

Flow.Set Function

var var_k_play = "light.study_light";
flow.set("var_k_play",var_k_play);
msg.var_k_play=var_k_play;
return msg;

Flow. Get Function

msg.var_k_play=flow.get('var_k_play');
return msg;

Then change your Entity_ID in the media node to

{{var_k_play}}

as this is referencing msg.var_k_play and not the variable you defined in the function -I think

So something like this:-

[{"id":"ccc8b9d6.541e58","type":"function","z":"3f648309.933bc4","name":"Flow.get","func":"msg.var_k_play=flow.get('var_k_play');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":720,"wires":[["3da8b512.e8e1b2"]]},{"id":"57339543.09db04","type":"debug","z":"3f648309.933bc4","name":"function get","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":720,"wires":[]},{"id":"3da8b512.e8e1b2","type":"api-call-service","z":"3f648309.933bc4","name":"","server":"57ce5691.fd6e48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"{{var_k_play}}","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":450,"y":720,"wires":[["57339543.09db04"]]},{"id":"f570eb8b.5ea69","type":"inject","z":"3f648309.933bc4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":720,"wires":[["ccc8b9d6.541e58"]]},{"id":"83ad44f6.9f769","type":"function","z":"3f648309.933bc4","name":"Flow.set","func":"var var_k_play = \"light.study_light\";\nflow.set(\"var_k_play\",var_k_play);\nmsg.var_k_play=var_k_play;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":660,"wires":[["dbfca78.1985ed8"]]},{"id":"1966a243.5fca16","type":"debug","z":"3f648309.933bc4","name":"function set","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":660,"wires":[]},{"id":"dbfca78.1985ed8","type":"api-call-service","z":"3f648309.933bc4","name":"","server":"57ce5691.fd6e48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"{{var_k_play}}","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":430,"y":660,"wires":[["1966a243.5fca16"]]},{"id":"1573a07c.d01dc","type":"inject","z":"3f648309.933bc4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":660,"wires":[["83ad44f6.9f769"]]},{"id":"57ce5691.fd6e48","type":"server","name":"Home Assistant","addon":true}]

image

[{"id":"e52598b1.342858","type":"inject","z":"ffbd7f06.4a014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":2736,"wires":[["bde8aaf6.f1cf88"]]},{"id":"bde8aaf6.f1cf88","type":"change","z":"ffbd7f06.4a014","name":"","rules":[{"t":"set","p":"var_k_play","pt":"flow","to":"light.study_light","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":2736,"wires":[[]]},{"id":"49533223.f3051c","type":"inject","z":"ffbd7f06.4a014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":172,"y":2784,"wires":[["c5b45262.59ada"]]},{"id":"c5b45262.59ada","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"{{flow.var_k_play}}","data":"{\"volume_level\": 0.30}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":402,"y":2784,"wires":[[]]}]
2 Likes

Yes, a much simpler way to do it. Thanks :slight_smile:

Wow thanks guys, I am going to test those. I managed to get it working by using a ‘current state’ node like below, but I prefer your ideas. I will report back! Thanks again.

This is so much better as I can then recycle the code for different rooms etc, build sub flows and only have to change entity_id’s in one place! Also if I change hardware etc… I am sure everyone does this and just doesn’t talk about it as I could not find anything much on this on the web.


@Kermit, your’s works a dream. I’ve implemented it on all my flows now. Not quite figured out how I could pass variables to subflows, but for now I am 100% sorted. Thanks again!