How Do I Regenerate a Random Template Value After 'X' Minutes

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.

Thanks in advance for your help.

Use the new Trigger-based Template Sensor.

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.

1 Like

This is the code for the Text-To-Speech part of the automation

service: tts.google_translate_say
data:
  entity_id: media_player.living_room
  cache: false
  message: >
     {{states('sensor.interruptionprefix')}} message goes here

It’s not working since I made the changes above, do I need to do this differently?

No change is needed; it’s still just a sensor.

Go to Developer Tools > States, find sensor.interruptionprefix and confirm its state value contains one of the phrases.


EDIT

Ensure you are using version 2021.4 or 2021.5.

I changed the time to every 1 minute, and I made a typo, my mistake. I corrected it and everything is working now.

interruptionprefix

Thank you very much for your help.

1 Like