Set variable in automation to part of trigger.entity_id and then use variable throughout

I am in the process of adapting a heating boost automation that I found. I want to be able to use it for multiple different thermostats so that I can maintain just one version of the automation rather than having individual ones per thermostat.

I had hoped to set a variable to trigger.entity_id and perform some replacements to get just the roomname.

I’d then use this variable throughout the automation so I wouldn’t have hard-coded entity_IDs - in triggers as well as actions.

I’m hitting a couple of issues. For one, when I try to set a variable to
{{ trigger.entity_id }}
it gets replaced by in the automation YAML
{'[object Object]': None}

I guess instead of using a variable, I could use

{{ trigger.entity_id | replace('input_button.','',1) | replace('_boost_cancel','',1) | replace('_boost_start','',1) }}

throughout the script but that makes things pretty messy.

As a test, I hardcoded the variable and this seems to work throughout my automation’s actions. One place it doesn’t work is in the trigger section.
Using

triggers:
  - entity_id:
      - input_button.{{room_name}}_boost_start

Results in

Message malformed: Entity input_button.{{room_name}}_boost_start is neither a valid entity ID nor a valid UUID for dictionary value @ data['entity_id']

I could always split this automation into three separate ones to avoid this issue.

Any help with this would be gratefully appreciated.

Hello antlane,

You didn’t give me much YAML to work from, but I will guess this is your problem.

If you show more of this automation, I’ll be glad to help more.

Also, a blueprint is what you want, here. Not clear from your post if it’s the direction you took.

The example you posted appears to be a fragment of a State Trigger. You can’t use a template in a State Trigger’s entity_id option. That’s why the error message says:

Message malformed: Entity input_button.{{room_name}}_boost_start is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]

Perhaps you can use a Template Trigger.

Thanks for the replies so far.

I don’t think a Blueprint is what I’m looking for. I understand it would probably work but I would prefer not to have multiple automations created when I’m hoping that one will suffice. Obviously if my aim isn’t possible, a Blueprint is my next port of call.

Here’s the YAML for my automation (I should attribute https://www.reddit.com/r/homeassistant/comments/18dm9gc/finally_got_heating_control_set_up_with_boost/ which is where I got the original code that I’m now adapting to my own needs)

alias: Heating Boost Start/Cancel/Finish V1
description: ""
triggers:
  - entity_id:
      - input_button.antstudyheating_boost_start
    id: heatingBoostStart
    trigger: state
  - entity_id:
      - input_button.antstudyheating_boost_cancel
    id: heatingBoostCancel
    trigger: state
  - event_type: timer.finished
    event_data:
      entity_id: timer.antstudyheating_boost_time
    id: heatingBoostExpire
    trigger: event
conditions: []
actions:
  - action: notify.mobile_app_ants_iphone
    metadata: {}
    data:
      message: "{{ room_name }}"
  - action: notify.mobile_app_ants_iphone
    metadata: {}
    data:
      message: >-
        {{ trigger.entity_id | replace('input_button.','',1) |
        replace('_boost_cancel','',1) | replace('_boost_start','',1) }}
    enabled: false
  - choose:
      - conditions:
          - condition: trigger
            id:
              - heatingBoostStart
        sequence:
          - target:
              entity_id: input_boolean.{{room_name}}_boost
            data: {}
            action: input_boolean.turn_on
          - target:
              entity_id: switch.{{room_name}}_schedules
            data: {}
            action: switch.turn_off
          - data:
              preset_mode: none
            action: climate.set_preset_mode
            target:
              entity_id: climate.{{room_name}}
          - target:
              entity_id: climate.{{room_name}}
            data:
              temperature: "{{states('input_number. ' ~ room_name ~ '_boost_temperature')}}"
            action: climate.set_temperature
          - target:
              entity_id: timer.{{room_name}}_boost_time
            data: {}
            action: timer.start
          - target:
              entity_id: input_select.{{room_name}}_boost_time_options
            data:
              option: none
            action: input_select.select_option
      - conditions:
          - condition: trigger
            id:
              - heatingBoostCancel
              - heatingBoostExpire
        sequence:
          - target:
              entity_id:
                - input_boolean.{{room_name}}_boost
              device_id: []
              area_id: []
            data: {}
            action: input_boolean.turn_off
          - target:
              entity_id: switch.{{room_name}}_schedules
            data: {}
            action: switch.turn_off
            enabled: false
          - target:
              entity_id: switch.{{room_name}}_schedules
            data: {}
            action: switch.turn_on
          - action: timer.finish
            metadata: {}
            data: {}
            target:
              entity_id: timer.{{room_name}}_boost_time
variables:
  room_name: >-
    {% if trigger.id == "heatingBoostExpire" %}  {{trigger.event.data.entity_id
    | replace ('timer.','',1) | replace ('_boost_time','',1)}} {%- else -%}
    {{trigger.entity_id | replace('input_button.','',1) |
    replace('_boost_cancel','',1) | replace ('_boost_start','',1) }} {%- endif -%}
mode: single

Issue 1
As you’ll see at the bottom, I have hardcoded the variable room_name. I’d like to set it to
{{ trigger.entity_id }}
but it gets replaced with the text I posted above when I click save and reload the editor.

In fact, I’d need to set it to
{{ trigger.entity_id | replace('input_button.','',1) | replace('_boost_cancel','',1) | replace('_boost_start','',1) }}
but this produces a duplicated mapping key error - not too sure why (it does work for the disabled step which sends it to my mobile as a notification). If I keep just one replace, it doesn’t error.

Issue 2
I need to include {{room_name}} in the triggers at the top as I aim for this to work for mulitple rooms. I had a quick play with a state trigger but I wans’t able to get it to work. As the first two triggers are based on the button state changing which changes state to the date/time it was pressed, I’m not sure how I can evaluate something to true for the template unless I’m missing something.

Issue 3
I haven’t got around to trying to work out how to fix this as there are larger issues to resolve first but I need to find a way to integrate {{room_name}} into the following line
temperature: "{{states('input_number.antstudyheating_boost_temperature')}}"

Obviously, if these problems can’t be fixed, I’ll look at Blueprint option.

Thanks again.

I worked out how to fix my first issue by updating the variable code to:

room_name: >-
    {{ trigger.entity_id | replace('input_button.','',1) | replace('_boost_cancel','',1) | replace('_boost_start','',1) }} 

I originally entered that template in the GUI for the mobile notification so didn’t realise >- was required.

If I’m able to amend the post above, I’ll do so to remove this.

I’m on a roll. Fixed my third issue too. I also had to amend my variable code as the timer trigger returned the room data I needed in trigger.event.data.event_id