Pass entity_ID through to "Call Service"

Ok I am VERY new to Node-Red, as well as HA. That being said. I have searched everywhere and either I am not using the correct keywords, or its just so basic the questions just never been ask!

I have 3 lights that I want to turn off if they are turned on for over 10 mins. How do I pass their entity_ID to the call service node? (yes I know I can just copy/paste the call service and have them work but I’m thinking of more complex automation down the road where I don’t want to have to use 20+ call service nodes.)

I use a get entities node when I am leaving the house to generate a list of every light.* device and then turn it off. (aside from the specific lights I have listed as an exception). Maybe something similar would work?

Are you looking to turn off the 3 lights if ANY are on for over 10 minutes? Or only turn them all off after 10 minutes if ALL of them are on?

And then heres the call service node that get entities feeds into

Want them to turn off if each has been on for 10 mins. So a separate timer for each. If I’m wording that correct.

I did get it to accept msg.payload.entity_id but the flow doesn’t actually turn the light off.

I’ll try {{payload.entity_id}} when I get home and post the export so u can see what I’ve done.

I dont quite get why you dont want to list the entity_id in the servce call node, if your gonna need a separate service call node for each light anyway.

But heres something that starts a 10 minute timer when the light turns on, if on for 10 min, it times out and turns the light off. If light is turned off by you, flow is stopped.

