Dynamic Entity ID through Templating

I’ve struggled with this overnight and not sure if anyone has had similar and managed to work it out, or whether I may need to open a new feature request.

Are we able to use templating in the name of an entity_id?

entity_id: calendar.{{now().year}}_fia_formula_one_world_championship_race_calendar

The official calendar is created each year and I add it when available for the upcoming season. However I need to go through and update all references to it under entity_id.

The template editor shows the output as I wish it to be displayed, however it doesn’t pass the configuration validation.

If I could template an entity_id name, then it would remove my need to go through and change the automation dependencies each year when the calendar is updated to the latest version!

Any help/information is appreciated.

It depends on whether you’re using it in a service call, trigger, condition, etc.

For example, this is valid:

  action:
  - service: light.turn_off
    data_template:
      entity_id: '{{ trigger.payload }}'
1 Like

Yep. Something like this also works. (light_name in this case being a parameter passed to a script)

- service: light.turn_off
  data_template:
    entity_id: "light.{{ light_name }}"

You’ll find useful information relevant to this here.

In case you missed it, the key part to take away from those above posts (that isn’t being made explicit :wink:) is the “data_template:” line in the action service call.

1 Like

It’s a bit old but maybe that helps

now().year = integer, you need a string for the entity_id name

{% set Jahr =  now().year | string() %}
{{state_attr('calendar.schichtplan_'+Jahr, 'message') }}

OR

{{ state_attr('calendar.schichtplan_'+now().year | string(), 'message') == 'Nacht-Rufbereitschaft (getauscht)' }}