Condition template for an entity based on a trigger

Hi all,

I would like to create a template condition based on a trigger to assess whether a script is running. I’d like some help with the formatting.

automation:
  - id: scenes_kitchen_start
    initial_state: 'true'
    alias: 'scenes kitchen start'
    trigger:
    - platform: state
      entity_id: input_boolean.dish, switch.espresso, input_boolean.oven
      to: 'on'
    condition:
    - condition: template
      value_template: :  {{ is_state(("script.{{ trigger.to_state.name }}_turn_on"), 'off')}}
1 Like
value_template: "{{ is_state('script.' ~ trigger.to_state.name ~ '_turn_on', 'off') }}"

Although I’m wondering if maybe you want trigger.to_state.object_id instead.

So you use tildes for templates in templates?

Not sure I understand your question. Tilde is a string concatenation operator.

Ah ok. Thanks.

Sorry,
Not got this. : -
“is_state(” ---- is asking if the state of this entity is “this possible state”
“(‘script.’” ---- so you are looking at ony “script.” objects
~ ---- looks like a string substution charachter to me so it then only matches with objects ending in -->
… “trigger.to_state.name” (though as I write this it looks more like -->)
where the “trigger.to_state.name” is -->
" ‘_turn_on’ " ???
but is " ‘off’ "
Nope, way over my head …

trigger.to_state.name is either the friendly_name, or the object_id (if it has no friendly_name), of the entity that caused the automation to trigger. So, e.g., 'Dish', or 'dish'.

Next the string concatenation operators (~) concatenate 'script.', the value of trigger.to_state.name, and '_turn_on'. So, e.g., 'script.dish_turn_on'.

Next the is_state() function is called to see if that entity is 'off'. So it’s checking if the script of interest is off, which means it’s not running.

1 Like

Thank you, works perfectly.

2 Likes

Okay, I now understand that and I’ve made a note of it but ask me in a week’s time and I’ll only have a vague recollection (so I refer to my notes).
This depends upon a close correlation between entity names (be it id or friendly) and the associated script. BUT if the aim is to get the trigger to provide the necessary ‘par’ ‘ts to be cons’ ‘idered’ then that is a minor irritation.
Thanks for the explanation, it really does help.

I used to use concatenate() in lotus 123 macros, but that was a while ago now ! : - )))

~ ensures that every item being concatenated is a string.
+ just adds things willy nilly without ensuring it’s the same type.