[solved] Notify only people present in the house

Hi!

I’m working on automating notifications when store’s door opens:

  • I want to get Xiaomi’s Gateway to ring and
  • send telegram messages

What I’ve got right now is this:

- alias: door notification
  initial_state: True
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_xxxx
    from: 'off'
    to: 'on'
  condition:
    - condition: state
      entity_id: group.all_devices
      state: 'home'
  action:
    - service: notify.telegram_one
      data:
        message: "Door open"
    - service: notify.telegram_two
      data:
        message: "Door open"
    - service: xiaomi_aqara.play_ringtone
      data:
        ringtone_id: 10
        ringtone_vol: 70
        gw_mac: xxxxxx

Which works great for messaging telegram_one and telegram_two when at least of us is around.

What I want to achieve is to telegram only the ones present, instead of notifying both always.

E.g.

  • if device.one is around then only message telegram_one,
  • if both device.one and device.two are around then message both

I can create different automation for those or build it using appdaemon, but I’s curious whether there’s a solution with one automation.

Thanks!

Fist of all I’m noob. I would make 2 automations for each telegram. the condition also for specific device ‘home’
If condition both met the 2 automation will run to both telegram. if one met will run to one telegram.

In this case i’d have to create 3 automations, one for telegram_one, one for telegram_two and one for the xiaomi doorbell.

Yes this can be done but I’m wondering whether there’s a better way

The way I would do it is utilising a service_template under the action component. You’ll have to get rid of your condition. Have a look at the example on the templating page for more info:

Thanks for the suggestion @icaman004

I followed your advice, created a notify group with both telegram ids and now the automation is like this

- alias: door notification
  initial_state: True
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_xxxx
    from: 'off'
    to: 'on'
  condition:
    - condition: state
      entity_id: group.all_devices
      state: 'home'
  action:
    - service_template: >-
        {% if is_state("device_tracker.xxx", "home") and
              is_state("device_tracker.yyy", "home") %}
        notify.telegram_all
        {% elif is_state("device_tracker.xxx", "home") %}
        notify.telegram_one
        {% else %}
        notify.telegram_two
        {% endif %}
      data:
        message: "Door open!"
    - service: xiaomi_aqara.play_ringtone
      data:
        ringtone_id: 10
        ringtone_vol: 70
        gw_mac: xxxx
4 Likes

Great to hear it works! :slight_smile:

I’m also trying to get this to work but I have 3 devices to track if they are at home. I can make a big chunk of if statements but that not really flexible. Did someone in the meantime find a better solution to this problem?

I don’t think there isn’t any. The most elegant solution is to extend the group notification platform with some sort of filter option to include or exclude notification services based on conditions or template logic.

Just stumbled upon this thread for inspiration and would like to add:

There is now a “choose” action which works like IF/ELSE-IF/ELSE and can be used in automation actions, see https://www.home-assistant.io/docs/scripts/#choose-a-group-of-actions

It can even be configured in the automation UI editor.

This allows you to have choose conditions for every person with an action block that is only executed if the condition is met, e.g. if that person is home.

2 Likes

I can recommend iq_notify.
Search for it on github.
Using it for about a year now and I am pretty happy with it.

1 Like