Help to template ... when house is empty for 20 hours

I want to see if there has not been any person in my house for over 20 hours - then make a ‘FALSE’ state - so I can use it in a condition.

I found this somewhere, but I dont think its right.

{% if as_timestamp(states.group.awesome_people.last_changed) | timestamp_custom('%H') < '20' %}true{% else %}false{% endif %}

Since the last_changed property does not survive restart, you may need to split this into two parts to get a reliable end result.

template:
  - trigger:
      - platform: numeric_state
        entity_id: zone.home
        below: 1
    sensor:
      - name: House Last Occupied
        state: "{{ now() }}"
        device_class: timestamp
  - binary_sensor:
      - name: House Unoccupied for 20 hours
        state: |
          {{ now() - timedelta(hours=20) >= 
          states('sensor.house_last_occupied') | as_datetime and 
          states('zone.home')| int(0) < 1 }}
4 Likes

Thanks, I think this will work. :slight_smile:

sorry it did not work, It still shows we are out of the house - its not reset when we come home the zone.home is above 0
can you help?

Sorry about that, I forgot the second clause for the binary sensor. I have corrected it above.

Thanks, It work :slight_smile: