How to only send notification when device is at home?

How can I setup notifications so that only the people that are currently at home receives them?

I got a bunch of notifications setup that are sent out to everyone in the household. But there’s no point in getting a notification reminder that the washing machine is done if you’re currently at work and won’t be home for hours.

So how should I configure the notification so that only devices that are “at home” receives them? Currently I have not created any “Persons” in Home Assistant, not sure if that’s a requirement?

We don’t have any personal tags or anything, so our phones’ locations would be the indicator if we’re at home or not.

This is a sample notification, notify.ios_all_iphones is a group containing all phones.

- id: 'notification_tumble_dryer_stopped'
  alias: 'notification_tumble_dryer_stopped'
  trigger:
    - platform: state
      entity_id: sensor.tumble_dryer
      from: 'Running'
      to: 'Done'
  action:
    service: notify.ios_all_iphones
    data:
      message: The tumble dryer is done.
      data:
        push:
          badge: 0
          category: notification_tumble_dryer_stopped

And is it possible to postpone a notification until I’m home? Using the sample above, let say the tumble dryer is finished while I’m away, so I don’t want to receive a notification. But once I arrive home, and if the tumble dryer is still in state ‘Done’, can I then receive a notification telling me that it still isn’t taken care of?

2 Likes

So how should I configure the notification so that only devices that are “at home” receives them?

What you can do is use a repeat loop for every phone in your group “notify.ios_all_iphones”.
Something like (untested code):

      - variables:
          phone_list: "{{ expand('group.ios_all_iphones') }}"
      - repeat:
          count: "{{ phone_list | count }}"
          sequence:
            - variables:
                entity: "{{ phone_list[repeat.index-1] }}"
            - condition: template
              value_template: "{{ is_state(entity, 'home') }}"
            - service_template: "{{ 'notify.' + entity | string }}"
              data:
                push:
                     .....

And is it possible to postpone a notification until I’m home?

Well, that’s a bit more complicated.
I personally have implemented this in a very custom way of using custom_integration variables, giving every notification a timestamp of valid_until and writing it to a variable entity holding all missed notifications.
When I am back home an automation loops through all of the missed notifications.

Alternatively you could create a script to notify a specific mobile and loop within that script until the phone is back home… never the less - this is very error prone as your script will end after a restart (and I don’t have in my mind at the moment if there is a timeout for scripts, too - but pretty sure it is)

A third alternative would be to write your notifications as options into a input_select an loop through them as soon as somebody comes back home…That I think would be the best option in regards to “stability VS. complexity”

I have it done in a probably more cumbersome way with a service template … but it was all I knew at the time and it has worked well for me…. There is a notify group for each possible combination… because obviously I don’t care if the laundry is done when I’m not at home… but I also have a repeating alert so if we don’t move it… so once someone is home they will then get that alert the next time it goes off


   - data:
       message: "The Laundry is Done, please move clothes to dryer"
     service: >
        {% if is_state("person.brian", "home") and is_state("person.amy", "home") and is_state("person.joann", "home") %}
        notify.amy_brian_joann_notify
        {% elif is_state("person.brian", "home") and is_state("person.amy", "home") %}
        notify.amy_brian_notify
        {% elif is_state("person.amy", "home") and is_state("person.joann", "home") %}
        notify.amy_joann_notify
        {% elif is_state("person.joann", "home") and is_state("person.brian", "home") %}
        notify.brian_joann_notify
        {% elif is_state("person.joann", "home") %}
        notify.mobile_app_joann_s_iphone
        {% elif is_state("person.brian", "home") %}
        notify.brian_phone_macbook
        {% elif is_state("person.amy", "home") %}
        notify.mobile_app_amys_iphone
        {% else %}
        notify.amy_brian_joann_notify
        {% endif %}

Thanks for both of your input!

I kind of used your solution but I created an AppDaemon app that listen for events instead. This way the automation code could stay more or less the same, I just forward the notify integration’s data object to the AppDaemon app.

So to send notifications only to persons in the defined group all that are in the zone home I do like his:

- id: 'notification_tumble_dryer_stopped'
  alias: 'notification_tumble_dryer_stopped'
  trigger:
    - platform: state
      entity_id: sensor.tumble_dryer
      from: 'Running'
      to: 'Done'
  action:
    event: 'notify.all@home'
    event_data:
      message: 'The tumble dryer is done.'
      data:
        push:
          badge: 0

