Conditional notifications?

So like many people here I get all sorts of useful notificiations, Mostly via pushbullet, but a few via SMTP as well. I’m actually moving more and more to SMTP. Anyway, sometimes I’d like to turn off notifications for a while, maybe for a day, maybe just for the afternoon. Not on any specific schedule. I know I can turn off the automation, but I have several. Also, some noticiations go to multiple people, so maybe I dont want them, but my wife does.

I thought about making an input boolean called “Notification On/Off” and adding that as a condition, but just for the notification, not the whole automation. Since other things may still need to happen.

Has anyone done anything like this? Or have any recommendations on the best way to do this?

Your best option is to create a script that handles notifications. The script should except 2 inputs, title and message. Add a conditional check in the script that checks the input_boolean. Here’s an example:

alias: Notify Users
sequence:
  - condition: template
    value_template: "{{ is_state('input_boolean.nofity','on') }}"
  - service: notify.notify
    data_template:
      title: "{{ title }}"
      message: "{{ message }}"

Then inside your automations, you just need to swap your service from notify.nofity (or whatever it’s called) to script.notify_users.

Change this (your current automation):

  action:
    - service: notify.notify
      data:
        title: "Hi"
        message: "Hello"

To this

  action:
    - service: script.notify_users
      data:
        title: "Hi"
        message: "Hello"

Good idea, I do use scripts for some of my TTS notifications

Could I use a single script for this

  - service: notify.gmail_house_archive
    data:
      title: 'Driveway Line Crossing From the  Street'
      message: "{{ as_timestamp (now()) | timestamp_custom('%I:%M %p') }} on {{ now().strftime('%d %b %Y') }}" 
      data:
        images:
          - /tmp/driveway2.jpg

and

  - service: notify.pushbullet_notifications
    data_template:
      message: "Your commute from {{ states('input_text.commute_start_zone') }} to {{ states('device_tracker.paul_all') }} took {{ ((as_timestamp(now()) - (states('input_number.commute_start_time')|int))/60)|round }} minutes"
      target:
      - !secret pauls_secret_email
      title: "Commute Logged"

Or would you use two differnet scripts (one for attachments, and one without)

Well, you’d have to make a unique script for each combination of attributes you are passing to the script. So if the image always changes, you’d need one that supplies title, message, and image. Also, you’d have to reformat some of your code in the actions to be at the same level instead of indented.

data:
  title:
  message:
  data:
    images:
      - img

to

data_template:
  title:
  message:
  image:

and your script would have everything with the correct indents:

data_template:
  title: {{ title }}
  message: {{ message }}
  data:
    images:
      - {{ image }}
1 Like

Working great. One last question ( I tried this and was not successful).

Any way to pass either of these as well

    value_template: "{{ is_state('input_boolean.nofity','on') }}"
  - service: notify.notify

I tried to pass the service type as a variable, thought that way I could use one script for multiple notification types (or people). But to do so would require me to be able to change the notify and the boolean.

I played around with templates that were valid, but could not figure out how to send to script
like

{% set notifier = 'input_boolean.notify_' + user %}
"{{ is_state(notifier,'on') }}"

Probably a simplier question would be can I send the same data to two different scripts (well 95% the same, just differnent input boolean condtions and different notifiers

I found that I could use one script and just not include image: in the automation, it sends fine. Just leaves a message in the log about attachment not found. Maybe it’s the service I’m using (SMTP/GMAIL), but it works. Very nice. thanks for the recommendations

You can pass the service type too:

automation:

service: script.nofity_users
data_template:
  service: notify.notify
  title:
  message:
  image:

script

service_template: {{ service }}
data_template:
  title: {{ title }}
  message: {{ message }}
  data:
    images:
      - {{ image }}
2 Likes

Awesome thanks so much, after fully understanding I was able to add the condtion as well

  action:
  - service: script.sms_notify_with_images
    data_template:
      service: notify.sms_paul
      condition: "{{ is_state('input_boolean.notify_paul','on') }}"
      title: "Testing of iPhone Charging"
      message: "Pauls phone is on the charger"

and script

sms_notify_with_images:
  alias: SMS Notify with Images
  sequence:
  - condition: template
    value_template: "{{ condition }}"
  - service_template: "{{ service }}"
    data_template:
      title: "{{ title }}"
      message: "{{ message }}"
      data:
        images:
          - "{{ imagename }}"

Works perfect. Thanks Again, took several scripts down to one, and so much simpler. In my example I did not include an image, this still works for me using smtp and gmail

2 Likes

Nice solution, You just learned how to pass variables to scripts!

1 Like

Yes, I like this so I want to try it…

I want to pass the actual logical operator true or false. i.e the word true to represent the logical condition True and the word false to represent the logical condition False

Do I need to I enclose it in quotes? I’m pretty sure that for strings it doesn’t matter and in fact it is my understanding that the true and false will also be passed as strings anyway but will be interpreted as logical operators???

Is this…

action:
  - service: script.notify
    data_template:
      debug: "true"
      message: blah blah

the same as…

action:
  - service: script.notify
    data_template:
      debug: true
      message: blah blah
  - service: script.notify
    data_template:
      debug: True
      message: blah blah

That should work. The yaml interpreter should turn that into a boolean. Needs to be capital T, don’t use quotes because then the interpreter might assign it as a string.

Capital T. Thanks I wouldn’t have thought of that!!

One more thing…

the receiving script then passes on the boolean again so in this example does the debug: have quotes or not (I’m guessing not but being a template I am not sure).

  notify:
    sequence:
      - service: script.ha_notify_debug
        data_template:
          debug: "{{ debug | default(False) }}"
          message: "{{ message }}"

or

  notify:
    sequence:
      - service: script.ha_notify_debug
        data_template:
          debug: {{ debug | default(False) }}
          message: "{{ message }}"

Needs to be in quotes.

So single line templates need to be in quotes:

  single_line_template: "{{ blah }}"

multiline tempaltes do not, but you need a carrot to tell it that it’s multiline:

  multiline_template: >
    {{ blah }}
1 Like

Thanks!!!

1 Like