Randomise messages with an entity and from a file

Howdy -

I was hoping to make our experience of HA a little nicer by randomising some of the status messages that are displayed on our phones and announced tts over our media player.

For example: when the washing machine finishes a cycle, our HA says the exact same thing every time, “the washing machine has finished, please hang out the washing soon.” It would be nice to have a list of similar phrases with the same intent but slightly different wording, and have HA choose randomly from them when announcing the end of the washing cycle.

I figured the correct way to do this would be with a template entity, something like:

template:
  - trigger:
    sensor:
      # random messages for the washing machine
      - name: "washing machine complete"
        state: '{{ ["msg1", "msg2", msg3"] | random }}'

and then I could add a trigger to that to either update on a time pattern, or the state change of the washing machine.

Where I’m stuck and can’t figure out how to do is I’d like to draw those messages (msg1, msg2, msg3) from a text file stored on the server somewhere – CSV, json, etc, I don’t care. Where do I start with that?

Thanks!

You’re only option is to take advantage of custom_templates

Make a folder in your config directory named custom_templates. Inside that folder make a file named washer.jinja.

Then in the contents of that file.

{% set messages = [
  'msg1',
  'msg2',
  'msg3',
  'msg4',
  'msg5',
  'msg6',
  'msg7',
] %}

Then when your laundry is done, use this template for your message:

{% from 'washer.jinja' import messages %}
{{ messages | random }}
2 Likes