Conditional Notifications

Does the Notify Group component support conditions?

The below are just theoretical, and assume you have the notification services enabled and working.

One example would be, if the TV is on, send notifications there, otherwise send them to pushover:

notify:
  - name: carrier pigeon
    platform: group
    services:
        condition:
          condition: or
          conditions:
          - condition: state
            entity_id: 'media_player.tv'
            state: 'on'
            - service: nfandroidtv
          conditions:
          - condition: state
            entity_id: 'media_player.tv'
            state: 'off'
            - service: pushover

Another example would be, send notifications to html5 if the user is home, otherwise send them to ios:

notify:
  - name: carrier pigeon
    platform: group
    services:
        condition:
          condition: or
          conditions:
          - condition: state
            entity_id: 'device_tracker.iphone'
            state: 'home'
            - service: html5
          conditions:
          - condition: state
            entity_id: 'device_tracker.iphone'
            state: 'away'
            - service: ios

I don’t expect the above configurations to work, I was just wondering if something like this was possible?

Maybe this is what you looking for? https://home-assistant.io/getting-started/scripts-service-calls/#use-templates-to-decide-which-service-to-call

~Cheers

So in the example on that page, would you call the service_template from the notify group component above? Seems kind of counterproductive.

Would be nice if some kind of notification would just have to call 1 notification platform, then that platform was able to determine where to send the notification.

I think this is what I am looking for as I am doing this with two separate automations; but how does this look in the context of an actual automation? Do you have an example you are using that I could look at?

@rpitera

action:
  - service_template: >
      {%- if OnOff == "on" -%}
        input_boolean.turn_on
      {%- else -%}
        input_boolean.turn_off
      {%- endif -%}
    data_template:
      entity_id: >
        {%- if Stuff == "zzz" -%}
          input_boolean.zzz
        {%- elif Stuff == "yyy" -%}
          input_boolean.yyy
        {%- elif Stuff == "xxx" -%}
          input_boolean.xxx
        {%- endif -%}

That’s from an apiai Action but it should work the same in a automation / script. I give it “on” or “off” in OnOff and “yyy”, “zzz” or “xxx” in Stuff.

~Cheers

1 Like

Duh. I feel like I should have been able to figure that out on my own but thanks for showing me visually!

I’ve got a number of automations I can rewrite and edit out now. I’ve been doing this the dumb way by creating an automation for home and one for away. Talk about doing it the hard way…

Thanks so much!

@PhyberApex Would this action you posted apply to different notification services as well (not just input_boolean’s)?

You could use this to call ANY service inside of HA so yes.

~Cheers

Maybe I’m still confused. So would a notification look like this:

notify:
  - name: carrier pigeon
    platform: group
    services:
    - service_template: >
      {%- if device_tracker.iphone == "home" -%}
      - service html5
      {%- else -%}
      - service ios
      {%- endif -%}

Not sure where the data_template would fall into.

data template would come in if you wanted to send data as a part of the notification message, like if a door was open, which door it was.

It’s totally optional.

Okay maybe we misunderstood each other. I don’t think you can use the service template in a notify group. You would have to make multiple groups like “home” and “away” and then instead of calling the notify platform you would call a script which calls the right group with a service_template.

~Cheers

Your solution might work, I’m just not sure how I would use it.
Ideally I’d like a notification, of any kind to go to a specific notification service. Then have that service figure out where to send it, based on some conditions. I thought the notification group might be an option because it supports multiple notification services.

The group does support multiple but it’s a all or nothing kinda deal. So you are better of with a script if that’s what you are looking for.

~Cheers

I do something similar. I have a package “notify” that contains my notification logic.

It is a simple script, which accepts the notification text as a parameter. The main idea is that from it you call as many other scripts as notification services you have, each with their own condition logic:

script:

  notify:
    sequence:

      # Read notification via TTS
      - service: script.turn_off
        data:
          entity_id: script.notify_voice
      - service: script.notify_voice
        data_template:
          notify_text: "{{ notify_text }}"

      - wait_template: "{{ states.script.notify_voice.state == 'off' }}"

      # Send a notification via telegram

      - condition: template
        value_template: '{{ notify_mobile if notify_mobile else False}}'

      - service: notify.telegram_notifications
        data_template:
          message: "{{ notify_text }}"

Note it’s work in progress, but you can extend it to as many notification mechanisms as you want. You can find the whole file here: https://github.com/jjmontesl/home-assistant-config/blob/master/config/packages/notify.yaml

2 Likes

Hmm I’ve never seen “wait_template” where did you find that? Is that a custom component or is it just not documented at all?

~Cheers

New to me too. I don’t see any mention of it in the Jinja docs.

1 Like

Sorry. It’s from the dev version, wait_template allows you to stop a script until a condition is met.

You can replace with a delay for the moment. Also, next version should be out this weekend, which will include that I believe.

2 Likes

Neat. I thought I had been following most of the PRs but I missed that one!

Hi,

is “wait_template” included in 0.40? I see it in the documentation for Scripts, but when I add it to my config, config validation fails with the error: “Invalid config for [script]: [wait_template] is an invalid option for [script]”.

Thanks

Scratch that; my bad

:blush: