How To: Notifications Based on Who is Home/Current Occupants

Hey everybody!

I scanned through a ton of threads in search of an answer to how I could deliver notifications via an automation to only current occupants of my home (i.e. only deliver a notification to a person when that person’s state = home). My use case is notifications about laundry (and this seems to be a pretty common use case!); I want everyone at home to get notifications that the laundry cycle is complete, but I don’t need anyone who is not home to be blown up with these notifications if I’m on a cleaning spree!

Everything I found involved one of the following solutions, each of which I thought was a bit cumbersome or simply unnecessary:

  • Installing a 3rd party integration
  • Building additional logic into each automation that require these dynamic notifications with an additional IF statement for each person entity to check if they are home or not (WAAAAAAY too high maintenance)
  • Using notify groups to be called based on a use case or conditions (better, but still high maintenance)
  • Building the notification to trigger only when everyone is home or no one is home (defeats the purpose)
  • Generally leading to a lot of maintenance overhead

So I decided to take matters into my own hands and create custom notification ‘templates’ in the form of scripts that can be called by an automation. This solution leverages out of box functionality and no custom integrations. Here’s my script; feel free to steal the code!

alias: Notify Current Occupants
sequence:
  - repeat:
      for_each:
        - person: person.dylan
          notify_method: mobile_app_dylans_iphone_15_pro
        - person: person.michael
          notify_method: mobile_app_michaels_iphone_12_pro_max
      sequence:
        - if:
            - condition: template
              value_template: "{{ is_state(repeat.item.person, 'home') }}"
          then:
            - service: notify.{{ repeat.item.notify_method }}
              metadata: {}
              data:
                message: "{{message}}"
                title: "{{title}}"
                data: "{{data}}"
mode: parallel
icon: mdi:bell
max: 10
fields:
  title:
    selector:
      text: null
    name: Title
    description: Title/Header for your notification.
    required: true
  message:
    selector:
      text:
        multiline: true
    name: Message
    description: Message/Body for your notification.
    required: true
  data:
    selector:
      template: {}
    name: Data
    description: Data/Parameters for your notification.

The script uses a For Each repeat and cycles through the list of persons (and uses an additional variable for each person’s notification method). For each person in the list it checks their status and, if they’re home, fires off the notification. Variables allow you to pass the title, message, and data from the automation to the script for use in the notification.

In this example the list of persons is defined in the script. This works perfectly for my use case because I only have 5 residents to notify and only 3 different notification ‘templates’. But I’m sure this could be customized further by either using a template to populate the list with all person entities each time the script is executed, or by setting up a group of persons and having the script call the group on each run. Heck, you could probably even use a value template to keep the group constantly updated.

I was ecstatic when I figured this out last night and wanted to share with the community. Feel free to use or modify my code. Certainly open to recommendations to make this more efficient!

2 Likes