Template in the action block (send dynamically) notification

Perhaps someone can help me. I am currently trying to create an action in which a notification is only sent to a device that is currently at home.

I tried the following action, but unfortunately it doesn’t work. What am I doing wrong?

action:
  - repeat:
      for_each: >
        {{ states.person
           | selectattr('state','eq','home')
           | map(attribute='entity_id')
           | list }}
      sequence:
        - service: notify.mobile_app_{{ repeat.item.split('.')[1] }}_iphone
          data:
            message: "Bewegung erkannt."

Can someone please help me?

Many thanks in advance.

looks fine, but that doesn’t guarantee the user used their name as the phone name.

There’s a script Blueprint to help you create a script that you can call from automations to do what you are trying to do, but it suffers from the same possible point of failure Petro mentioned above.

Regarding Petro’s comment, you may want to consider setting up a Notify group for each person so that the object ID of the person entity matches that of the notify action… it makes this kind of thing easier.

https://community.home-assistant.io/t/create-a-group-of-devices-to-notify-people-that-are-at-home/749319/2

First of all thank you very much for your help but I’m sorry I can’t get it to work. :frowning:

My configuration yaml:

  - name: Sascha
    platform: group
    services:
      - action: mobile_app_iphone_16_von_sascha

My automation:

alias: Dynamische Benachrichtigung nur an Anwesende
description: Sendet eine Nachricht nur an Personen, die zuhause sind
triggers:
  - entity_id: binary_sensor.bewegung_flur
    to: "on"
    trigger: state
conditions:
  - condition: template
    value_template: |
      {{ states.person
         | selectattr('state','eq','home')
         | list
         | count > 0 }}
actions:
  - repeat:
      for_each: >
        {{ states.person | selectattr('state', 'eq' ,'home') | map(attribute =
        'name' ) | list | slugify }}
      sequence:
        - data:
            message: Bewegung erkannt.
          action: notify.mobile_app_{{ repeat.item.split('.')[1] }}
mode: single

What i need to change?

Thanks

You need to use the map function to iterate slugify over all the list items. I would also just set the names list to a variable, so you don’t have to run essentially the same query multiple times:

alias: Dynamische Benachrichtigung nur an Anwesende
description: Sendet eine Nachricht nur an Personen, die zuhause sind
triggers:
  - entity_id: binary_sensor.bewegung_flur
    to: "on"
    trigger: state
variables:
  names: |
    {{ states.person | selectattr('state', 'eq' ,'home') 
    | map(attribute = 'name' ) | map('slugify') | list }}
conditions:
  - condition: template
    value_template: "{{ names | count > 0 }}"
actions:
  - repeat:
      for_each: "{{ names }}"
      sequence:
        - data:
            message: Bewegung erkannt.
          action: notify.{{ repeat.item }}
mode: single
1 Like

OK, I did exactly as you suggested, but the action notify.mobile_app_sascha is being called, which does not seem to exist.

Logger: homeassistant.components.automation.dynamische_benachrichtigung_nur_an_anwesende
Quelle: helpers/script.py:2098
Integration: Automation (Dokumentation, Probleme)
Erstmals aufgetreten: 09:16:49 (4 Vorkommnisse)
Zuletzt protokolliert: 09:27:03

Dynamische Benachrichtigung nur an Anwesende: Repeat at step 1: Error executing script. Service not found for call_service at pos 1: Action notify.mobile_app_sascha not found
Dynamische Benachrichtigung nur an Anwesende: Error executing script. Service not found for repeat at pos 1: Action notify.mobile_app_sascha not found

Also the value_template: "{{ names | count > 0 }} shows "False" even though we are both at home:

variables:
  names: |
    ['sascha', 'shi']
conditions:
  - condition: template
    value_template: "False"

Did I perhaps do something wrong when creating the notify group?

Sorry, the action from your original post needed to have the “mobile_app” part removed. I have corrected it in my post above.

Great, it’s working perfectly now. Thank you very much for your help and have a nice weekend.