Conditions inside actions. How?

Hi.
I’m new, I started playing with HA about 10 days ago.
I flashed a sonoff rf bridge with tasmota and now I’m doing some test with a DW1 door contact.
It’s working perfectly, so I wanted to add some notifications.
I configured both telegram message notification and TTS speech notification towards my google home mini.
Both are working.

Then I created a switch to enable/disable notification (somthing like arming/disarming alarm).
By using a single switch (boolean_input). Using just one switch to activate/deactivate both the 2 action works perfectly.
I’d like to have 2 different switches, 1 for telegram notification and 1 for voice notification.
Here’s where I need you help…
I’ve been trying the whole afternoon and a t the end I got something working, but it’s not correct.
As it is, by triggering only the telegram notification, I get only telegram notification.
By triggering Voice only notification, I get both telegram message and voice.
I can accept this, but I’m trying to learn, so I’d like to know the correct way.

Cattura

configuration.yaml part is basic:

#Notifiche Sensori RF on/off    
input_boolean:
  notify_telegram:
    name: Notifiche Telegram Allarme
    initial: off
    icon: mdi:alarm-light
    
  notify_voice:
    name: Notifiche Vocali Allarme
    initial: off
    icon: mdi:volume-high  

Automation part:

#Notifica via Telegram e TTS aperture contatti e presenze
- id: 'Contatto1'
  alias: Notifica Contatto1
  trigger:
  - entity_id: binary_sensor.contatto_apertura_1
    from: 'off'
    platform: state
    to: 'on'
  action:
    - condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.notify_telegram
          state: 'on'
    - service: notify.telegram_bot
      data:
        message: Il contatto di apertura 1 ha rilevato una apertura
        title: '*RILEVATO ACCESSO 1*'
    
    - condition: or
      conditions:
        - condition: state
          entity_id: input_boolean.notify_voice
          state: 'on'
    - service: tts.google_say
      entity_id: media_player.salotto
      data:
        message: 'Il contatto di apertura 1 ha rilevato una apertura'
        language: 'it'

I just would like that each switch activates its action.
Probably the easiest way would be splitting the automation in two parts, but I’d like to avoid that.
Thanks

IMO, it will be much easier to keep it two automations.

Yes, but I will do this for many sensors, and duplicating all automations is not probably the best way.
I’ll keep it like this until I found a different way…

Use a service template with elif statement for all different possible scenarios

this can be done easily, by creating 2 scripts, one for telegram, one for voice, which are called from the automation.

start each script with the condition the boolean has to be on, else do nothing.

- id: 'Contatto1'
  alias: Notifica Contatto1
  trigger:
    - platform: state
      entity_id: binary_sensor.contatto_apertura_1
      to: 'on'
  condition: []
  action:
    - service: script.telegram
    - service: script.voice


script:
  telegram:
    sequence:
      - condition: template
        value_template: >
          {{is_state('input_boolean.notify_telegram','on')}}
      - service: notify.telegram_bot
        data:
          message: Il contatto di apertura 1 ha rilevato una apertura
          title: '*RILEVATO ACCESSO 1*'

and a same one for voice. If this works, you could enhance this by passing data to the script, but please first start like this and confirm it is working.

1 Like

Once marius scripts above are working, you can do it with a single automation:

      - service: script.turn_on
        data_template:
           entity_id: >-
               {% if is_state('input_boolean.notify_telegram', 'on') %}
                   script.telegram
               {% elif is_state('input_boolean.notify_voice', 'on') %}
                   script.voice
               {% endif %}
1 Like

no i wouldn’t advice that, because that takes out the option for having them both on. The whole idea of the scripts is it takes out the conditions to the scripts, so the automation doesn’t stop after one negative evaluation. it evaluates all script conditions like this, and if both booleans are ‘on’ it runs both scripts.

If that’s not what the OP wants, another solution can be made, like yours of course, but I did believe OP wanted to have both options available.

2 Likes

First, really thanks to everybody.
I didn’t expect so much kind help, if everybody is like you, this has to be a great community.

I followed Mariusthvdb method and it’s working perfectly.
He’s right, what FunkyBoT suggested is not what I tried to realize, because I need the possibility to have both the option enabled, but it’s really appreciated because it helps me to learn how to do things. It’s the first “if - else” that I see in HA and it will become useful soon.

