Automation was not triggered (template)

I was trying to create a blueprint automation to control the exhaust fan in the bathroom that turns off if the dew point drops below a threshold, but the fan would have to be on for a specific time before that (15 minutes). I have set up the automation as below, but the automation only triggered once, when it past the 15-minute on condition. It was never triggered again when the dew point drops below the level.

trigger:
  - platform: state
    entity_id: fan.master_bedroom_vent_fan
    to: 'on'
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - platform: template
    value_template: >-
      {{ (states(actual_dp)|float - states(bl_dp)|float)|round(1) <= threshold
      }}
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: fan.master_bedroom_vent_fan
        state: 'on'
        for:
          hours: 0
          minutes: 15
          seconds: 0
      - condition: template
        value_template: >-
          {{ (states(actual_dp)|float - states(bl_dp)|float)|round(1) <=
          threshold }}
action:
  - service: homeassistant.turn_off
    entity_id: fan.master_bedroom_vent_fan
variables:
  bl_dp: sensor.master_bedroom_dew_point
  actual_dp: sensor.master_bedroom_ensuite_dew_point
  target_fan: fan.master_bedroom_vent_fan
  threshold: 2
mode: restart

May I know what is missing? I thought the HA would evaluate the template every time any entity inside the template changes.

For values that need to be available to template triggers you should use Trigger Variables.

trigger_variables:
  bl_dp: sensor.master_bedroom_dew_point
  actual_dp: sensor.master_bedroom_ensuite_dew_point
  target_fan: fan.master_bedroom_vent_fan
  threshold: 2
trigger:
  - platform: state
    entity_id: fan.master_bedroom_vent_fan
    to: 'on'
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - platform: template
    value_template: >-
      {{ (states(actual_dp)|float - states(bl_dp)|float)|round(1) <= threshold }}
condition:
  - condition: state
    entity_id: fan.master_bedroom_vent_fan
    state: 'on'
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - condition: template
    value_template: >-
      {{ (states(actual_dp)|float - states(bl_dp)|float)|round(1) <= threshold }}
action:
  - service: homeassistant.turn_off
    entity_id: fan.master_bedroom_vent_fan
mode: restart

I would add that float should have a default value to keep from errors and log spam
(states(actual_dp)|float(1) ...

1 Like