Detect if HomeAssistant was rebooted

Hello,

please adivce me how to detect that HomeAssistant was rebooted and now up and ready?
Ok i can check this glob var


But it will trigger every time i make deploy and so on.

Or this one will trigger on every soft restart of НА


I would like to detecd only hard reboots of PI (for example unexpected if the electricity was down)

Thanks a lot!

The easiest way to do this is actually in Home Assistant itself using a simple automation:

- id: f2e2bedc4ebb49528afd7807264734d9
  alias: 'Notifications : Home Assistant : Startup'
  description: Sends notifications when HA starts up.
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      entity_id: media_player.office
      message: Home Assistant has started.
    service: tts.amazon_polly_say
  - data:
      entity_id: media_player.kitchen
      message: Home Assistant has started.
    service: tts.amazon_polly_say
  - data:
      message: Home Assistant has started
      title: Home Assistant - Status
    service: notify.system_messages
  mode: single
- id: '1574363508782'
  alias: 'Notifications : Home Assistant : Shutdown'
  description: Sends out a notification when HA shuts down.
  trigger:
  - event: shutdown
    platform: homeassistant
  condition: []
  action:
  - data:
      entity_id: media_player.office
      message: Home Assistant has stopped.
    service: tts.amazon_polly_say
  - data:
      entity_id: media_player.kitchen
      message: Home Assistant has stopped.
    service: tts.amazon_polly_say
  - data:
      message: Home Assistant has stopped
      title: HA - Status
    service: notify.system_messages
  mode: single

[EDIT]

If NodeRed is running on a separate server, you could accomplish this using the Ping node. If you HA Pi stops responding to a ping, start a loop and wait for the Pi to come back up. But, if your power is out, unless you have NodeRed on a battery backup of some sort, it’s not going to be able to do anything.

The other solution, if your HA instance is public facing and accessible is to use a service like UptimeRobot monitor your external URL. If your instance is down for more than 5 minutes, it sends you an email.

2 Likes

I am using glances on my pis and home-assistant box. It’s also available as an add-on. It reports the uptime (albeit as a string), you can use that also that, and detect when it was reset.

1 Like

Thanks.
But uptime give you no detection of hard reboot.
I have already made it. Now i am from Smartphone, but if someone needs it, let me know.

How did you do it?

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}]
1 Like

I like your definition of simple. :smiley:

I see what you have done there. Very clever!

I mean the logic is simple!

1 Like

Hi, how do I run it alone when restarting Hassio? The “Fake Node” node does not run alone.

Not sure this will help but personally I have a YAML automation that toggles an input boolean when HA starts. Then I use that input to trigger my flows.

- id: '4567773212467'
  alias: DAILY DING on restart
  trigger:
  - event: start
    platform: homeassistant
  action:
  - delay: 00:00:05
  - service: homeassistant.turn_on
    entity_id: input_boolean.startup
  - delay: 00:00:10
  - service: homeassistant.turn_off
    entity_id: input_boolean.startup
2 Likes

@thisIO I copied your code to my NodeRed and adjusted the sql query i.a.w. the MariaDB shema like below (HA Version 2023.4.5). I do have three questions about that:

  1. Unfortunatelly I connot find the sensors sensor.hassio_previous_start_date_time and sensor.hassio_previous_start_date_time in the HA developer tools. Do you know why?
  2. NodeRed tells me I need to update the nodes. Is it really neccessary after starting NodeRed? (yellow marked Node, see screenshot below)
  3. What does the input_boolean.fake do? I do get an error there. (see screenshot below)
#HA-Stert-Stop-Reboot-Sensor: https://community.home-assistant.io/u/thisIO
  - platform: sql
    db_url: !secret mariadb_url
    queries:
      - name: Hassio Last Stop Date Time
        query: "SELECT * FROM 'homeassistant events' WHERE event_type = 'homeassistant_stop' ORDER BY event_id DESC LIMIT 1;"
        column: 'time_fired_ts'
  - platform: sql
    db_url: !secret mariadb_url
    queries:
      - name: Hassio Previous Start Date Time
        query: "SELECT * FROM 'homeassistant events' WHERE event_type = 'homeassistant_start' ORDER BY event_id DESC LIMIT 1,1;"
        column: 'time_fired_ts' 

Homeassistant DB-Sheme: