Alert that cat door is closed for over an hour

I’ve installed a z-wave door sensor on our cat door to alert me that it’s been closed an hour. I’ve got it working OK, but anytime I reboot Home Assistant when the cat door timer is running, it looses it’s state.

How could I store the timestamp for when the cat door closes and clear that timestamp when the door opens again? It needs to be able to survive a reboot of Home Assistant.

Thanks for any help!

binary_sensor:
  - platform: template
    sensors:
      cat_door_timer:
        friendly_name: "Cat Door Timer"
        icon_template: mdi:cat
        delay_on:
          seconds: 3600
        value_template: "{{ is_state('binary_sensor.cat_door_sensor', 'off') }}"

sensor:
  - platform: template
    sensors:
######### cat door status sensor 
      catdoorstatus:
        friendly_name: Cat Door Status
        icon_template: mdi:cat
        value_template: "{% if is_state('binary_sensor.cat_door_sensor', 'off') %}Closed{% else %}Open{% endif %}"

automation:
  - alias: 'Cat Door closed for 1 hour'
    initial_state: true
    trigger:
      platform: state
      entity_id:
        - binary_sensor.cat_door_timer
      to: 'on'
    action:
      - wait_template: "{{ is_state('media_player.mpd', 'off') }}"
      - service: media_player.play_media
        data:
          entity_id: media_player.mpd
          media_content_id: local:track:cat_door_closed_one_hour.mp3
          media_content_type: "audio/mp3"

maybe this could help:

somehow I have a feeling there is a integrated way to do this now, but can’t recall the details.

1 Like

You could use https://home-assistant.io/components/input_datetime, that will survive a restart if you don’t set initial

1 Like

@datamonkey I did some experimentation with the variable component and it doesn’t seem to maintain it’s state after a reboot.

@Tinkerer input_datetime seems to be doing what I want, but time templates are still confusing me. I want to add an hour to the the input_datetime. Can you point me in the right direction?

input_datetime:
  cat_door_date_and_time:
    name: Cat door date and time
    has_date: true
    has_time: true
    icon: mdi:cat

automation:
  - alias: 'Cat Door closed'
    initial_state: true
    trigger:
      platform: state
      entity_id:
        - binary_sensor.cat_door_sensor
      to: 'off'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.cat_door_date_and_time
        data_template:
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

Here are the time functions. You’d want to do something like:

  1. Convert the current time to a number of seconds
  2. Add an hours worth of seconds
  3. Convert to a human time

Taking that step by step:

{{ as_timestamp(now()) }}
{{ as_timestamp(now()) + (60 * 60) }}
{{ (as_timestamp(now()) + (60 * 60))|timestamp_local }}
2 Likes

@Tinkerer Thanks for the details on time templates. I’ve got it working correctly now! I’m including the final version of my automation below for anybody who might want to reference it.

input_datetime:
  cat_door_time:
    name: Cat door time
    has_time: true
    icon: mdi:cat

automation:

  - alias: 'Cat Door closed'
    initial_state: true
    trigger:
      platform: state
      entity_id:
        - binary_sensor.cat_door_sensor
      to: 'off'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.cat_door_time
        data_template:
          datetime: "{{ (as_timestamp(now()) + (60 * 60))|timestamp_local }}"

  - alias: 'Cat Door Alert'
    initial_state: true
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') == (state_attr('input_datetime.cat_door_time', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
    condition:
      condition: state
      entity_id: binary_sensor.cat_door_sensor
      state: 'off'
    action:
      - wait_template: "{{ is_state('media_player.mpd', 'off') }}"
      - service: media_player.play_media
        data:
          entity_id: media_player.mpd
          media_content_id: local:track:cat_door_closed_one_hour.mp3
          media_content_type: "audio/mp3"
1 Like

After further testing, I discovered that the z-wave door sensor was changing state on reboot. I added some automations and an input boolean to track that status of the zwave network. Waiting for the zwave network to start resolved the last issue I was having. If anybody wants to see those automations, let me know.