Condition: if a switch is ON for more than a given time

I’d like to make an automation: if an input boolean is turned off it checks that a switch is ON for 1 hour. If yes than it turns off the switch else it leaves ON the switch. So I need a condition which checks if the switch is ON for more than an hour. How should I do this?

You can use a history_stats entity for a paeriod (last hour) and use that output as a condition

Thanks a lot! I’m thinking about a template which checks if the switch is ON for more than an hour currently but I’m not so good in templates.

You could use the same history_stats for a period of 1+ hour (e.g. 1.5) and you check when it’s value is more than 1
this would be sufficient imo

  - platform: history_stats
    name: switch_on
    entity_id: [yourswitchentity]
    state: "on"
    type: time
    duration: "01:05:00"

if measures the time the switch is on for the past 1hour and 5 minutes (rolling)
The automation condition would use

  1. trigger: boolean off
  2. condition: sensor.switch_on > 1

Wouldn’t a simple for in the condition not be enough (or missed i something in the question)?

    - platform: state
      entity_id: switch.your_switch
      state: 'on'
        hours: 1
        minutes: 1

Yeah, you are right, I mistook the switch to be the same as the boolean and then it is less simple,

Maybe I was not clear enough. I have an input boolean for a schedule. The schedule switches on or off the input boolean.
I have an automation which switches on or off a switch based on input boolean state. But I also can use the switch manually. I’d like to avoid the case when I switch on the switch manually, but it switches off e.g. in 5 minutes because the automation. So my solution when the schedule triggers the automation via input boolean to switch off my switch, it should check that the switch is on for at least 1 hour (it can be 1.5, 2 or 3 hours as well but shall be more than one hour). That’s why I think a template condition would be nice to check if the current time is bigger with at least one hour compared to the switch on time of my switch.

well ok… you have the options with history_stats sensor
On template I am less confident

But that’s exactly what the for condition does. In my example it would check if the switch is at least 1 hour and 1 minute in state on (it could be 2 or three hours, but more than…). If the condition is true the action part of the automation runs. If not, the automation ends.
A template would be more complicated, but if you want to, this should work:

    - condition: template
      value_template: "{{ (as_timestamp(now()) | int(0) -as_timestamp(states.switch.vour_switch.last_changed))|int(0) > 3600 and states('switch.your_switch') == 'on'}}"

This template checks if the switch is on and if the last state change is more than 3600 seconds (assuming it switched from off to on) before the actual time.
But in the end it does the same as the for in the condition, only more complicated.

1 Like