Blog

Home Assistant

A Foundational Home Assistant Script

Welcome back to my series on the foundational configurations that form the bedrock of my Thinking Home. In the last post, we tamed the chaos of network reboots. Today, we are bringing that same principle of centralized control and order to another fundamental part of any smart home: notifications. In our homes, as in our lives, clear and orderly communication is key to peace and good management.

Scattered and Inconsistent Notifications

As your Home Assistant setup grows, so does the number of automations that need to send alerts. You might have one automation that sends a simple text message when a door opens, another that sends a camera snapshot when motion is detected, and a third that alerts you to a water leak.

Before long, your automations are littered with dozens of different notify service calls. If you want to change how you’re notified—maybe add your spouse to the alerts, or switch to a new phone—you have to hunt down and edit every single automation. It’s inefficient, error-prone, and a major headache to maintain. What is needed is a single, central service to handle every notification.

A Universal “Notify All” Script

To solve this, I created a universal notification script. Instead of calling notify.mobile_app_your_phone in an automation, I now make a single call to script.notify_all.

This script acts as a central hub. It takes a title, a message, and an optional photo, and then takes on the complex work of formatting and distributing that notification to multiple people on multiple devices. It’s a “fire-and-forget” system that dramatically cleans up my automations and makes my notification strategy incredibly easy to manage.

How It Works

This script is more than just a simple forwarder; it’s packed with intelligence to handle the complexities of modern notifications.

  1. A Single Point of Entry: The script accepts three parameters: title, message, and an optional photo. Every other automation simply calls this one script, passing in the necessary data.
  2. Smart Photo Handling: This is the script’s most powerful feature. You can provide a photo in two ways: as a full URL from the web, or as a local path in your Home Assistant’s /www/ directory (e.g., /local/camera_snapshots/front_door.jpg). The script automatically detects which you have provided and constructs the correct URLs for both internal and external access. This is critical for ensuring camera snapshots show up in push notifications when you are away from home.
  3. Multi-User, Multi-Platform: The script is designed to send notifications to everyone who needs them. It uses simple lists in the variables section, so you can easily add all your family’s Android and iOS devices. It correctly formats the payload for each platform, as they handle image attachments differently.
  4. Built-in Debugging: A simple input_boolean toggle activates a powerful debug mode. When turned on, the script creates detailed persistent notifications that show you exactly what data it received and what URLs it generated. This makes troubleshooting issues like a broken image path incredibly simple.

How to Implement This in Your Home Assistant

This script is designed to be the central pillar of your notification system. With a few helpers and small customizations, it can be yours.

You can get the complete, up-to-date code from my GitHub repository:

View the Notify All Script on GitHub

To get it running, follow these steps:

1. Create the Required Helpers: This script relies on three helpers. You can create these via Settings > Devices & Services > Helpers or by adding them to your configuration.yaml.

  • An input_boolean to control the debug mode.
  • Two input_text helpers to store your internal and external Home Assistant URLs. This is what allows the script to build the correct image links.
# In your configuration.yaml
input_boolean:
  debug_notify_all:
    name: "Debug Notify All Script"
    icon: mdi:bug

input_text:
  home_assistant_base_url:
    name: "Home Assistant Base URL"
    initial: "http://YOUR_HA_IP:8123" # e.g., http://192.168.1.100:8123
  home_assistant_external_url:
    name: "Home Assistant External URL"
    initial: "https://YOUR_NABU_CASA_OR_DUCKDNS_URL"

2. Configure the Script Variables: Open the script’s YAML file. All the settings you need to change are grouped together at the top of the variables section.

  • android_devices & ios_devices: Add your family’s notification services to these lists.
  • ha_base_url_entity, ha_external_url_entity, debug_mode_entity: Ensure these match the helpers you created in step 1.

3. Using the Script in Your Automations: Now for the fun part! Go into any automation where you want to send a notification and replace the notify service call with a call to this script.

Example: Simple text notification

- action:
  - service: script.notify_all
    data:
      title: "Garage Door"
      message: "The garage door was just opened."

Example: Notification with a camera snapshot

- action:
  - service: script.notify_all
    data:
      title: "Motion Detected!"
      message: "Motion detected at the front door."
      photo: "/local/camera_snapshots/front_door_latest.jpg"

By channeling all alerts through this single, intelligent script, you make your entire Home Assistant configuration cleaner, more robust, and far easier to maintain. It is a foundational element that elevates your smart home from a collection of individual automations to a cohesive, manageable system.

Did you find this post helpful? Have you implemented something similar or have ideas to improve on this script? If so, let me know in the comments below or on GitHub. I may feature your changes in a future update!

Leave a Reply