[{"id":"c26113a3.aa95","type":"tab","label":"Playground","disabled":false,"info":""},{"id":"bace28e2.85aca8","type":"server-state-changed","z":"c26113a3.aa95","name":"Light turned on?","server":"ab3e2c53.e3491","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.entryway_master","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":175,"y":288,"wires":[["e85afc33.03c4f"],[]]},{"id":"e85afc33.03c4f","type":"ha-wait-until","z":"c26113a3.aa95","name":"Wait 10 minute","server":"ab3e2c53.e3491","outputs":2,"entityId":"light.entryway_master","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":"10","timeoutType":"num","timeoutUnits":"minutes","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":432,"y":288,"wires":[[],["125afb75.e828b5"]]},{"id":"125afb75.e828b5","type":"api-call-service","z":"c26113a3.aa95","name":"Turn light off","server":"ab3e2c53.e3491","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"{{data.entity_id}}","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":665,"y":288,"wires":[[]]},{"id":"ab3e2c53.e3491","type":"server","name":"AvilaSmartHomeRpi","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

The Domain, Service and Entity Id fields all accept mustache templates, as it says in the help info for the node in the right panel. Here’s the doc on mustache templates from the documentation for node-red-contrib-home-assistant-websocket so you can see what all that allows you to do. Among other things, it allows you to set the value of entity ID dynamically from msg data, as nappyjim shows in the screenshot.

Also in HA entity ID is actually passed as service data for service calls. It’s such a common input for service calls it often gets promoted into its own input box but in reality its just another field in the service data. You can see this in dev tools in HA, anything you type in the entity ID input actually goes into the YAML box like so:

Node RED is just making service calls in HA so it works the same. You don’t actually have to type anything in that Entity Id box, you can just put it in the Data like this:

{
  "entity_id": "switch.my_light"
}

If you notice the Data input lets you switch to JSONata which lets you easily build expressions using message data, globals, functions, etc. So you can pretty much create the entity_id however you need to that way.

And if even that isn’t enough, as the doc on the node says you can override the values in those fields with passed in data. So if the msg passed into the node has a value in payload.data it will be merged with anything in the data field to create the data for the service call. So if you set a value to msg.payload.data.entity_id in the message you pass to the call service node, it will be used. Which means you can create entity_id out of literally anything you want, some other node, javascript in a function node, etc.

There’s a bunch of tips and tricks in the doc, including a whole section on the call service tips and tricks. Might want to give it a read if you haven’t yet, covers a lot of useful stuff

Thank you for the information. Ive read the help files multiple times and either im just that stupid or just not understanding it for some reason.

This is what im attempting to do:

using this:

[{"id":"6eeb52c2.cd714c","type":"server-state-changed","z":"7a5cf191.9decb","name":"Mud Room Light","server":"dc363a68.6ae0a8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.mud_room","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"10","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":100,"y":180,"wires":[["99080d1b.e64"],[]]},{"id":"99080d1b.e64","type":"api-call-service","z":"7a5cf191.9decb","name":"Turn Off","server":"dc363a68.6ae0a8","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"turn_off","entityId":"","data":"{ \"entity_id\": $join(payload.entity_id, \",\") }","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":400,"y":160,"wires":[["52f2b303.867c1c"]]},{"id":"dd14ac56.11fc","type":"server-state-changed","z":"7a5cf191.9decb","name":"Hall Light","server":"dc363a68.6ae0a8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.hall_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"10","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":80,"y":240,"wires":[["99080d1b.e64"],[]]},{"id":"bb5506f.1cd4df8","type":"server-state-changed","z":"7a5cf191.9decb","name":"Basement Step Lights","server":"dc363a68.6ae0a8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.basement_steps","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"10","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":120,"y":300,"wires":[["99080d1b.e64"],[]]},{"id":"52f2b303.867c1c","type":"debug","z":"7a5cf191.9decb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":520,"y":80,"wires":[]},{"id":"dc363a68.6ae0a8","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I get no errors now but it does not actually turn the light off. ad in debug info it shows the payload as being on…

The reason is:

There are actually 14 lights like that I want in the automation, i started with 3 to test and see if i can actually get it to work.

I have a wife and child like most people do, and they use these rooms indepentantly so i dont want the light in the hall to turn off at same time that the light in the closet does because they being used for different reasons at different times.

and writing a automation for each light seems like a waste of time and energy to me, but then again im starting to feel that trying to figure this system out is as well.

When imported your JSON it was just one debug node. You have to select all the nodes you want to export or choose export whole flow.

Edited the export to have all nodes.

Hm ok so first of all you’re looking in the wrong place for the entity ID. When you click on the events:state node and look at the doc in the right side panel you can see at the bottom it shows you that the output of that node has payload set to the state of the entity that changed. So nothing will be in payload.entity_id since payload isn’t an object, it’s a string in this case.

What you want is the data field in the output, that looks like this according to the doc:

{
  "entity_id": "light.kitchen",
  "old_state": {
    "entity_id": "light.kitchen",
    "state": "off",
    "attributes": {
      "friendly_name": "Kitchen Light",
      "icon": "mdi:light-switch"
    },
    "last_changed": "2019-12-29T04:49:50.230660+00:00",
    "last_updated": "2019-12-29T04:49:50.230660+00:00",
    "context": {
      "id": "3a639379992d430595e3e9c73fb349e1",
      "parent_id": null,
      "user_id": null
    }
  },
  "new_state": {
    "entity_id": "light.kitchen",
    "state": "on",
    "attributes": {
      "brightness": 118,
      "friendly_name": "Kitchen Light",
      "icon": "mdi:light-switch"
    },
    "last_changed": "2019-12-29T05:28:44.238349+00:00",
    "last_updated": "2019-12-29T05:28:44.238349+00:00",
    "context": {
      "id": "8a147f61a375489284ef7a7715a6a8f2",
      "parent_id": null,
      "user_id": null
    },
    "timeSinceChangedMs": 12
  }
}

So in the call service node when you want to refer to the id of the entity which triggered, you should refer to data.entity_id, not payload.entity_id.

I’d recommend changing the data field of your call service node from this

{ "entity_id": $join(payload.entity_id, ",") }

to this:

{ "entity_id": data.entity_id }

But I’m confused why the join is there at all. Each message that comes out of an events:state node refers to exactly one entity_id, there will never be multiple to join. So perhaps you can explain what you were trying to do that you thought would result in one message having multiple IDs? Since nothing will in your current set up.

I was trying multiple different commands from the help file to see what did what and apparently forgot to revert it back to the one that was suggested earlier. Since it was the only data entry I had tried that did not pull a error i was going down the road of trying to see what commands do what.

I’m a visual learner and typically help files get me in the right place but then I have to try a few different things to completely understand what the hell is actually going on.

I honestly never saw the panel on the right showing what is output from the node, this will help a ton thank you.

1 Like

I get it, but sometimes separating is best and honestly, using my flow I showed you a few days ago, you could have done that 14 times and be done, lol.

Im the same way you are, trying to condense as much as possible but then sometimes thatll cause more issue down the line when you want to change how just 1 light operates but then you realize you have that light tied into 12 others.

Thank you very much, your flow was working fine, and the way I had it set up. Was just maddening that carrying a simple X variable to spot was so difficult for me that i think it just became a matter of “Im going to figure this out if it kills me!”

Mike was spot on I was looking in the wrong spot the entire time, and once i changed it to data everything worked as intended.

Thank you both for the patience and help. Now I can move on to bigger and more complex flows.