Automation template trigger doesn't trigger


Trying to make a bedroom fan auto on/off timer with input sliders, so i can change when it should auto turn off.

Heres the code:

- alias: Fan - Auto-OFF test
  hide_entity: false  
    - platform: template
      value_template: "{% if (as_timestamp(now())-as_timestamp(states.switch.sonoff__room_fan.last_updated)) >(states.input_number.bedroomfan_timer_hours.state |int *3600) + (states.input_number.bedroomfan_timer_minutes.state |int *60) %}true{% else %}false{% endif %}"
    - service: switch.turn_off
      entity_id: switch.sonoff__room_fan

The template just gets the input_number_HOURS and input_number_MINUTES, converts them to seconds, addd them together and compare it to when the room_fan state was last changed.

In HASS template tester it works… Ends up with either a true or false statement.

I can think of two reason why it doesnt work when put into automation.yaml:

  • [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘None’ has no attribute ‘state’
  • Hass doesnt “loop tests” the template… so after x minutes it could end with a true statements, thus triggering the automation

This won’t update because you are using the now() method for a time based trigger. The now() method does not let home assistant know when it needs to trigger because it isn’t a state object. In laymens terms, you need a state object like a sensor to trigger off of.

So currently, your trigger will only trigger off of changes to the following entities: input_number.bedroomfan_timer_minutes, input_number.bedroomfan_timer_hours, and switch.sonoff__room_fan.

If you want this to trigger at any time in the day, you need to add the following sensor to home assistant and replace the now() method with that sensor:

Here is an example of a template that uses the time properly to update:

1 Like

I see… Okey, i will try this!

Got it working. Thanks!!

1 Like