WTH Notify only devices at home requires a lot of code

I would like to have a notify group for devices in a zone like home or devices outside a zone.
My use case is door ring: Only phones at home shall show, that someone rings the door.

see code snipped below.

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.json.event == \"incoming_call\" }}"
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state('person.casastroeh', 'home') }}"
                sequence:
                  - service: notify.mobile_app_oneplus_a6003
                    data:
                      title: Door Ring
                      message: Ring ring!
                      data:
                        notification_icon: mdi:lock-open-variant
                        tag: door-ring-detected
                        timeout: 60
                        actions:
                          - action: OPEN_DOOR
                            title: Open door
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state('person.jannes', 'home') }}"
                sequence:
                  - service: notify.mobile_app_jannes_handy
                    data:
                      title: Door Ring
                      message: Ring ring!
                      data:
                        notification_icon: mdi:lock-open-variant
                        tag: door-ring-detected
                        timeout: 60
                        actions:
                          - action: OPEN_DOOR
                            title: Open door
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state('person.pia', 'home') }}"
                sequence:
                  - service: notify.mobile_app_pia_moto_g_30
                    data:
                      title: Door ring
                      message: ring ring
                      data:
                        notification_icon: mdi:lock-open-variant
                        timeout: 60
                        tag: door-ring-detected
                        actions:
                          - action: OPEN_DOOR
                            title: Open door
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ is_state('person.anja', 'home') }}"
                sequence:
                  - service: notify.mobile_app_anja_handy_moto_g8
                    data:
                      title: Door ring
                      message: ring ring
                      data:
                        notification_icon: mdi:lock-open-variant
                        timeout: 60
                        tag: door-ring-detected
                        actions:
                          - action: OPEN_DOOR
                            title: Öffne Tür

While I would love to see this be easier to set up for new users, it is possible to reduce that automation considerably. The crux may be related to the fact that Zones track person enties but person has no relationship with notification device.

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.json.event == \"incoming_call\" }}"
        sequence:
          - repeat:
              for_each: >
                {{ expand('device_tracker.mobile_app_anja_handy_moto_g8',
                'device_tracker.mobile_app_oneplus_a6003', 'device_tracker.mobile_app_jannes_handy',
                'device_tracker.mobile_app_pia_moto_g_30') | selectattr('state', 'eq', 'home') 
                | map(attribute='entity_id') | list }}
              sequence:
                - service: notify.{{ (repeat.item).rsplit('.')[1] }}
                  data:
                    title: Door Ring
                    message: Ring ring!
                    data:
                      notification_icon: mdi:lock-open-variant
                      tag: door-ring-detected
                      timeout: 60
                      actions:
                        - action: OPEN_DOOR
                          title: Open door

EDIT: Removed extraneous mobile_app pointed out by TheFes

1 Like

Hi, there is a blueprint for this (disclaimer, I haven’t tried it yet) which should cut down your maintenance

Nice, although your template for the service is incorrect. It results in notify.mobile_app_mobile_app_something

You could just map the object_id and use that in the service call template:

          - repeat:
              for_each: >
                {{ expand('device_tracker.mobile_app_anja_handy_moto_g8',
                'device_tracker.mobile_app_oneplus_a6003', 'device_tracker.mobile_app_jannes_handy',
                'device_tracker.mobile_app_pia_moto_g_30') | selectattr('state', 'eq', 'home') 
                | map(attribute='object_id') | list }}
              sequence:
                - service: notify.{{ repeat.item }}
1 Like

Good catch! That extra mobile_app was necessary based on the entity ids of device tracker I was testing with, but it would have been an issue for OP.

Building on the above, if your mobile app and person has the same name (e.g. person.jason and mobile_app_jason) this works too:

for_each: >
  {{ states.person | selectattr('state','eq','home') | map(attribute='entity_id') | list | replace('person.','mobile_app_') }}
sequence:
  - service: notify.{{ repeat.item }}
    data:
      message: ""

You can’t use replace on a list. It can only be used on a string.
But you can just map the object id and use that

for_each: >
  {{ states.person | selectattr('state','eq','home') | map(attribute='object_id') | list }}
sequence:
  - service: notify.mobile_app_{{ repeat.item }}
    data:
      message: ""
1 Like