Using variables in condition template automation

Hello i’m trying to use variables inside automation without success.
what’s wrong?

alias: Gestione Temperatura e Umidità Camera Grande con variabili
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.clima_big_inside_temperature
      - sensor.clima_big_humidity
      - sensor.monit_casa_e_fotovoltaico_channel_1_power
action:
  - variables:
      temperature_target: "{{ state_attr('climate.clima_big', 'temperature') }}"
      humidity_target: "{{ states('sensor.clima_big_target_humidity') | float }}"
      temp: "{{ states('sensor.clima_big_inside_temperature') | float }}"
      humidity: "{{ states('sensor.clima_big_humidity') | float }}"
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.monit_casa_e_fotovoltaico_channel_1_power') |
              float > 100 }}
        sequence:
          - service: climate.turn_off
            target:
              entity_id: climate.clima_big
            data: {}
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.monit_casa_e_fotovoltaico_channel_1_power') |
              float <= 100 }}
          - condition: template
            value_template: >
              {{ temp > temperature_target or (humidity not in ['unknown',
              'unavailable', 'None'] and humidity | float > humidity_target) }}
        sequence:
          - service: climate.turn_on
            target:
              entity_id: climate.clima_big
            data: {}
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.monit_casa_e_fotovoltaico_channel_1_power') |
              float <= 100 }}
          - condition: template
            value_template: >
              {{ temp <= temperature_target and (humidity in ['unknown',
              'unavailable', 'None'] or humidity | float <= humidity_target) }}
        sequence:
          - service: climate.turn_off
            target:
              entity_id: climate.clima_big
            data: {}
  - service: homeassistant.update_entity
    target:
      entity_id:
        - sensor.clima_big_inside_temperature
        - sensor.clima_big_humidity
        - sensor.monit_casa_e_fotovoltaico_channel_1_power
    data: {}

You supply float with a default value.

  1. If float cannot convert the supplied value to a number, it will report the default value.
  2. If no default value is provided and it cannot convert the value, the template fails with an error.

In the following example, if float cannot convert the sensor’s value to a number, it will report 0 so the test becomes 0 > 100. Without a default value, the template would simply fail.

            value_template: >-
              {{ states('sensor.monit_casa_e_fotovoltaico_channel_1_power') |
              float(0) > 100 }}

In the following example, if float can’t convert the sensor’s value (because it’s unavailable or unknown) it will report 20. You can set the default value to be whatever number is the best choice for your application.

temp: "{{ states('sensor.clima_big_inside_temperature') | float(20) }}"

thanks to fix my code however that part is working… what donesn’t work is the part related the variables which always said “not passed” but with gray color… nor organge as usually… so i guess something doesn’t work here or something related the variable setup.

          - condition: template
            value_template: >
              {{ temp > temperature_target or (humidity not in ['unknown',
              'unavailable', 'None'] and humidity | float > humidity_target) }}

Where are you seeing these colors and “not passed” message? In the Automation Editor? Don’t use that for testing Template Conditions, especially when they contain variables defined outside of the actual template.

but the result should be pass looking the values.

No, because it only evaluates the template, not any other part of your automation, like the variables section. So it doesn’t know what temp or temperature_target or humidity are because they are defined outside of the template.

Here’s a simple example:

alias: example 123456
trigger:
  - platform: state
    entity_id: sensor.foo
action:
  - variables:
      x: 1
      y: 2
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ x + y == 3 }}"
        sequence:
          - service: notify.persistent_notification
            data:
              title: Test
              message: "{{ x }}, {{ y }}"
    default: []

The Template Condition “Did Not Pass” because the variables x and y are defined outside of the template and the Automation Editor only evaluates what is known in the template.

You can’t test Template Conditions in the Automation Editor if the template references variables defined outside of the template.

you are totally right. it was unknown from my side. thanks for explanation

You’re welcome!

You’re not the only person who has been misled by the way the Automation Editor tests Template Conditions. Ideally it should process the variables section but it doesn’t. It limits its processing to Jinja2 code in the template and excludes processing any YAML (like what is defined in the variables section).

I suppose what would be a little bit better is if it posted an error message when it encounters a problem with the template (such as "found undefined variables: temp, humidity, …) like what is shown in the Template Editor.

1 Like