Single trigger, multiple recipients with different conditions

Hi - I want to monitor a window and notify all family members at home to close it.
I did write an automation for one person (s. below) but it seems clunky to repeat that four times (number of people here). Especially since there are likely more applications with a similar problem.

Is there maybe some way to create a dynamic list of persons who satisfy certain conditions?

Thanks.

- id: "1645375982893"
  alias: Klofenster lange offen
  description: ""
  trigger:
    - type: opened
      platform: device
      device_id: 0cf51fc7a89c5c2cfefa1180544adf30
      entity_id: binary_sensor.klofenster
      domain: binary_sensor
      for:
        hours: 0
        minutes: 5
        seconds: 0
  condition:
    - condition: state
      entity_id: person.markus
      state: home
  action:
    - service: notify.mobile_app_hyrule
      data:
        message: Bitte Klofenster schliessen!
  mode: single

Hi!

Have you tried this? You can create notification groups.

Cheers,

Joaquín

It is possible to use templating to create a dynamic list of persons who meet a condition and then use notifier groups as Joaquin linked. However, the number of necessary notifiers grows exponentially with each additional person and becomes way more effort than necessary if you have more than 2 people.

Instead, use a series of indendent Choose actions. You can make it more universal/reusable by turning it into a script and having your automations pass the desired variables.

- id: "1645375982893"
  alias: Klofenster lange offen
  description: ""
  trigger:
    - type: opened
      platform: device
      device_id: 0cf51fc7a89c5c2cfefa1180544adf30
      entity_id: binary_sensor.klofenster
      domain: binary_sensor
      for:
        hours: 0
        minutes: 5
        seconds: 0
  condition: []
  action:
    - service: script.family_notifier
      data:
        message: 'Bitte Klofenster schliessen!'
        state: 'home'
  mode: single
###scripts.yaml

family_notifier:
  alias: Family Notifier
  description: "Send a notification to every family member who is in a specific state"
  fields:
    state:
      description: "The state person entities should be in to receive the notification"
      example: "home"
    message:
      description: "The notification's message"
      example: "Markus is leaving work"
  sequence:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ is_state('person.markus', state) }}"
          sequence:
            - service: notify.mobile_app_hyrule
              data:
                message: '{{ message }}'
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ is_state('person.2', state) }}"
          sequence:
            - service: notify.mobile_app_2
              data:
                message: '{{ message }}'
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ is_state('person.3', state) }}"
          sequence:
            - service: notify.mobile_app_3
              data:
                message: '{{ message }}'
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ is_state('person.4', state) }}"
          sequence:
            - service: notify.mobile_app_4
              data:
                message: '{{ message }}'
1 Like

Hi,
thanks for the answers and sorry for the late reply.
I’ve tried @Didgeridrew solution but it works only halfway. The automation calls the script but the script does not send the notifier.
It works only if I explicitly put the state and the message into the script like this:

family_notifier:
  alias: Family Notifier
  description: "Send a notification to every family member who is in a specific state"
  fields:
    state:
      description: "The state person entities should be in to receive the notification"
      example: "home"
    message:
      description: "The notification's message"
      example: "Markus is leaving work"
  sequence:
    - choose:
        - conditions:
            - condition: state
              entity_id: person.markus
              state: "home"
          sequence:
            - service: notify.mobile_app_hyrule
              data:
                message: "blabla"

Somehow the parameters are not transferred to the script.

?

It’s not because the variables aren’t being passed, it’s because I’m an idiot… I forgot that you can’t template a state condition like that, but you can do it as a template condition. I have updated my earlier post with the fix.

:grinning: - hey, no problem, thanks for the help.
But: there still seems to be something wrong:

  sequence:
    - choose:
        - conditions:
          - condition: template
            state: '{{ is_state('person.markus', state) }}'
          sequence:
            - service: notify.mobile_app_hyrule
              data:
                message: '{{ message }}'
    - choose:
        - conditions:

gives me ‘Missing property “sequence”.’ after the line ‘- conditions:’. Since there is a ‘sequence’ I’m a bit lost.

Double check your indents/spaces and quotation symbols.

They can get messed up copying and pasting in this forum system sometimes.

Sorry, I’m a bit lost. I checked all indentations and quotations but couldn’t find a wrong one.
This is a screenshot of what it looks like in VSCode in HA (sorry, can’t paste the error message):

Look’s like I should have quadruple-checked myself… state: should be value_template: in each of those conditions.

:man_facepalming:t3:

1 Like

:grinning_face_with_smiling_eyes:
Now it works - thank you for your persistance!