Add conditional notification to this automation

HI

using this automation on an input_select in my setup, i need to add an option to switch the notification on and off.

How can that be achieved? It has to be conditioned after the service_template, since the has to happen always.
Below i have made a separate automation for now, but somehow that doesnt trigger, though the input_booleans exist and switch in and off.

full automation:

  - alias: Activity selection
    id: '1511601478553'
    trigger:
      platform: state
      entity_id: input_select.activity
    action:
      - service_template: >
          {% if is_state ('input_select.activity','Opstart') %} script.opstart
          {% elif is_state ('input_select.activity','Aan de slag') %} script.aan_de_slag
          {% elif is_state ('input_select.activity', 'Home theater') %} script.home_theater
          {% elif is_state ('input_select.activity','Gym') %} script.gym
          {% elif is_state ('input_select.activity', 'Selamat makan') %} script.selamat_makan
          {% elif is_state ('input_select.activity', 'Uit huis') %} script.uit_huis
          {% elif is_state ('input_select.activity', 'Naar bed') %} script.naar_bed
          {% elif is_state ('input_select.activity', 'Opstaan') %} script.opstaan
          {% endif %}
      - delay: 00:00:02
      - service: python_script.summary
        data_template:
          event: "{{ trigger.event }}"
      - service: notify.notify
        data_template:
          title: "Selected Activity: {{states.input_select.activity.state}} 
                at {{ as_timestamp(states.input_select.activity.last_updated) 
                | timestamp_custom ('%H:%M') }}"
          message: > {{ as_timestamp(states.input_select.activity.last_updated) 
                | timestamp_custom ('%H:%M') }}:
            {% if is_state ('input_select.activity','Opstart') %} Starting up
          {% elif is_state ('input_select.activity','Aan de slag') %}All set and ready to be productive!
          {% elif is_state ('input_select.activity', 'Home theater') %} Enjoy!
          {% elif is_state ('input_select.activity','Gym') %} Be fit!
          {% elif is_state ('input_select.activity', 'Selamat makan') %} Selamat Makan, maak er wat van!
          {% elif is_state ('input_select.activity', 'Uit huis') %} See you soon, the house is safely lit.
          {% elif is_state ('input_select.activity', 'Naar bed') %} Turned down the lights, sleep tight.
          {% elif is_state ('input_select.activity', 'Opstaan') %} Espresso is klaar over 10 minuten!
          {% endif %} 

this works and sends the correct notifications.

i was hoping an input-boolean.activity_notification could be made, but im uncertain where and how to use that in this automation.

separate for notification based on input_boolean, working fine also:

  - alias: Notify activity selection
    id: '1511601478554'
    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      condition: state
      entity_id: 'input_boolean.activity_notification'
      state: 'on'
    action:
      - service: notify.notify
        data_template:
          title: "Selected Activity: {{states.input_select.activity.state}}"
          message: >
            {{ as_timestamp(states.input_select.activity.last_updated) 
                | timestamp_custom ('%H:%M') }}:
            {% if is_state ('input_select.activity','Opstart') %} Starting up
            {% elif is_state ('input_select.activity','Aan de slag') %} All set and ready to be productive!
            {% elif is_state ('input_select.activity', 'Home theater') %} Enjoy!
            {% elif is_state ('input_select.activity','Gym') %} Be fit!
            {% elif is_state ('input_select.activity', 'Selamat makan') %} Selamat Makan, maak er wat van!
            {% elif is_state ('input_select.activity', 'Uit huis') %} See you soon, the house is safely lit.
            {% elif is_state ('input_select.activity', 'Naar bed') %} Turned down the lights, sleep tight.
            {% endif %}

input_boolean:

input_boolean:
  activity_notification:
    name: Notify activity selection
    initial: on
    icon: mdi:message-bulleted

Please have a look?
Thanks!
Marius

I’m not sure I understand. Do you mean you want part of your actions to run all the time and some other actions to run only if the input_boolean is on?
If so you can simply add conditions in your actions after the initial list of actions that should run no matter what.
Hope this helps

hi thanks, yes indeed.

Would need the scripting to be happening always (service_template) and the notification only when boolean is on.
so i can just move that condition to the action service: notify?

If so, couldn’t i even compress that further since most of the code will be the same if, elif,endif…

some sort of table with activiy_select, script, notify?
Or would that overcomplicate things…

easiest way is to simply insert a condition after the scripting bit. the actions will run until the condition
If the condition does not return true, anything after won’t run

like this?:

  - alias: Activity selection
    id: '1511601478553'
    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    action:
      - service_template: >
          {% if is_state ('input_select.activity','Opstart') %} script.opstart
          {% elif is_state ('input_select.activity','Aan de slag') %} script.aan_de_slag
          {% elif is_state ('input_select.activity', 'Home theater') %} script.home_theater
          {% elif is_state ('input_select.activity','Gym') %} script.gym
          {% elif is_state ('input_select.activity', 'Selamat makan') %} script.selamat_makan
          {% elif is_state ('input_select.activity', 'Uit huis') %} script.uit_huis
          {% elif is_state ('input_select.activity', 'Naar bed') %} script.naar_bed
          {% elif is_state ('input_select.activity', 'Opstaan') %} script.opstaan
          {% endif %}
      - delay: 00:00:02
      - service: python_script.summary
        data_template:
          event: "{{ trigger.event }}"
      condition:
      condition: state
      entity_id: input_boolean.activity_notification
      state: 'on'
      action:
      - service: notify.notify
        data_template:
          title: "{{ as_timestamp(states.input_select.activity.last_updated) 
                | timestamp_custom ('%H:%M') }} - Selected Activity: {{states.input_select.activity.state}}"
          message: > 
            {% if is_state ('input_select.activity','Opstart') %} Starting up
            {% elif is_state ('input_select.activity','Aan de slag') %} All set and ready to be productive!
            {% elif is_state ('input_select.activity', 'Home theater') %} Enjoy!
            {% elif is_state ('input_select.activity','Gym') %} Be fit!
            {% elif is_state ('input_select.activity', 'Selamat makan') %} Selamat Makan, maak er wat van!
            {% elif is_state ('input_select.activity', 'Uit huis') %} See you soon, the house is safely lit.
            {% elif is_state ('input_select.activity', 'Naar bed') %} Turned down the lights, sleep tight.
            {% endif %}

yes, though you use - at the beginning of each line, so I’d do the same for the condition.
also make sure it’s correctly indented. hass --script check_config will tell you if there’s an error

1 Like

thx!

just to be complete, or have an other perspective:
it would be wiser or possible to add an extra if, to each line in the first service template?

like:

{% if is_state (‘input_select.activity’,‘Opstart’) %} script.opstart
{% if is_state (input_boolean.notify_activity’, ‘On’) %} notify. notify etc etc?

Personally I would not, it requires more maintenance, if for any reason you change your condition or want to add another one on top, you’ll have to duplicate your efforts.
It’s easier to just add a condition indented in your actions.
I personally find that it’s also easier to read, meaning easier to debug should anything go wrong.
but that’s just my personal view :slight_smile:

1 Like

Agreed! thanks.