Home Assistant Shutdown/Startup Notification - One Notification Automation

Good Day All,

I know this is most likely a simple and extremely noobie type question but after hours of reading different threads and Google’ing the heck out of this I can’t seem to get it sorted out.

What I’m trying to do, create a single automation to notify me when Home Assistant services are shutdown or startup.

Right now I have this;

- id: '1683137743046'
  alias: Notify (T+S) - Home Assistant Shutdown
  description: Telegram and SMTP notification of Home Assistant status
  trigger:
  - platform: homeassistant
    event: shutdown
  condition: []
  action:
  - service: telegram_bot.send_message
    data:
      message: '⚠️ Home Assistant Services: shutdown ⚠️'
  mode: single
- id: '1683141972262'
  alias: Notify (T+S) - Home Assistant Startup
  description: Telegram and SMTP notification of Home Assistant status
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - service: telegram_bot.send_message
    data:
      message: '✅ Home Assistant Services: started ✅'
  mode: single

And this works perfectly fine… However, as I develop my skill and learn more what I want is something like this;

- id: '1683137743046'
  alias: Notify (T+S) - Home Assistant Service Status
  description: Telegram and SMTP notification of Home Assistant status
  trigger:
  - platform: homeassistant
    event: shutdown
  - platform: homeassistant
    event: startup
  condition: []
  action:
  - service: telegram_bot.send_message
    data:
      message: '⚠️ Home Assistant Services: {{ trigger.event }}⚠️'
  mode: single

The above just sends a message – :warning: Home Assistant Services: :warning:

So I can’t get the variables to work. In this case, the event that was triggered.

Can this be done?

- id: '1683137743046'
  alias: Notify (T+S) - Home Assistant Service Status
  description: Telegram and SMTP notification of Home Assistant status
  trigger:
  - id: shutdown
    platform: homeassistant
    event: shutdown
  - id: started
    platform: homeassistant
    event: startup
  condition: []
  action:
  - service: telegram_bot.send_message
    data:
      message: '⚠️ Home Assistant Services: {{ trigger.id }}⚠️'
  mode: single

Thank you for the reply… I tried that as well. Telegram output is just :warning: Home Assistant Services: :warning:

Below is the trace output:

Executed: May 3, 2023 at 3:53:28 PM
Result:
params:
  domain: telegram_bot
  service: send_message
  service_data:
    message: '⚠️ Home Assistant Services:'
  target: {}
running_script: false
limit: 10

For some reason I can’t get the variable to show up on the output.

Yeah… so I’m dumb.

Maybe this brain fart will help someone else.

When testing this and click ‘Run’ from the Automation, since there is no actual event that happened or state being changed the “trigger.event” is blank.

DUH!

So once you actually restart Home Assistant services, I get the right notifications.

Sometimes you just have type it all out to figure it out.

Explained here: Testing your automation

An automation’s Run command only executes the automation’s actions, therefore the trigger object is undefined. Any references within actions to the trigger object will fail.

Similarly, if one defines any Trigger Variables, they’ll also be undefined if you attempt to test the automation using Run.

1 Like

This might also be of interest to you.

1 Like

For anyone that was following along and/or wanted to see the final YAML code for this;

alias: Notify (S) - Home Assistant Service Status
description: SMTP notification of Home Assistant service status
trigger:
  - platform: homeassistant
    event: shutdown
  - platform: homeassistant
    event: start
condition: []
action:
  - service: notify.gmail
    data:
      title: "⚠️HAOS ALERT: {{ trigger.description}}"
      message: "Home Assistant Service: {{ trigger.description }}"
mode: single

The output when the actual event triggers AND NOT using the script ‘RUN’ function, looks like this;

When HAOS is starting;

⚠️HAOS ALERT: Home Assistant stopping
Home Assistant Service: Home Assistant stopping

When HAOS is shutting down;

⚠️HAOS ALERT: Home Assistant starting
Home Assistant Service: Home Assistant starting

Using the ‘trigger.description’ variable perfectly provided everything I needed in one single line.

Again thanks to @123 for their help.

2 Likes