The fact that person.state uses the friendly name of the current zone as state and not the zone’s entity_id made it a bit cumbersome to map from person to zone. But I’m able to notify members in groups in specific zones like this:

event: 'notify.all@home'         // to group all that are home
event: 'notify.all@not_home'     // to group all that are not at home
event: 'notify.parents@home'     // to group parents that are home 
event: 'notify.parents'          // to group parents regardless of zone
event: 'notify.children@school'  // to group children that are in zone school

An improvement would perhaps be to allow multiple zones like this parents@work1,work2 since I can only filter to one zone at the moment, and support not_xxx for all zones and not only home. E.g. notify.children@not_school would send to all in children currently not in zone school. But that was really out of scope from my requirements.

If anyone is interested in the AppDaemon app, let me know.

1 Like

hey martin, great automation. I am using a blueprint and I would like to notify ppl that are at home. Would the AppDaemon do the job as well ? Here is the Blueprint I am using: hass-blueprints/reminder_to_close_window_with_temp_sensor.yaml at 2e4a19ffb8aa4543059a640b2b79b97075386b05 · home-IoT/hass-blueprints · GitHub

This part would be the one to be modified:

# Send a notification to the mobile device. 
action:
- device_id: !input device_identifier
  domain: mobile_app
  type: notify
  message: 'Time to close {{trigger.to_state.attributes.friendly_name}}.'
  title: 'Close {{trigger.to_state.attributes.friendly_name}}!'

Thank you in advance

I haven’t used blueprints so I can’t really tell or test myself. But if the action can send a custom event I don’t see why it wouldn’t work.

Hello there :slight_smile: I’m interested in the Appdaemon App. Is there a Source Code in GitHub somewhere?

Thanks :slight_smile:

Hi, sorry for a (very) late reply. I never got around to move my Appdaemon apps to GitHub, it’s been long due. I’ve created a repository and added the notifier app there, feel free to use it.

GitHub: group_notifier

Since I posted the last time I’ve started to use blueprints, so I can verify that the app is compatible with blueprints.

One thing to note is that we only have iOS devices in our household and I *think* the app currently only support iOS notifications. If I remember correctly the format when sending a notification differs a bit between Android and iOS.

But if someone is willing to help out, it’s probably fairly simple to add Android support.

How can I setup notifications so that only the people that are currently at home receives them?

You could do it like this: add a helper that checks those devices that are at home, make a notification script, use the script in automation(s). Here are the building blocks
Add the helper: Hass → Settings → Devices & Settings → Helpers → Add helper → Template {} and add this into the Template section:

state: >
  {% set ns = namespace(home_users=[]) %}
  {% for state in states.device_tracker %}
    {% if state.state == 'home' %}
      {% set ns.home_users = ns.home_users + [ state.entity_id ] %}
    {% endif %}
  {% endfor %}
  {{ ns.home_users | join(',') }}

Add the script: Hass → Settings → Automations & Scenes → Add script → First give name “notify_home_users” to Entity ID and then click Edit in Yaml from the Hamburger menu and add

alias: Notify home users
sequence:
  - variables:
      title: "{{ title }}"
      message: "{{ message }}"
  - variables:
      home_users: "{{ states('sensor.devices_at_home').split(',') }}"
  - repeat:
      count: "{{ home_users | length }}"
      sequence:
        - service: >
            {% set user = home_users[repeat.index - 1] %} {% set username =
            user.split('.')[1] %} notify.mobile_app_{{ username }}
          data:
            title: "{{ title }}"
            message: "{{ message }}"
fields:
  title:
    description: Title of the notification
    example: High Electricity Price Alert
  message:
    description: Message content for the notification
    example: Please reduce consumption as electricity prices are high.
mode: single

Finally, use the script in your automations e.g.

alias: Send notification on high electricity price
description: Notify all users who are home about high electricity prices
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.nordpool_kwh_fi_eur_3_10_024
    attribute: current_price
    above: 15
action:
  - service: script.notify_home_users
    data:
      title: High Electricity Price
      message: High electricity price; please reduce consumption.
mode: single

Hope this helps!