Run flow on homeassistant restart

I was looking for a way to run a command when home assistant is restarted. Based on the changelog it looked like the way to do this was to use the “Events all” node and specifying the home_assistant_client event type when it’s msg.payload is connected. However on restart nothing is ever triggered even though the text underneath the node changes from ‘disconnected’ to ‘connected’. Any idea what I"m missing?

connected

[
    {
        "id": "42c22cba.2751fc",
        "type": "api-call-service",
        "z": "842f595d.496c68",
        "name": "Install chromedriver",
        "server": "b454cc2c.3067b",
        "service_domain": "shell_command",
        "service": "install_chromedriver",
        "data": "",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "x": 560,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "b1446f65.e73bc",
        "type": "switch",
        "z": "842f595d.496c68",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "connected",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 370,
        "y": 240,
        "wires": [
            [
                "42c22cba.2751fc"
            ]
        ]
    },
    {
        "id": "7b430d40.702f84",
        "type": "server-events",
        "z": "842f595d.496c68",
        "name": "home_assistant_client",
        "server": "b454cc2c.3067b",
        "event_type": "home_assistant_client",
        "x": 160,
        "y": 240,
        "wires": [
            [
                "b1446f65.e73bc"
            ]
        ]
    },
    {
        "id": "b454cc2c.3067b",
        "type": "server",
        "z": "",
        "name": "Home Assistant",
        "legacy": false,
        "hassio": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true
    }
]

You can create a sensor for the HA uptime and use that as trigger for your automation.

- platform: uptime
  name: "HA Runtime"
  unit_of_measurement: minutes
2 Likes

Those events happen in ha. My guess is you’re not gonna catch them in nodered especially before connected.
Try a yaml automation that turns on a Boolean on start and catch that state in nodered. I did that for an automation that I wanted to execute in nodered on ha start.

Firstly create a shell command.

shell_command:
  ha_start: curl http://yourip:port/hastart

Next create an automation

automation:
  - alias: homeassistant_start
    trigger:
      - platform: homeassistant
        event: start
    action:
      - service: shell_command.ha_start

Add this Node “http” you can add or leave the delay I just make sure everything has loaded before I run restart automations.

image

image

Enjoy :slight_smile:

3 Likes

Thanks for all of the suggestions! I ultimately went with @jimpower 's suggestion. I was not familiar with the http node, it’s pretty cool. Just for reference for anyone who stumbles upon this thread, the shell_command url should be the ip/port of node-red, not home assistant. I was using the ip/port for home assistant initially and was wondering why it wasn’t working until I read the documentation closer for the http node in node-red.

1 Like

Yeah I was going to add that when I got home glad you figured it out as I didn’t make that part clear. This however may break with 0.92.0 and Node Red ingress unless you re-enable the port option.

I have been using MQTT to send HA startup messages to node-RED. This would still work with ingress mode although you are relying on an extra component.
I have an automation that sends a startup MQTT message. I have only just setup brith and last will MQTT messages, I am looking into migrating over to those soon.

I can’t seem to get this work work no matter what I try. I set up the shell command and automation but cant get anything to appear when it restarts.

shell_command:
  ha_start: curl http://hassioip:1880/hastart

I’ve tried both http and https without luck. When I navigate to http://hassioip:1880 it brings up a username and password field and I try to log in using the username and password in my config with no luck. I also tried admin as the username with my secret key as my password and no luck. I’m running to last version of hassio and node red. Any suggestions?

Here is my node red config:

{
  "credential_secret": "PASSWORD",
  "dark_mode": true,
  "http_node": {
    "username": "USERNAME",
    "password": "PASSWORD"
  },
  "http_static": {
    "username": "USERNAME",
    "password": "PASSWORD"
  },
  "ssl": true,
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem",
  "require_ssl": false,
  "system_packages": [],
  "npm_packages": [],
  "init_commands": []
}

edit: when I trigger the automation I see this in my node red logs:

[08/Nov/2019:10:00:24 -0500] 401 -(192.168.1.127) GET /hastart HTTP/1.1 (curl/7.66.0)

I was able to login at http://hassioip:1880 but when I try to go to http://hassioip:1880/hastart all’s I see is

Cannot GET /hastart

From the add-on docs:

Note : In order to use the http_node you will need to expose Node-RED using a network port in addition to ingress. The HTTP nodes will also be presented under /endpoint/ as shown in the UI. If using the node-red-dashboard module this will also be hosted under this path and will use any credentials set here.

I thought I exposed the port by specifying 1880 under network port on the config page. I’m not really familiar with what the /endpoint/ thing is

Maybe http://hassioip:1880/endpoint/hastart in your case

That did it! Thanks

Hi,
where do i create the shell command ?
thanks

Right inside your configuration.yaml

Hi
First thanks for your help
I have tryed many times, many things but it does not run

what is hastart in the shell command ha_start: curl http://yourip:port/hastart ?

That tripped me up too. After some help from @mbonani I used

 ha_start: curl http://yourip:port/endpoint/hastart 

And that worked

2 Likes

I used to use the http node, then I discovered the status node which seemed to be a better way to achieve the same result. I even wrote about it here.

2 Likes

I tried to get this to work and couldn’t make it happen. I hooked a status node to a debug and the tried to get it to show a msg.status.text of “connected”, but it wouldn’t. It would just be blank with no debug. It would show the “on” and “off” state of the entity, but would not show the connected or disconnected status even though I would see the green dot and connected or red dot disconnected below the node it was checking. Either way not a big deal since I got the shell command working, but I always like trying new ways of doing things.

edit: Well I couldn’t get it to show connected through the debug node, but when I actually restarted it triggered 19 times. It works but now I just need to scale it down I guess

This worked well for me. I have the flow trigger when the sensor reaches 2 minutes to give Hassio time to start the Node Red addon.

2 Likes