Dynamically Update Entity ID State - need help

I have some code where I’m able to dynamically build an entity id and store it in a variable. Is there a way to then update the state of this entity by passing in this variable? It seems that call service would allow me to change the state, but I cannot pass the entity id to this node. I have to select the entity id manually in the node…

I’m not a programmer just know some scripts… so I figured there may be a way to do this in a function but i’m at a loss…

Of course you can pass the entity_id dynamically. Put it msg.payload.data.entity_id.
This if you want to use the previous msg to carry the entity id, you can use flow context (variables) to expand in the data field, just tick the render templates, and use the mustache syntax

Before you do that make sure your flow is entity proof, so basically don’t send the payload without entity id or you’re gonna start seeing funny things happening.

1 Like

awesome, thanks. Took a lot of trial and error to figure out I had to put my string into an object (then had to figure out how to do that), but got it working. Awesome, appreciate it.

I would like to see an example of that code. :slight_smile:

It basically starts with how I label my entities. I have to use a consistent format. For example:

these specific lights start with ‘lt’, this tells me it’s for the litetouch lighting system. This light template reviews the status of an input_boolean. To turn the light on and off I call a script and pass data to the script. I’m telling the script which light keypad I’m using and which button.

  friendly_name: Study
  value_template: "{{ is_state('input_boolean.lt_19_3_boolean', 'on') }}"
    service: script.light_onoff
      keypad: 19
      button: 3
      entity_id: "light.lt_19_3"
    service: script.light_onoff          
      keypad: 19
      button: 3
      entity_id: "light.lt_19_3"

Input boolean

  name: Study
  initial: off

Script - the script creates a custom event that I listen for in node red. It tells node red which keypad and button were pushed.

      - event: litetouch_onoff
          keypad: "{{ keypad }}"
          button: "{{ button }}"
          entity_id: "{{ entity_id }}"

In node red I listen for the light touch event that gets broadcast when a button is pushed.

I then have a function that parses the payload from the node red event to determine the button and keypad used. Then formats the string that gets passed to the lighting controller. This code also sets the entity boolean name to be used later.

hex = function(num) {
    return pad(num.toString(16).toUpperCase());
pad = function(num) {
    num = num.toString();
    while (num.length < 3) {
      num = "0" + num;
    return num;

msg.keypad = msg.payload.event.keypad;
msg.button = msg.payload.event.button;
entity_id = msg.payload.event.entity_id;

msg.button = parseInt(msg.button,10);

msg.payload = hex(msg.keypad) + (msg.button -1);
msg.ledkeypad = pad(msg.keypad);

msg.ledstates = "R,CGLES," + msg.ledkeypad +"\r";

entityid = msg.payload.entity_id;
msg.entityboolean = entity_id.replace('light','input_boolean') + "_boolean";

msg.payload = "R,CTGSW,"+ msg.payload +"\r";
return msg;

The controller returns the result, I then parse this data to determine if the light was turned on or not. As I still have the entity boolean id that I generated above (see msg.entityboolean = …). I then have a function that puts the entityboolean into a data object which can be passed back to HA.

msg.payload = {
   "data": {
         "entity_id": msg.entityboolean
return msg;

then I call the service intput boolean to set it to true by turning it on. Now the status of my light is on.

1 Like

Thanks :slight_smile:

I’ll try to make it work, I’m not much of a programmer but I think I understand .

Good luck, let me know if you need any help. I’m not a programmer either, almost all of my code above is pieced together from google searches lol and from a nodejs module that I was originally using via command line but realized I could do it in node red. The benefit of having it in node red was so that I could use hassio, but it also turned out to be much faster that running through the command line and calling nodejs.

1 Like