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"
@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') }}"
@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.
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.