Get time door closed after it was opened

I use a Google home to play rain sounds for my daughter’s nap. I’d like to set up an automation so that when I get her up from a nap the Google home stops playing the rain sounds automagically. The thing that is making this tricky, is I only want it to turn off if the door has been closed for more than 45 minutes. This is because I have to go in there sometimes a few times while she is falling asleep to put her stuffed animal back in her bed or whatever, and I don’t want it turning off when I go in there for that.

I have a contact sensor on her door. I tried adding this condition to my automation, but it is always false because the last_changed value is showing up as when the door was opened, not when I closed it.
{{ (now() | as_timestamp) - (states.binary_sensor.baby_door.last_changed | as_timestamp) > 2700}}

Is there a template variable or something I could get when the door was last closed?

Welcome Tyler!

I read about that question in another thread yesterday:

Thank you! That is exactly what I am trying to do. Too bad I didn’t find that thread before posting a new one.

I’m still new to Home Assistant, and would love a small explanation if you have a minute, and maybe advice if I am doing this incorrectly. This is the code I added to my configuration.yaml:

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.baby_door
        from: "off"
        to: "on"
    sensor:
      - name: Nursery Door Closed Duration
        state: "{{ (trigger.to_state.last_updated | as_timestamp - trigger.from_state.last_updated | as_timestamp) | int }}"

From what I understand, the sensor is basically like an entity that will store a value in it - in this case an integer (why wouldn’t we use a helper instead?). The trigger part is basically an automation that runs when the door changes from closed to open. The code in the curly brackets is just getting the number of seconds since the trigger last ran?

This is what I have in my automation.yaml:

- id: '1651787576717'
  alias: Wake up Baby
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.baby_door
    from: 'off'
    to: 'on'
  condition:
  - condition: time
    before: '19:00:00'
    after: 07:15:00
    enabled: true
  - condition: numeric_state
    entity_id: sensor.nursery_door_closed_duration
    above: '1800'
  action:
  - service: notify.mobile_app_tyler_s_phone
    data:
      message: 'Test: Baby is awake - should shut off google home now.'
  mode: single

Ah, okay, if a template sensor is not a must, you can simply use the template part of such a sensor as a condition. E.g.

#### (for UI editor usage)

   condition:
     - condition: template
       value_template: |
         {{ (trigger.to_state.last_changed - trigger.from_state.last_changed) > timedelta(minutes=45) }}


Means:

Door opening time (= binary sensor = on = trigger.to_state.last_changed )
minus
door closing time before (= binary sensor = off = trigger.from_state.last_changed )
greater than 45 minutes

In my humble opinion (I’m no top level Jinja coder :wink: ) you have understood it absolutely right. HA is continuously being developed, so: who knows? - at some point there may be a template helper :slight_smile:

This thread helped me fix an issue I had with spurious events. I only wanted to take action if previous state lasted more than a few second. When I tried the condition as shown above, I got the following error:

In 'template' condition: TypeError: '>' not supported between instances of 'float' and 'datetime.timedelta'

Simple fix was just to compare with the number of seconds. So below is for 10 minutes

condition: template
value_template: |
  {{ ((trigger.to_state.last_changed|as_timestamp) -
  (trigger.from_state.last_changed|as_timestamp)) > 600 }}