Built in automatic timeout / decay function for switches and binary sensors

I was looking at the Turn on Lights for 10 Minutes example and was somewhat annoyed that it was so verbose to do something so simple.

I think if switches or binary_sensors templates had a time out attribute you could set it would allow for some very interesting features.

  1. Switches that always turn them selves off after X
  2. A template occupancy sensor that you reset when users are taking actions and it counts down back to an off / vacant state after a set time.

I did this via a template sensor:

binary_sensor:
 - platform: template
   sensors:
      bedroom_occupancy:
        friendly_name: Bedroom Occupancy
        value_template: '{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.bedroom_motion.last_changed) < 1800)}}'
        sensor_class: motion
        entity_id: sensor.time

you can then use this binary sensor in an automation to turn something on/off with the for option in the trigger

1 Like

I get the following error when trying to create this type of template.

homeassistant.components.binary_sensor.template: UndefinedError: ‘None’ has no attribute ‘last_changed’

You also reference bedroom_motion and class motion but label it as occupancy.

Can you post your configuration of the relevant item?

I label it as occupancy since I use the motion detectors to establish whether someone is in the room.

I think a better question is what is your example doing.

Is it taking the input of a REAL sensor binary_sensor.bedroom_motion of class motion sensor and then activating it self for 1800 seconds?

I don’t currently have any motion sensors so I can’t confirm if they have a last_changed attribute.

What I am looking for is the template it self to time out if turned on by ANYTHING. As this becomes universally useful for “activity” tracking…