Passing data to the scripts would be an improvement.

Thank you all again! :+1:

Hi.
I managed to pass arguments to the scripts, so I avoided creating too many of them.
I duplicated them, because now I’m using new door sensor supporting both on and off states, so now scripts are 2 for ON state and 2 for OFF state. They will not grow more than this. I didn’t test the voice scripts because it was late and i didn’t want to make noise at home, but the telegram scripts are working perfectly.
My actual scripts are:

#Notifiche Allarme Sensori Accesso
telegram_alarm_open:
  sequence:
    - condition: template
      value_template: >
        {{is_state('input_boolean.notify_telegram','on')}}
    - service: notify.telegram_bot
      data_template:
        message: Il contatto "{{ quale }}" è aperto
        title: '*RILEVATO ACCESSO "{{ quale }}"*'
          
voice_alarm_open:
  sequence:
    - condition: template
      value_template: >
        {{is_state('input_boolean.notify_voice','on')}}
    - service: tts.google_say
      entity_id: media_player.salotto
      data_template:
        message: 'Il contatto "{{ quale }}" è aperto'
        language: 'it'
        
telegram_alarm_close:
  sequence:
    - condition: template
      value_template: >
        {{is_state('input_boolean.notify_telegram','on')}}
    - service: notify.telegram_bot
      data_template:
        message: Il contatto "{{ quale }}" è chiuso
        title: '*RILEVATO ACCESSO "{{ quale }}"*'
          
voice_alarm_close:
  sequence:
    - condition: template
      value_template: >
        {{is_state('input_boolean.notify_voice','on')}}
    - service: tts.google_say
      entity_id: media_player.salotto
      data_template:
        message: 'Il contatto "{{ quale }}" è chiuso'
        language: 'it'

Automation part now is:

#Notifica con switch via Telegram e TTS aperture contatti e presenze
- id: 'Contatto1'
  alias: Notifica Contatto1
  trigger:
    - platform: state
      entity_id: binary_sensor.contatto_apertura_1
      to: 'on'
  condition: []
  action:
    - service: script.telegram_alarm_open
      data:
        quale: 'CONTATTO 1'
    - service: script.voice_alarm_open  
      data:
        quale: 'CONTATTO 1'

- id: 'Box Aperto'
  alias: Notifica Box Aperto
  trigger:
    - platform: state
      entity_id: binary_sensor.contatto_open_close_2
      to: 'on'
  condition: []
  action:
    - service: script.telegram_alarm_open
      data:
        quale: 'BOX'
    - service: script.voice_alarm_open   
      data:
        quale: 'BOX'      

- id: 'Giardino Aperto'
  alias: Notifica Giardino Aperto
  trigger:
    - platform: state
      entity_id: binary_sensor.contatto_open_close_1
      to: 'on'
  condition: []
  action:
    - service: script.telegram_alarm_open
      data:
        quale: 'PORTA GIARDINO'
    - service: script.voice_alarm_open  
      data:
        quale: 'PORTA GIARDINO'                    
        
- id: 'Box Chiuso'
  alias: Notifica Box Chiuso
  trigger:
    - platform: state
      entity_id: binary_sensor.contatto_open_close_2
      to: 'off'
  condition: []
  action:
    - service: script.telegram_alarm_close
      data:
        quale: 'BOX'
    - service: script.voice_alarm_close   
      data:
        quale: 'BOX'      

- id: 'Giardino Chiuso'
  alias: Notifica Giardino Chiuso
  trigger:
    - platform: state
      entity_id: binary_sensor.contatto_open_close_1
      to: 'off'
  condition: []
  action:
    - service: script.telegram_alarm_close
      data:
        quale: 'PORTA GIARDINO'
    - service: script.voice_alarm_close   
      data:
        quale: 'PORTA GIARDINO'  

This will grow for sure as I’ll add new sensors, so it’s on this part that I have to work in order to optimize it. I think that here the “elif” learned from FunkyBoT will become handy.
I don’t know if I’ll be able to do it, but yesterday I thought the same about passing data to script, and at the end it worked. :sweat_smile:

1 Like