How to do automation: publish mqtt message on shutdown / disconnect / restart

Mqtt configuration:

mqtt:
  broker: ip
  birth_message:
    topic: 'service/irbridge1/service/connection'
    payload: '1'
    retain: True
  will_message:
    topic: 'service/irbridge1/service/connection'
    payload: '0'
    retain: True

I notices on restart or shutdown it does not update with the will_message, so i tried this:

  - id: mqtt_on_exit
    trigger:
      platform: event
      event_type: homeassistant_stop
    action:
      service: mqtt.publish
      data:
        topic: 'service/irbridge1/service/connection'
        payload: '0'
        retain: True

and this:

  - id: mqtt_on_exit
    trigger:
      platform: homeassistant
      event: shutdown
    action:
      service: mqtt.publish
      data:
        topic: 'service/irbridge1/service/connection'
        payload: '0'
        retain: True

This does not seem to work, either way.

Please point me in the right direction.

Thank you kindly

I use something like this

automation restart:
  alias: HA started
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: automation.restart
    to: 'on'
  action:
    - service: mqtt.publish
      data:
        topic: 'service/irbridge1/service/connection'
        payload: '0'
        retain: True
    - service: automation.turn_off
      entity_id: automation.restart
1 Like

Tried with entity_id: automation.stop / .shutdown and it doesn’t seem to work (config check failure).

I need to catch the stop/restart event when triggered from “outside” home assistant - like when the machine is rebooted.