I am working on adding some Text-To-Speech automations, and would like all of my announcements use a randomly chosen interruption prefix. I added this code to my ‘configuration.yaml’ file and it randomly chooses an interruption prefix
sensor:
- platform: template
interruptionprefix:
value_template: >
{{ ["Excuse me", "Excuse me for interrupting", "Pardon me", "Pardon the interruption", "Pardon me for interrupting", "Excuse the interruption", "Please excuse the interruption", "Please excuse me for interrupting", "sorry to interrupt", "sorry for interrupting", "Sorry for the interruption", "Sorry to disturb you", "sorry for interrupting you" ] | random }}
The problem is unless I manually reload the Template Entities it will keep using the same phrase. Is there a way to have the value regenerate after a certain number of minutes has passed?
If there is a better way to do this, please give me a nudge in the right direction.
I know I could generate the random phrase in an automation, but I’m going to use it in multiple places, and I thought it would be easier to use this way, if I’m wrong please let me know.
template:
- trigger:
- platform: time_pattern
hours: '/1'
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
sensor:
- name: interruptionprefix
state: >
{{ [ "Excuse me", "Excuse me for interrupting",
"Pardon me", "Pardon the interruption",
"Pardon me for interrupting", "Excuse the interruption",
"Please excuse the interruption",
"Please excuse me for interrupting",
"sorry to interrupt", "sorry for interrupting",
"Sorry for the interruption",
"Sorry to disturb you",
"sorry for interrupting you" ] | random }}
The three triggers will cause this Trigger-based Template Sensor to evaluate its template (i.e. pick a random phrase):
Every hour
When Home Assistant starts
Whenever you Reload Template Entities
Feel free to change the first Time Pattern Trigger to run every X minutes instead of hours.