Condition that restart hasn't happened in the last 2 hours

Hi,
Looking for a way to create a condition that a Home Assistant restart hasn’t happened in the last two hours. The reason I am doing this is that I have an automation that watches the state of a switch. If this switch hasn’t been on for the last two hours it triggers. When I do a restart it starts triggering because it hasn’t been on for the last two hours since the restart resets it.

alias: 'ON: Turn on fridge if off for longer than 2 hours'
description: ''
trigger:
  - platform: time_pattern
    hours: /1
  - platform: homeassistant
    event: start
condition:
  - condition: or
    conditions:
      - condition: not
        conditions:
          - condition: state
            entity_id: switch.fridge
            state: 'on'
            for:
              hours: 2
              minutes: 0
              seconds: 0
      - condition: not
        conditions:
          - condition: state
            entity_id: switch.freezer
            state: 'on'
            for:
              hours: 2
              minutes: 0
              seconds: 0
action:
  - service: notify.mobile_app_tincan
    data:
      message: Fridge or Freezer is currently off, are you sure this is right??
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - type: turn_on
    device_id: ba976e16955b49f4a2e11ce42ea83913
    entity_id: switch.fridge
    domain: switch
  - type: turn_on
    device_id: 67becce4d66c4b4f82909f2737023e01
    entity_id: switch.freezer
    domain: switch
  - service: notify.mobile_app_tincan
    data:
      message: Fridge or Freezer was off, turning on
mode: single

Use this:

1 Like

Thanks, however this didn’t seem to work. Anything that I am missing?

condition: state
entity_id: sensor.uptime
state: ''
for:
  hours: 2
  minutes: 0
  seconds: 0

Well… proper indentation for one. I’ll assume that was a copy / paste formatting mistake.

The state of uptime is a time, you need to use a template condition to compare this time with now() to see if two hours has passed.

Like this:

condition:
  - "{{ ( now() - states('sensor.uptime')|as_datetime ).seconds > 7200 }}"
1 Like