How to use helper value as part of entity name?

Hello,
I have 5 lights entities that I manage with an “input_number” helper which indicates the number of the light to turn on/off.
Instead of to have 5 if condition within an automation, is there a way to concatenate entity name with helper value? Something like python f print f"light.my_light_n{helper_value}"?
Thanks.

Yes. Templating along the lines of:

light.my_light_n{{ states('input_number.example') }}

If you share the automation you are using, we can give you an answer that addresses your actual situation instead of something general.

Depending on what your end goal is, you may also want to take a look at Template lights which can be used to create a single light entity to control your group of lights so you could use normal brightness controls instead of an Input number helper.

2 Likes

I simply created an input_number helper and an automation based on its changes to dimmer the light with the same id of the helper:

service: light.turn_on
entity_id: light.my_led{{ states('input_number.led_helper')|int }}
data_template:
  brightness: "{{states.light.my_led{{ states('input_number.led_helper')|int }}.attributes.brightness + 20}}"

but it give me the following error: not a valid value for dictionary value @ data[0][‘entity_id’]

I think the error is due to entity_id not being under one of the expected keys, either data or target.
Also, you can’t nest templates i.e. {{ {{ }} }}, data_template was deprecated about 2 years ago, and you will likely want to add an int(0) for your brightness to avoid none-type errors when the bulb is off.

From what you have provided, I would approach it as follows:

  - variables:
      current_light: "light.my_led{{ states('input_number.led_helper') }}"
  - service: light.turn_on
    target:
      entity_id: "{{ current_light }}"
    data:
      brightness: "{{ state_attr(current_light, 'brightness') | int(0) + 20 }}"

There may be better ways to do this, but you would need to share the actual automation instead of just a single piece.

I cannot share the automation since it was created via this blueprint project: Controller - IKEA E2001/E2002 STYRBAR Remote control | Awesome HA Blueprints.
I can post a screenshot:


The first action works.
While the second action generates the following error: Message malformed: extra keys not allowed @ data[‘action’][3][‘choose’][6][‘sequence’][0][‘choose’][0][‘sequence’][0][‘default’][2][‘choose’][0][‘sequence’][1][‘service’]

Ok, looking at the blueprint YAML it looks like an action sequence is the expect input.

Since both variables and service count as actions, you might be able to just add the hyphens in front of variables and service, like I had them in my example, so they are interpreted as a list of actions instead of just one action, but I’m not 100% sure that will work…

In this case working within the limitations of the blueprint, it might be best to just do something similar to what you were attempting with your earlier example:

service: light.turn_on
target:
  entity_id: "light.my_led{{ states('input_number.led_helper') }}"
data:
  brightness: |-
    {% set entity = 'light.my_led' ~ states('input_number.led_helper') | int | string %}
    {{ state_attr(entity, 'brightness') | int(0) + 20 }}

You can see the automation, check the tab ‘Automation Config’ in the trace to see the yaml.

Your script partially works:
with this piece of code:

service: light.turn_on
data: {}
target:
  entity_id: light.my_led_{{ states('input_number.led_helper') | int }}
enabled: true

I can turn on light (I had to change entity_id: "light.my_led{{ states('input_number.led_helper') }}" to entity_id: "light.my_led{{ states('input_number.led_helper')| int }}".
So, when I try to change brightness it doesn’t work.

Ok. This is my complete automation:

alias: Controller - IKEA E2001/E2002 STYRBAR Remote control
description: ""
use_blueprint:
  path: EPMatt/ikea_e2001_e2002.yaml
  input:
    integration: ZHA
    controller_device: xxxxxxxxxxxxxxx
    helper_last_controller_event: input_text.led_helper
    action_button_left_short:
      - service: input_number.decrement
        data: {}
        target:
          entity_id: input_number.led_helper
    action_button_right_short:
      - service: input_number.increment
        data: {}
        target:
          entity_id: input_number.led_helper
    action_button_up_short:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.my_led_{{ states('input_number.led_helper')|int }}
      - service: light.turn_on
        target:
          entity_id: light.my_led_{{ states('input_number.led_helper')|int }}
        data:
          brightness: >-
            {{ state_attr( 'light.my_led_'~states('input_number.led_helper'),'brightness')|int(0) + 20 }}
    action_button_down_short:
      - service: light.turn_on
        target:
          entity_id: light.my_led_{{ states('input_number.led_helper')|int }}
        data:
          brightness: >-
            {{ state_attr( 'light.my_led_'~states('input_number.led_helper'),'brightness') | int(0) - 20 }}

@Didgeridrew can you help me to solve this issue?

EDIT: I didn’t realize you edited your answer. Now it works! Thank you very much!