Do something if an attribute has been false for x amount of time

As my first automation, I’d like to have my ecobee turn on the system fan for 10 minutes if it hasn’t run in the past 2 hours. From what I’ve researched so far, this seems more difficult than it sounds :slight_smile:

What are some ideas you folks have to accomplish this?

You could trigger on the state.last_changed attribute of the fan and compare it to the current time like with something like this {{ as_timestamp(now()) - as_timestamp(states(sensor.mysensor.last_changed)) > 7200 }}

You could do something like:

alias: "Turn on the fan"
initial_state: "on"
trigger:
  - platform: state
    entity_id: switch.your_fan
    state: 'off'
    for:
      minutes: 120
action:
  - service: homeassistant.turn_on
    entity_id: switch.your_fan

And then turn it off after 10 minutes with:

alias: "Turn on the fan"
initial_state: "on"
trigger:
  - platform: state
    entity_id: switch.your_fan
    state: 'on'
    for:
      minutes: 10
action:
  - service: homeassistant.turn_off
    entity_id: switch.your_fan

If you only wanted to turn it off if it’d been turned on automatically then create an input_boolean:

fan_auto_on:
  name: "Fan automatically turned on"
  initial: off

Then change the action of the first automation to:

action:
  - service: homeassistant.turn_on
    entity_id: switch.your_fan
  - service: input_boolean.turn_on
    entity_id: input_boolean.fan_auto_on

And the second automation becomes:

alias: "Turn on the fan"
initial_state: "on"
trigger:
  - platform: state
    entity_id: switch.your_fan
    state: 'on'
    for:
      minutes: 10
condition:
  - condition: state
    entity_id: input_boolean.fan_auto_on
    state: 'on'
action:
  - service: homeassistant.turn_off
    entity_id: switch.your_fan
  - service: input_boolean.turn_off
    entity_id: input_boolean.fan_auto_on
1 Like

Thanks. After some more research I’ve come up with the code below. The ecobee fan status is a state attribute. I haven’t been able to get this to work, however. It’s complaining about the “for:” in the trigger. According to the documentation that should work?

- alias: "Run ecobee fan after set time"
  trigger:
    platform: template
    value_template: "{{ is_state_attr('states.climate.main_floor', 'fan', 'off') }}"
    for: 
      minutes: 120
action:
  - service: climate.ecobee_set_fan_min_on_time
    data:
      fan_min_on_time: 10
  - delay: 15:00
  - service: climate.ecobee_set_fan_min_on_time
    data:
      fan_min_on_time: 0

What am I doing wrong here?

Try lining action up with trigger for starters. Spacing is key

@Jer78 is right… spacing is key. After you verify that your spacing is correct, it might be that the actual template trigger won’t fire. This is an issue I have run into, and if you search the forum, you will find that we are not unique. For whatever reason, creating a trigger based on a template sometimes won’t fire, even if that same template is valid and you can make it “work” in the Developer tools.

What I did for my automation that required a template was to simply trigger off of time and use the template as a condition. In your case you can’t trigger off of time, but you could trigger off of something like @Tinkerer said. Namely your fans state and how long it has been in state.

@Jer78, @ammdc7 Thank you. That spacing issue was just a problem with how I formatted it in my post, not in the actual file. It turns out the real issue was that I was editing auomations.yaml through an SMB share in N++, and somehow the file format got switched to Windows/DOS. I changed it back to UNIX and saved the file. Started working. I spent an embarassing amount of time on that mistake :slight_smile:

I took @Tinkerer’s advice and changed the template to this:

value_template: "{% if as_timestamp(now()) - as_timestamp(states.sensor.ecobee_fan.last_changed) > 1800 %}true{% endif %}" 

Just using 1800 seconds to test with. It doesn’t trigger though. How can I view the output of that condition to make sure it is behaving as I expect?

I think that this is the issue I was referring to earlier… There are a few of us here on the forum that can’t get template triggers to work. You might want to consider wrapping that template into a sensor and triggering off of that. There are a few different options you have, but either a sensor or time trigger would be a good option in my mind.

If you want to test to make sure the template is valid, you can use the Developer tools on the frontend. Let me know if you’re not familiar with those and we’ll get you squared away.

Rather than a template trigger, try creating a template sensor, and using that sensor in the trigger.

If nothing else, it’ll perform much better. Template triggers are evaluated at least once per second…

@ammdc7 @Tinkerer Here’s what I’ve done:

sensor:
  - platform: template
    sensors:
      ecobee_fan:
        friendly_name: "Furnace Fan State"
        value_template: "{{ states.climate.main_floor.attributes.fan }}"

automation:
  - alias: "Automated air circulation"
    trigger:
      - platform: state
        entity_id: sensor.ecobee_fan
        to: 'off'
        for:
          minutes: 120

This seems to work well. I’m curious about how triggers work, however. Let’s say the fan is off, and has been for several hours. If I restart HASS and the automation starts up with fan == off it appears that even after the minutes:120 has been met, the trigger doesn’t fire because it doesn’t start until it sees the state change. This isn’t a huge deal for this scenario, the automation just won’t start until the furnace has come on once. But I’m curious if there is any way to fire a trigger based off a condition without a state having to change?

I’m fairly confident it’ll behave as expected, well, other than it’ll be 120 minutes after the startup of HA (so if you keep restarting HA it’ll never happen). That’s because looking at the startup logs, sensors start as unknown or None.

I have asked the question on Discord though, to check that I’m reading that right.

Take a look at my response on the top of this topic. Trigger based on time since state last_changed should trigger also after a reboot.

1 Like