Template with `state_attr` trigger not triggering automation

I have the following blueprint;

blueprint:
  name: Set climate target temperature from schedule.
  domain: automation
  input:
    target_sensor:
      name: Temperature Sensor
      selector:
        entity:
          filter:
            integration: schedule_state
    target_area:
      name: Area
      selector:
        area:
          entity:
            domain: climate
variables:
  sensor: !input target_sensor
  area: !input target_area
  devices: >-
    {{ 
      expand(area_entities(area)) 
        | selectattr('domain', 'eq', 'climate') 
        | map(attribute='entity_id') 
        | list
    }}
trigger:
  - platform: template
    value_template: "{{ states(sensor) }}"
  - platform: homeassistant
    event: start
  - platform: template
    value_template: &noErrorCondition >-
      {{ state_attr(sensor, 'errors') | length == 0 }}
  - platform: template
    value_template: >-
      {{ state_attr(sensor, 'errors') }}
condition:
  - condition: template
    value_template: *noErrorCondition
action:
  - if:
      - condition: template
        value_template: "{{ states(sensor) == 0 }}"
    then:
      - service: climate.set_temperature
        data:
          temperature: 5
        target:
          entity_id: "{{ devices }}"
      - delay:
          milliseconds: 500
      - service: climate.turn_off
        target:
          entity_id: "{{ devices }}"
    else:
      - service: climate.set_hvac_mode
        data:
          hvac_mode: auto
        target:
          entity_id: "{{ devices }}"
      - delay:
          milliseconds: 500
      - service: climate.set_temperature
        data:
          temperature: "{{ states(sensor) | int }}"
        target:
          entity_id: "{{ devices }}"
mode: queued
max: 50

Which I used in the following way;

id: '1697455140665'
alias: Set Temperatur Wohnzimmer
description: ''
use_blueprint:
  path: cwrau/climate-set-target-temperature.yaml
  input:
    target_area: wohnzimmer
    target_sensor: sensor.wohnzimmer_temperatur_target

The last 2 triggers are there because after a configuration reload the sensor.wohnzimmer_temperatur_target, which is a schedule_state, is in an error state and gives the wrong state.

Iā€™ve observed the template {{ state_attr('sensor.wohnzimmer_temperatur_target', 'errors') | length == 0 }} in the developer tools and it progresses from ā€œan exception because the attribute is Noneā€ to false and finally to true after the integration reconciles after a minute.

But the automation never gets triggered. Is there anything I did wrong?

The order you place things in the automation does not dictate when it is executed. There is an order of execution.

In your automation, ā€˜sensorā€™ is being generated by a variable statement. Variable statement is processed after the trigger, hence when the trigger happens ā€˜sensorā€™ does not exist.

If you want a variable you can trigger on, use a trigger_variable statement. These are limited templates so not all jinga with work in triggers and trigger variables, again because a lot of things donā€™t exist yet in the automation, so be cautious.

You can also look at using a float filter on that sensor to make sure itā€™s numeric defaulting to 0 and add a condition that if the value is 0, donā€™t run.
Just a couple of thoughts.

1 Like

If this turns out to solve your problem, please consider clicking the solution button.

Thanks, that worked!