Has anyone came up with a complete solution?
This is what I use. I get this via telegram:
Home Assistant Shutdown
Sunday at 01:23:04 PM
Home Assistant Startup
Sunday at 01:24:04 PM
Restart took: 59.9 seconds.
notify:
- name: shutdowns_log
platform: file
filename: shutdowns.log
- name: restart_times_log
platform: file
filename: restart_times.log
sensor:
- platform: file
name: Last Shutdown Timestamp
file_path: shutdowns.log
value_template: "{{ value.split(' ')[0] }}"
- platform: file
name: Restart Time
file_path: restart_times.log
value_template: "{{ value.split(' ')[0] }}"
unit_of_measurement: s
automation:
- id: Shutdown Notification
alias: Shutdown Notification
trigger:
- platform: homeassistant
event: shutdown
action:
- service: notify.shutdowns_log
data:
message: "{{ now().timestamp() }} Shutdown"
- service: script.communicate_log
data:
title: Home Assistant Shutdown
message: " {{ now().strftime('%A at %r') }} "
- id: Startup Notification
alias: Startup Notification
trigger:
- platform: homeassistant
event: start
action:
- service: notify.restart_times_log
data:
message: "{{ (now().timestamp() - states.sensor.last_shutdown_timestamp.state | float) | round(1) }} Restart Time"
- service: script.communicate_log
data:
title: Home Assistant Startup
message: |
{{ now().strftime('%A at %r') }}
Restart took: {{ (now().timestamp() - states.sensor.last_shutdown_timestamp.state | float) | round(1) }} seconds.
recorder:
include:
entities:
- sensor.restart_time
It might be easier to understand if I show the contents of the two log files:
shutdowns.log:
1621119776.465644 Shutdown
1621120391.125334 Shutdown
1621121079.836683 Shutdown
1621121659.466762 Shutdown
1621136335.858423 Shutdown
1621141537.6622 Shutdown
1621141831.100703 Shutdown
1621142584.379932 Shutdown
restart_times.log:
58.2 Restart Time
60.4 Restart Time
46.6 Restart Time
48.8 Restart Time
70.0 Restart Time
65.8 Restart Time
55.2 Restart Time
62.5 Restart Time
59.9 Restart Time
I cant recall whether James posted the config here, or he sent it directly to me
It requires Node-Red and the following packages: node-red-contrib-interval-length, node-red-node-rbe
[{"id":"8fa31d33.f6d548","type":"inject","z":"fa272026.a5eb2","name":"HASS Connected?","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"homeassistant.homeAssistant.isConnected","payloadType":"global","x":90,"y":1100,"wires":[["6311fbf4.a8b4f4"]]},{"id":"6311fbf4.a8b4f4","type":"rbe","z":"fa272026.a5eb2","name":"","func":"rbei","gap":"","start":"","inout":"out","property":"payload","x":340,"y":1100,"wires":[["6b4ce4b0.ad037c"]]},{"id":"6b4ce4b0.ad037c","type":"interval-length","z":"fa272026.a5eb2","format":"human","bytopic":false,"minimum":"35","maximum":"","window":"","timeout":false,"msgTimeout":"","minimumunit":"secs","maximumunit":"secs","windowunit":"msecs","msgTimeoutUnit":"msecs","reset":false,"startup":false,"msgField":"payload","timestampField":"timestamp","repeatTimeout":false,"name":"","x":570,"y":1100,"wires":[["118db764.b6fe21"],[]]},{"id":"118db764.b6fe21","type":"mqtt out","z":"fa272026.a5eb2","name":"","topic":"harestart","qos":"","retain":"true","broker":"c2d1b0f2.af1008","x":810,"y":1100,"wires":[]},{"id":"c2d1b0f2.af1008","type":"mqtt-broker","name":"MQTT Server","broker":"192.168.1.63","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
I am trying your solution but i have this error:
Invalid config for [sensor.file]: not a file for dictionary value @ data[‘file_path’]. Got ‘shutdowns.log’. (See ?, line ?).
Invalid config for [sensor.file]: not a file for dictionary value @ data[‘file_path’]. Got ‘restart_times.log’. (See ?, line ?).
You should define correct paths to log files, e.g. for supervised instance it would be something like /config/log/shutdowns.log
and /config/log/restart_times.log
. Also, make sure to create all needed subfolders for your paths.