I have made two custom sensors:
- platform: sql
queries:
- name: Hassio Last Stop Date Time
query: "SELECT * FROM 'events' WHERE event_type = 'homeassistant_stop' ORDER BY event_id DESC LIMIT 1;"
column: 'time_fired'
- platform: sql
queries:
- name: Hassio Previous Start Date Time
query: "SELECT * FROM 'events' WHERE event_type = 'homeassistant_started' ORDER BY event_id DESC LIMIT 1,1;"
column: 'time_fired'
And then on the connect of Node-Red with HA i compare times of last stop and previous start.
Logic - if there was no last stop command after previous start - it means that restart reason was electricity down. Simple!
Here is the Node Red Flow
[{"id":"4b73794.7144288","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c45b7226.ab866","type":"switch","z":"4b73794.7144288","name":"","property":"a.hassio_soft_reboot_trigger","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":210,"wires":[["3b25cb67.035fd4"],["b53a59a1.e18d88"]],"l":false},{"id":"5be57e99.6fcda","type":"function","z":"4b73794.7144288","name":"Check","func":"//msg.a.previous_start_date_time = new Date(msg.a.hassio_previous_start_date_time.state).getTime();\n//msg.a.last_stop_date_time = new Date(msg.a.hassio_last_stop_date_time.state).getTime();\n\nif (new Date(msg.a.hassio_previous_start_date_time.state).getTime() < new Date(msg.a.hassio_last_stop_date_time.state).getTime()) {\n msg.a.hassio_soft_reboot_trigger = true\n reboot_reason = \"Soft Restart\"\n} else {\n msg.a.hassio_soft_reboot_trigger = false\n reboot_reason = \"Hard Reboot\"\n}\n\nmsg.payload = 'Hassio Started! Reason: ' + reboot_reason\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":210,"wires":[["c45b7226.ab866"]]},{"id":"9e3c1803.667ac8","type":"api-current-state","z":"4b73794.7144288","name":"hassio_last_stop_date_time","server":"63f11f83.adf4c","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.hassio_last_stop_date_time","state_type":"str","state_location":"","override_payload":"none","entity_location":"a.hassio_last_stop_date_time","override_data":"msg","blockInputOverrides":false,"x":210,"y":225,"wires":[["5be57e99.6fcda"]]},{"id":"4c7d183.38e3ce8","type":"api-current-state","z":"4b73794.7144288","name":"hassio_previous_start_date_time","server":"63f11f83.adf4c","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.hassio_previous_start_date_time","state_type":"str","state_location":"","override_payload":"none","entity_location":"a.hassio_previous_start_date_time","override_data":"msg","blockInputOverrides":false,"x":230,"y":180,"wires":[["9e3c1803.667ac8"]]},{"id":"738c72c3.5bbddc","type":"comment","z":"4b73794.7144288","name":"Check Restart/Reboot Reason","info":"Soft Reset\nHard Reboot","x":210,"y":135,"wires":[]},{"id":"506bea6c.46df04","type":"server-state-changed","z":"4b73794.7144288","name":"Fake Node","server":"63f11f83.adf4c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.fake","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is_not","outputs":1,"output_only_on_state_change":false,"x":105,"y":75,"wires":[["4c7d183.38e3ce8"]]},{"id":"3b25cb67.035fd4","type":"debug","z":"4b73794.7144288","name":"Hard Reboot","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":180,"wires":[]},{"id":"b53a59a1.e18d88","type":"debug","z":"4b73794.7144288","name":"Soft Restart","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":240,"wires":[]},{"id":"63f11f83.adf4c","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]