[Code] Get Entities node to Call Service node

Hi all,

I’m new to HA and Node-RED. I was struggling to link a Get Entities node to a Call Service node which I wanted to use to power off all lights which are on when setting the alarm. I thought that a blanket turn off all lights was taking too long and might be issuing the command to all lights whether the lights were on or off.

I have used the following code in a function between the two nodes. I can give a more detailed example if required but hopefully the function code should be enough.

let entities = [];

msg.payload.forEach((entity, index) => {
    entities.push(entity.entity_id);
});

let newMsg = { payload: "" };
newMsg.payload = { data: { entity_id: entities.join(",")} };
return newMsg;

Kev

Definitely, an excellent and powerful way to do it. Here’s an option to do it using no custom code.

[{"id":"bac602d7.34282","type":"inject","z":"2802ff38.60d4c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":1408,"wires":[["74c0aa4e.973254"]]},{"id":"5a256649.5ced08","type":"debug","z":"2802ff38.60d4c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":738,"y":1408,"wires":[]},{"id":"ad4f2ae5.d6d918","type":"template","z":"2802ff38.60d4c","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.entity_id}}","output":"str","x":476,"y":1408,"wires":[["fad11fab.b7d29"]]},{"id":"fad11fab.b7d29","type":"join","z":"2802ff38.60d4c","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":",","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":610,"y":1408,"wires":[["5a256649.5ced08"]]},{"id":"74c0aa4e.973254","type":"ha-get-entities","z":"2802ff38.60d4c","server":"ef067c6f.620e6","name":"","rules":[{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":310,"y":1408,"wires":[["ad4f2ae5.d6d918"]]}]
1 Like

That’s great to see. Thanks @Kermit

Any advantage to using either method? Does a custom function add overhead?

If they are equal I would say your method is nicer as it’s more readable :smiley:

If anything the second example might have a little more overhead but it’s going to be really close either way.

I’m new to node-red and wanted to test using this instead of yaml.

But why is it necessary to do all that!?
Why can’t I just use something like msg.payload.entity_id inside the entity_id field in the call service node?!
I’m running a “get entities” to find lights that is on and that one is also set to split the output, it’s then connected to the “call service”, my debug between sees that it’s sending the messages based on the condition inside “get entities”, but for no reason as the message (object) is unusable in the “call service” node… or am I missing something?

It does not feel like it’s that user friendly to be honest…

well, that was easy :slight_smile:
Thanks!

But why is the reason to do as OP did?
Is it to avoid doing multiple calls to home-assistant and only supply a comma separated list of entities?

Because OP is still learning :grin:

We are all still learning :slight_smile:
Trying to wrap my head on all of this, fun but frustrating :smile:

My preferred method is doing more work inside Node-RED and only sending one service call, the way @JumpMaster did in their original post, instead of sending multiple service calls to Home Assistant this is especially good when dealing with a large number of calls, one or two probably not a big deal but you might see a slower reaction time when sending 10+.