Notification logic based on who is home

I’ve done some searching of the forums and found some things that do parts of this, but am having trouble seeing how to piece it all together.

I want to set up some notification logic and re-use it for a number of automations. I’d prefer to have this in one place but I am a total Home Assistant noob so I’m not even sure where to start (template? script?). The basic logic I’m looking for is:

  • If everyone is home, notify everyone.
  • If someone is away, notify only the person who is home.
  • If everyone is away, notify everyone.

Other details:

  • There are only 2 us in the house, if that simplifies things.
  • I’d like to notify both the iPhone and iPad of each person. (We’re both on iOS.)

Any ideas on the best way to accomplish this? Maybe my idea of having this logic in a central place is foolish and I should just build this out in one Automation and copy / paste to the others. But anytime I’m copy/pasting the same code to multiple place I start to think there must be a better way.

How about something like:

###scripts.yaml
notify_person_1:
  alias: "Notify Person 1"
  description: "Send a notification to Person 1's phone and iPad"
  fields:
    message:
      description: "The notification's message"
      example: "The windows are open"
  sequence:
  - service: notify.mobile_iphone_1
    data:
      message: '{{ message }}'
  - service: notify.mobile_app_ipad_1
    data:
      message: '{{ message }}'

notify_person_2:
  alias: "Notify Person 2"
  description: "Send a notification to Person 2's phone and iPad"
  fields:
    message:
      description: "The notification's message"
      example: "The windows are open"
  sequence:
  - service: notify.mobile_iphone_2
    data:
      message: '{{ message }}'
  - service: notify.mobile_app_ipad_2
    data:
      message: '{{ message }}'

household_notifier:
  alias: Household Notifier
  description: "Send a notification to everyone depending on their state"
  fields:
    message:
      description: "The notification's message"
      example: "The windows are open"
  sequence:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ 'home' in [ states('person.1'), states('person.2') ] }}"
          sequence:
            - choose:
                - conditions:
                    - condition: template
                      value_template: "{{ is_state('person.1', 'home') }}"
                  sequence:
                    - service: script.notify_person_1
                      data:
                        message: '{{ message }}'
                - conditions:
                    - condition: template
                      value_template: "{{ is_state('person.2', 'home') }}"
                  sequence:
                    - service: script.notify_person_2
                      data:
                        message: '{{ message }}'
      default:
        - service: script.notify_person_1
          data:
            message: '{{ message }}'
        - service: script.notify_person_2
          data:
            message: '{{ message }}'

You would need to add in any other notification variables that you use like title, channel, etc

I don’t have time to really dig into this right now (probably a poor time to start this thread :confused:), but at quick glance this seems to be exactly what I need as a starting point. Thank you!