Make variables usable in automations with condition?

Hi everybody,

since variables have been introduced, I try to implement them where ever I can. Unfortunately, the seem not to work when using the condition.

Example

This will not work

automation:
  - alias: "[Hei] AZ Morgens"
    trigger:
      - platform: time
        at: input_datetime.az_morgens
    variables:
      temperatur: input_number.az_morgens
      heizung: climate.arbeitszimmer_heizung_climate
      fenster: input_boolean.az_fenster
    condition:
      - condition: state
        entity_id: "{{ fenster }}"
        state: "off"
    action:
      - service: climate.set_temperature
        data_template:
          entity_id: "{{ heizung }}"
          temperature: "{{ states(temperatur) }}"

This will work

automation:
  - alias: "[Hei] AZ Morgens"
    trigger:
      - platform: time
        at: input_datetime.az_morgens
    variables:
      temperatur: input_number.az_morgens
      heizung: climate.arbeitszimmer_heizung_climate
      fenster: input_boolean.az_fenster
    condition:
      - condition: state
        entity_id: input_boolean.az_fenster # changed this part only
        state: "off"
    action:
      - service: climate.set_temperature
        data_template:
          entity_id: "{{ heizung }}"
          temperature: "{{ states(temperatur) }}"

Is this something that will be considered in future releases? Or should I just stick to hard coding the input_boolean part of the code? I have this automation at least twice per room (morning, evening, perhaps night and/or afternoon).

Thank you in advance for your feedback :slight_smile:

You can’t template the entity_if of a state condition as far as I understand. Try using a template condition instead, something like this:

- condition: template
  value_template: "{{ is_state(fenster, 'off') }}"

I think this should work.

1 Like

Thank you so much. It seems to work (= does not throw an error when testing the configuration). This makes things much simpler, just copy & paste the code template and change the variable :slight_smile:

While I agree that it works with a template condition, I think it should work with entity_id as a variable as well. So I support this as a feature request.

1 Like