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.
#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
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…
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.
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.
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.
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'
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.