How to turn a switch off if it is running more than 10 minutes after a HA restart

Ordinarily, this should be pretty simple, as follows:

    entity_id: switch.homeseer_switch_mba_fan
    platform: state
    to: 'on'
      minutes: 2

But what happens if HA is rebooted while the fan was on?

switch.homeseer_switch_mba_fan last_changed and switch.homeseer_switch_mba_fan last_updated should be reset when home assistant starts. So it should trigger 2 minutes after HA starts.

Unfortunately, that’s not happening for me. What is the syntax for reading last_changed and last_updated?


And I see why my advice was wrong. Here’s how I actually stop my dehumidifier:

- id: lounge_dehumidifier_max_runtime_stop
  alias: 'Lounge Dehumidifier Max Runtime Stop'
  initial_state: true
    platform: template
    value_template: "{{ (as_timestamp(strptime(states('sensor.date_time'),'%Y-%m-%d, %H:%M')) - as_timestamp(states.switch.lounge_dehumidifier.last_changed) ) / 3600 > states('input_number.lounge_dehumidifier_run_time') | float }}"

This will stop at the run time specified in the input_number (in hours), either after it has been on that long or that long after a HA restart.

I’m going to improve it according to this advice, so that restarts don’t affect the run time:


  minutes: 2

behaves as a timer. According to your trigger, switch.homeseer_switch_mba_fan must change to on and remain in that state for at least 2 minutes for the trigger to occur. The key here is that the 2-minute countdown starts at the moment the fan’s state changes to on.

Let’s assume the 2-minute countdown is underway. At around 90 seconds, you restart Home Assistant. Upon restart, the 2-minute timer is also reset, not restarted, but reset. That 90 seconds of elapsed time is reset to 0 after the restart.

In addition, after the restart, the trigger won’t do anything until the fan’s state changes to on. If the fan is already on after the restart, the trigger will never start the 2-minute countdown timer. It will only do so if the fan’s state changes to on (so turned off first then turned on).