If Statement in Automation Action with notify

I’m trying to set up home assistant to notify users only if an input boolean is switched on. I’m not sure how to format this information with an if statement. Ideas?

Have you tried a condition for your automation?

I’ve considered it, but it would require five automations to perform the same notify task, as the message can go to up to 5 different people. Unless…is there a way to put conditions within the action?

can you share your automation first along with a brief explanation of what you’re trying to achieve? It would be easier to give you constructive feedback and advice

So where it now says notify.ios_notify_fam I’d like to break up the family group, which has 5 members, so that any specific user can turn off notifications for themselves through the frontend. Not the best explanation but that’s it.

- alias: Alarm Armed Notification
  trigger:
    platform: state
    entity_id: 'alarm_control_panel.home'
    to: 'armed_away'
  action:
    - service: notify.ios_notify_fam
      data:
           message: 'System is now armed.'
    - service: tts.google_say
      entity_id: media_player.audio_system_speaker
      data:
        message: 'System is now armed'
    - service: lock.lock
      entity_id: lock.front_door_lock

thanks, that makes more sense. give me a couple of min to test a script for you

To reply to your earlier question, you can’t add conditions to your actions as it will stop as soon as one condition is false

Try this (not tested, and there may be indentation issues)
create an input_boolean for each family member.
Name it notification_-_ios_notify_fam (replace ios_notify_fam with the actual name for each member)
then try and replace your notify action:

d

with this:

- service_template: >
  {% for state in states.input_boolean %}
  {% if state.name.split('_-_')[1] is defined and state.name.split('_-_')[0] == 'notification'%}
  {% if state.state == 'on' %}
    notify.{{state.name.split('_-_')[1]}}
    data:
      message: 'System is now armed.'
  {% endif %}
  {% endif %}
  {% endfor %}

This assumes you have no other input_boolean named like notification_-_'anything'

1 Like

You could just reorganize this with a condtion in the sequence:

- alias: Alarm Armed Notification
  trigger:
    platform: state
    entity_id: 'alarm_control_panel.home'
    to: 'armed_away'
  action:
    - service: tts.google_say
      entity_id: media_player.audio_system_speaker
      data:
        message: 'System is now armed'
    - service: lock.lock
      entity_id: lock.front_door_lock
    - condition:
        condition: state
        entity_id: input_boolean.nofity_family
        state: 'on'
    - service: notify.ios_notify_fam
      data:
           message: 'System is now armed.'

My syntax might be off, may need to adjust the spacing and remove the second condition. I’m going off the top of my head.

Anyways, everything after the condition will only fire based on the condition. Everything before will work as expected.

I think he wants 5 notifications to fire independently depending on whether an input_boolean for each is enabled or not, so for example wife can turn input_boolean off to not receive notifications but the rest of the family members would still received notifications based on their individual input_boolean.
The least scriptic way of achieving this is 5 independent automations, but that’s a lot of duplication,hence his question.
I can’t think of a better way than a service_template and for loop to go through each input_boolean and fire the corresponding notification if input_boolean is on, but I agree it makes it complicated to write and very difficult to troubleshoot.
@lukers83 did you try? Did it work?

Ah yes, your method would work much better. To me, it seems a little work intensive though and a pain to read. He could simplify it by naming his booleans the same as his notify.ios_nofity_x then only have a for loop:

- service_template: >
    {% for state in states.input_boolean if state.state == 'on' and 'ios_nofity' in state.name %}
      notify.{{state.name}}
      data:
        message: 'System is now armed.'
    {% endfor %}

assuming they all have ios phones…

1 Like

oh that’s much better indeed :+1:
Although I’m no stranger to coding, I’m still a little bit new to for loops in jinja2 / yaml
Thanks for this

jinja is a pain, but it’s similar to python. There is so much crap you can do with looping, its insane. I’ve only learned it by helping other people here.

yeah that’s why I love this community :slight_smile:

also, if you didn’t know… I noticed you used list[1] to call out the second object in the list. You can also use list[-1] to get the last item in a list. Very nice functionality that isn’t present in most languages.

1 Like

I implemented this method and (after just testing in) none of the users got notifications. To recap, I have input booleans named things such as input_boolean.ios_lukes_iphone and have put this snippet of code in the action of the automation.

None when the boolean is on? Try it in the template section and see what the results are. We may need to segregate the service_template and the data_template, which is no big deal.

Change that to this:

- service_template:
    {% for state in states.input_boolean if state.state == 'on' and 'ios_nofity' in state.name %}
      notify.{{state.name}}
    {% endfor %}
  data:
    message: 'System is now armed.'

It will probably work, but it may require you to have at least one boolean on.

I won’t have access to my installation for a few days as I am traveling. However, I do recall the log stating that the service template was invalid. I will try this new one at my earliest chance.