I think this is something the whole community could benefit from so it would be great if everyone could chime in on the best way to do this?
I’m looking to create TTS notifications based on which one of the six smoke detectors has gone into activation. Based on which detector and whether it’s upstairs or downstairs and whether it is at the north or south end, the notification will provide assistance in regards to which exit to use (assuming my Google Homes haven’t melted in the fire!).
If the activation is upstairs, tell me which detector it is then to exit via the front door (based on the fact the the upstairs smoke group is on).
If it is downstairs but in the north end, don’t use the front door so exit via the deck door etc.
I’ve created groups to help with the ‘steering’ and a test script with a basic non-templated notification but seek assistance into creating an automation template that everyone can use.
## Smoke Detectors (Xaiomi) ##
Smoke_Detectors_Downstairs:
name: Ground Floor Smoke Detectors
entities:
- binary_sensor.smoke_sensor_158d000153634c # Davids Bedroom
- binary_sensor.smoke_sensor_158d00015730d2 # Entry Foyer
- binary_sensor.smoke_sensor_158d0001db8b97 # Gaming Lounge
- binary_sensor.smoke_sensor_158d00015746bc # Master Bedroom
Smoke_Detectors_Upstairs:
name: 1st Floor Smoke Detectors
entities:
- binary_sensor.smoke_sensor_158d000157467e # Landing
- binary_sensor.smoke_sensor_158d0001d937e0 # Lounge
Smoke_Detectors_Downstairs_North:
name: Northern Smoke Detectors
entities:
- binary_sensor.smoke_sensor_158d00015730d2 # Entry Foyer
- binary_sensor.smoke_sensor_158d00015746bc # Master Bedroom
Smoke_Detectors_Downstairs_South:
name: Southern Smoke Detectors
entities:
- binary_sensor.smoke_sensor_158d000153634c # Davids Bedroom
- binary_sensor.smoke_sensor_158d0001db8b97 # Gaming Lounge
## Test fire alarm notification ##
tts_notification:
alias: 'Test fire alarm on Kitchen Home'
sequence:
- service: media_player.volume_set
data:
entity_id: media_player.kitchen_home
volume_level: '0.90'
- service: tts.google_say
entity_id: media_player.kitchen_home
data:
message: "The smoke detector in the lounge is in activation. Exit via the front door. The smoke detector in the lounge is in activation. Exit via the front door"
- service: media_player.volume_set
data:
entity_id: media_player.kitchen_home
volume_level: '0.50'
Thank you so much @RobDYI. How you guys come up with templating code like this is beyond me. I’m assuming I can fire an activation for each detector under the Services tab? I’m going to rename my entity_ids.yaml first I think to make coding and debugging much easier and then provide some additional feedback once tested. I only have three exit doors and currently six smoke detectors (going to nine) so your template should work fine, thank you.
Is there an easier way to make TTS repeat the message or do I need to just repeat the code?
{%- for state in states.sensor if state.entity_id == trigger.entity_id -%}
{{ state.attributes.friendly_name }} is detecting smoke, use the
{%- endfor -%}
can be more simply done like this:
{{ trigger.to_state.attributes.friendly_name }}
And actually, since in this case the trigger is a binary_sensor, I don’t think the original for loop would work unless states.sensor was changed to states.binary_sensor (but that was probably just a cut and paste error. )
Building on @RobDYI’s suggestion, how about something like this:
automation:
- alias: Fire exit
trigger:
platform: state
entity_id:
# Upstairs
- binary_sensor.smoke_sensor_158d000157467e # Landing
- binary_sensor.smoke_sensor_158d0001d937e0 # Lounge
# Downstairs south
- binary_sensor.smoke_sensor_158d000153634c # Davids Bedroom
- binary_sensor.smoke_sensor_158d0001db8b97 # Gaming Lounge
# Downstairs north
- binary_sensor.smoke_sensor_158d00015730d2 # Entry Foyer
- binary_sensor.smoke_sensor_158d00015746bc # Master Bedroom
to: 'on'
action:
service: script.tts_notification
data_template:
message: >
The smoke detector in the {{ trigger.to_state.attributes.friendly_name
}} is activated. Exit via the {%
set doors = {
('binary_sensor.smoke_sensor_158d000157467e',
'binary_sensor.smoke_sensor_158d0001d937e0',
'binary_sensor.smoke_sensor_158d000153634c'
'binary_sensor.smoke_sensor_158d0001db8b97'): 'front',
('binary_sensor.smoke_sensor_158d00015730d2',
'binary_sensor.smoke_sensor_158d00015746bc'): 'deck'} -%}
{% for detectors in doors if trigger.entity_id in detectors -%}
{{ doors[detectors] }}
{%- endfor %} door.
script:
tts_notification:
alias: 'Test fire alarm on Kitchen Home'
sequence:
# Put some appropriate condition here that allows
# the message to continue to be repeated
- condition: ...
- service: media_player.volume_set
data:
entity_id: media_player.kitchen_home
volume_level: '0.90'
- service: tts.google_say
entity_id: media_player.kitchen_home
data_template:
message: "{{ message }}"
- service: media_player.volume_set
data:
entity_id: media_player.kitchen_home
volume_level: '0.50'
- service: script.tts_repeat
tts_repeat:
alias: Repeat the message
sequence:
service: script.tts_notification
I really can’t test this, but hopefully it’s a start. Let me know if you have any questions.
Thanks. I’ll give that a try this afternoon and post the results. The only thing I was thinking about last night and the reasons for grouping nearby sensors is that if one sensor goes into activation, in a real fire it is likely that the next sensor closest would also go into activation confusing the TTS?
In the case of upstairs, I have two detectors, one in the lounge and one at the top of the stairs (landing) so I’ve grouped these together. In the case of either sensor going off, the group would be active driving the door notification. So the first part of the TTS would define the actual detector in activation and the second part would use the group to define the exit to use…Just thinking out aloud.
Just realised you are grouping the doors inside your template, cool. I’ll load that up this afternoon. Not sure if it is possible to get HA to simulate a binary_sensor activation though or whether I have to wave real smoke underneath each sensor.
You can temporarily change the state of any entity on the state’s page, which should trigger the automation. Next time the sensor updates it should go back to the real state.
Not the Services page, the States page. If you click on an entity, it will be populated into the top area where you can change its state (or any attribute, for that matter.) This writes directly to the state machine. It doesn’t actually change the entity itself. It’s basically causing a state change event and temporarily changing the state of the entity in the state machine (until the next time the entity updates and refreshes the state machine.)
Don’t you hate it when your wifes to do list gets in the way of yours!
OK, did some testing using the Dev > States panel (funny how you go 2yrs without fulling knowing how valuable that is!). Seems to work with the code below except that the volume calls 90%, drops to 50% then reads the message so something not quite right. And of course, I don’t know what to but in the condition field to make the message repeat.
automation:
- alias: Fire exit notification
trigger:
platform: state
entity_id:
# Upstairs
- binary_sensor.smoke_sensor_landing
- binary_sensor.smoke_sensor_lounge
# Downstairs south
- binary_sensor.smoke_sensor_davids_bedroom
- binary_sensor.smoke_sensor_gaming_lounge
# Downstairs north
- binary_sensor.smoke_sensor_entry_foyer
- binary_sensor.smoke_sensor_master_bedroom
to: 'on'
action:
service: script.tts_fire_notification
data_template:
message: >
The smoke detector in the {{ trigger.to_state.attributes.friendly_name
}} is activated. Exit via the {%
set doors = {
('binary_sensor.smoke_sensor_lounge',
'binary_sensor.smoke_sensor_landing',
'binary_sensor.smoke_sensor_gaming_lounge'
'binary_sensor.smoke_sensor_davids_bedroom'): 'front',
('binary_sensor.smoke_sensor_master_bedroom',
'binary_sensor.smoke_sensor_entry_foyer'): 'deck'} -%}
{% for detectors in doors if trigger.entity_id in detectors -%}
{{ doors[detectors] }}
{%- endfor %} door.
script:
tts_fire_notification:
alias: 'Test fire alarm on Google Homes'
sequence:
# Put some appropriate condition here that allows
# the message to continue to be repeated
# - condition: ...
- service: media_player.volume_set
data:
entity_id:
- media_player.kitchen_home
- media_player.gaming_room_home
- media_player.ensuite_speaker
- media_player.bathroom_speaker
volume_level: '0.90'
- service: tts.google_say
entity_id:
- media_player.kitchen_home
- media_player.gaming_room_home
- media_player.ensuite_speaker
- media_player.bathroom_speaker
data_template:
message: "{{ message }}"
- service: media_player.volume_set
data:
entity_id:
- media_player.kitchen_home
- media_player.gaming_room_home
- media_player.ensuite_speaker
- media_player.bathroom_speaker
volume_level: '0.50'
- service: script.tts_repeat
tts_repeat:
alias: Repeat the message
sequence:
service: script.tts_fire_notification
I would guess you need to insert a delay before changing volume back to 50% to give it time to actually read the message.
So as it is it should repeat until you manually stop it. Is it repeating? The idea behind the condition is to add something that will stop it from repeating. In the most simple case you could have an input_boolean (that defaults to ‘on’) that you use in the condition. That would effectively enable the announcement. Then if it ever went off (hopefully a false alarm!) you could stop the messages by turning the input_boolean ‘off’.
Or, just skip adding a condition. You should always be able to stop the script manually, assuming you add the delay.
Invalid config for [script]: expected a dictionary for dictionary value @ data['script']['tts_repeat']['sequence'][0]['data_template']. Got None extra keys not allowed @ data['script']['tts_repeat']['sequence'][0]['message']. Got '{{ message }}'. (See /config/configuration.yaml, line 194). Please check the docs at https://home-assistant.io/components/script/
Thanks. And yours works? I’ll try removing the cache. I’m limited in my testing time as I have to wait until the rest of the family goes out before I can fire the script hence the delays…frustrating.