Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Home Assistant] Restart Time
################################################################
# Automations / Restart Time
# Publish timestamps to MQTT broker on start and shutdown.
################################################################
- id: restart_time_start
alias: Publish Start Time
description: Publish current timestamp to MQTT broker on startup.
mode: single
trigger:
- platform: homeassistant
event: start
action:
- service: mqtt.publish
data:
topic: homeassistant/start
payload: "{{ now()|as_timestamp }}"
retain: true
- id: restart_time_shutdown
alias: Publish Shutdown Time
description: Publish current timestamp to MQTT broker on shutdown.
mode: single
trigger:
- platform: homeassistant
event: shutdown
action:
- service: mqtt.publish
data:
topic: homeassistant/shutdown
payload: "{{ now()|as_timestamp }}"
retain: true
################################################################
# Sensor / Restart Time
################################################################
# Sensor for Home Assistant start timestamp.
- platform: mqtt
name: Start Timestamp
unique_id: 120a114d-a8a1-4364-8d43-a35954b19239
icon: mdi:timer-outline
state_topic: homeassistant/start
# Sensor for Home Assistant shutdown timestamp.
- platform: mqtt
name: Shutdown Timestamp
unique_id: affce05d-153b-48f2-af61-1333e50c2163
icon: mdi:timer-outline
state_topic: homeassistant/shutdown
# Sensor for Home Assistant difference between start and shutdown times.
- platform: template
sensors:
restart_time:
friendly_name: Restart Time
unique_id: c252bb03-374f-41a9-9e41-d1e1e02b9d90
icon_template: mdi:timer-outline
unit_of_measurement: s
device_class: timestamp
value_template: "{{ (states('sensor.start_timestamp')|float(0) - states('sensor.shutdown_timestamp')|float(0))|round(1) }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment