Using mqtt to shutdown home-assistant

Home Assistant 2022.10.5 Supervisor 2022.10.2
Operating System 9.3 Frontend 20221010.0 - latest
running haos_generic-aarch64-9.3.qcow2 virtual machine under ubuntu-aarch64 (Cheap TV box). Host mqtt.

I am concerned regarding the data corruption danger of shutting down my haos virtual machine and want to do it automatically / cleanly as part of ubuntu host shutdown service, especially if using a SD card. My research indicates I can use an mqtt message.

Here is the command (from virtual machine host - ubuntu) that is ignored:

mosquitto_pub -t /homeassistant/hassio -m “hassio.host_shutdown” -u <mqtt_userID> -P <mqtt_password> -h localhost

I believe all is correct except the topic (-t). What is correct topic?

bonus: is there a list of valid HA control messages such as “hassio.host_reboot”?

Thanks;
Bill

Where did you read that you can use MQTT to shutdown Home Assistant? The linked topic you posted doesn’t even mention MQTT.

Here’s what I’m using

SET MOS="C:\Program Files\mosquitto\mosquitto_pub.exe"

%MOS% -h 192.168.1.26 -t homeassistant/CMD/shutdown -m {\"type\":\"HA\",\"cmd\":\"SHUTDOWN\"} -u username -P password -q 2 -r

I believe you overlooked to mention that this requires the use of a custom integration called IOT Link.

I use docker to run Home Assistant, and have a similair case. In a certain scenario I want to shutdown the host server, initiated frome Home Assistant.

What I do is run a shell command from an automation, writing some specific text in a file. On the host server I have a script that starts at boot, and runs in a loop, that monitors that file. When the text is found, shutdown command is given.

If interested I can share it with you.

my bad. Wrong link. Mentioned here: How Can I Cleanly Shutdown HA In VirtualBox From Windows (The Host) With CLI?

IOT Link: Integrate your Windows PC with Home Assistant using IOT Link

seems to be be for HA to Control / monitor host. I want host to control HA (at least shutdown) from host.

@ServiceXp

Can you please confirm your command? What version of HA are you using? Possible version syntax change?

This not working for me - removed escapes, windoze, made linux compat:

mosquitto_pub -d -t home-assistant/CMD/shutdown -m ‘type:HA,cmd:SHUTDOWN’ -u USER -P PASS -h localhost -q 2 -r
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending PUBLISH (d0, q2, r1, m1, ‘home-assistant/CMD/shutdown’, … (20 bytes))
Client (null) received PUBREC (Mid: 1)
Client (null) sending PUBREL (m1)
Client (null) received PUBCOMP (Mid: 1, RC:0)
Client (null) sending DISCONNECT

and ignored by HA

Why not using automation?


description: ""
mode: single
trigger:
  - platform: mqtt
    topic: sometopic
    payload: somepayload
condition: []
action:
  - service: hassio.host_shutdown
    data: {}

@aceindy

If I fail using built in mqtt method, your suggestion will be “plan B”. Thanks.

I created a MQTT sensor in HA to trigger on …

Example:

  - unique_id: '123456HomeAssistantShutDown'
    state_topic: "homeassistant/CMD/shutdown"
    value_template: "{{ value_json.cmd }}"

plan c, send a webhook with Curl?

Are you sure Ubuntu doesn’t close down VB properly?

I use VMware om debian, and it suspends the virtual machine when i restart/shut it down

edit: did some reading, and it appears VB work differently from VMware…

I use virt-manager and, shutdown, reboot do not work from GUI. I have to use force which is dangerous, since not graceful, risks disk corruption.

hence; here I am, attempting to avert such woes.

There is no “built-in method” and the corrected link you posted misled you into believing there is one.

Just use aceindy’s automation. It’s concise and does exactly what you want. Using an MQTT Trigger, you can define whatever topic and payload you want.

Go to Developer Tools > Services and enter “hassio”. It will list the available service calls:

Thanks All, @aceindy ; This is sorted out.

#1 HA has no builtin method to reboot or execute any services from MQTT messages. Need to define MQTT topic, message(s) and create automation:
#2 Absolutely do not use -r (retain) when sending MQTT shutdown, reboot messages. They will be received and acted upon at HA start, leading to reboot loop.

automations/shutdown.yaml:

- id: 'shutdown home-assistant'
  mode: single
  trigger:
    - platform: mqtt
      topic: home-assistant/CMD
      payload: SHUTDOWN
  condition: []
  action:
    - service: hassio.host_shutdown
      data: {}

- id: 'reboot home-assistant'
  mode: single
  trigger:
    - platform: mqtt
      topic: home-assistant/CMD
      payload: REBOOT
  condition: []
  action:
    - service: hassio.host_reboot
      data: {}

shutdown message (using host mqtt):
mosquitto_pub -t home-assistant/CMD -m SHUTDOWN -u mqtt_user -P mqtt_password -h localhost

reboot message (using host mqtt)
mosquitto_pub -t home-assistant/CMD -m REBOOT -u mqtt_user -P mqtt_password -h localhost

I implemented the re-boot command because, occasionally communication to host USB devices (zigbee, zwave HUSZB-1) craps out for some reason and a HA reboot is required. Intend to detect this state and reboot using another automation.

NOTE: It takes a long time for SHUTDOWN, REBOOT to happen (not including HA restart time). Hints to reduce this time appreciated.

Style tips also appreciated

If aceindy’s suggestion solved your problem then it’s customary to tag aceindy’s post with the Solution tag (not your own which merely implements aceindy’s example). For more information about the Solution tag, refer to guideline 21 in the FAQ.


NOTE

There’s no need to create separate automations for each command (nor is there a need to capitalize portions of an MQTT topic) and can be done with a single automation.

- id: 'home-assistant host commands'
  mode: single
  variables:
    command: "{{ trigger.payload | lower }}"
  trigger:
    - platform: mqtt
      topic: home-assistant/cmd
  condition:
    - condition: template
      value_template: "{{ command in ['reboot', 'shutdown'] }}"
  action:
    - service: "hassio.host_{{ command }}"
      data: {}
2 Likes