Can an automation randomly select from a list during action?

I currently have an automation that plays a certain message on my speakers under certain conditions. Right now it’s just a combination of if/else statements and that works alright, but eventually the messages get a bit boring/repetitive. I would like to make a list that the automation can pull a random line to use. I would prefer to keep these in separate files so I don’t have to edit the automation every time I modify the lists, and to help keep the automation action clean looking. Is any of this possible to do?

I’m not sure if this is possible using standard automation (it might be but i don’t know). If you know a bit of programming you might want to look into pyscript, for me this opened up a lot of possibilities in my automations.

I provided an example of how to do that here:

It creates a Trigger-based Template Sensor that randomly selects a phrase every hour from a list (defined within the sensor’s template). It will also randomly selects a phrase on startup and whenever Template Entities are reloaded. That means if you want to add new phrases, it’s simply a matter of appending them to the sensor’s list and executing Reload Template Entities. Done!

The Template Sensor’s state value contains the randomly selected phrase and is easily accessible from an automation, script, or even another Template Sensor. The limitation is that a phrase cannot exceed 255 characters in length because that’s the storage limit of a state value for all entities.

Here’s a related example:

1 Like

This looks like it could do what I want. For my purposes, I could just use a trigger for when a specific other automation is triggered, or even just the state for a specific entity changes, which is what triggers that automation. Thanks for this example.

Let us know what you ultimately choose to do.

Well at the moment I keep running into an error in the editor saying that I have an invalid spacing. Specifically it looks like this

bad indentation of a sequence entry at line 486, column 17:
          - platform: template
                    ^ // The formatting in this page is off, but the arrow is directly under the 'o' in perform.

Any ideas how to fix that? I’ve never seen it complain about indentation in the middle of a word before. I tried making it 2 or 4 spaces and it still is wrong at the ‘o’.

Hard to say, that line doesn’t exist in the example I posted.

You’ll have to reveal more of what you pasted and where you pasted it.

Sorry about that, was in a rush and forgot to paste the second part.

template:
  - trigger
      - platform: template
        value_template: "{{ not is_state('group.all_people', 'home') }}"
    sensor:
    - ...

Before we dive into troubleshooting this, which version of Home Assistant are you using?

core-2021.5.3
HASS-OS 5.13

The excerpt you posted is located in confguration.yaml?

That is correct. It’s also my only template entry.

Before getting ahead of ourselves, try adding the linked example exactly as shown. It should produce a new entity named sensor.interruptionprefix. If that fails to work then we know something isn’t quite right with how or where it’s being defined (as opposed to a syntax issue within itself).

Yours was fine, so while pasting mine back in I saw that my - trigger was missing the colon. It finally checked out as valid but they never showed up. After poking around quite a bit longer, I found that I had a few other formatting errors, including one of my quotes escaping too early. It’s all good now.

I might move the sensor out to a separate file so that I don’t have to edit my main config file every time I want to modify the entries, but that’s a problem for future me.

Thanks for help!

You’re welcome!

If the suggested Trigger-based Template Sensor meets your requirements (shown in your first post) please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title signaling to other users that this topic is resolved. It will also place a link below your first post that leads to the Solution post. All of this helps other users find answers to similar questions.

you can use something very simple, instead of the phrase, copy this…

  {{ [
  "OK",
  "Sure",
  "If you insist",
  "Done",
  "No worries",
  "I can do that",
  "Leave it to me",
  "Consider it done",
  "As you wish",
  "By your command",
  "Affirmative",
  "Yes oh revered one",
  "I will",
  "As you decree, so shall it be",
  "No Problem"
  ] | random }}
8 Likes

is there a syntax to make an NOT-selection.
For example if i extract the wled-effect list, if i would like to exclude some of the effects? how would i change the effect-row?

    - service: light.turn_on
      target:
        entity_id: light.wled
      data:
        brightness: 254
        color_name: "{{ colors | random }}"
        effect: "{{ state_attr('light.wled', 'effect_list') | random }}"

I do not want to specify the list as shown above, wled adds effects with upgrades, so i prefer being able to exclude instead.

This one is working with my Hue lights:


{{ state_attr('light.hue_iris', 'effect_list') |reject('match', 'candle') |list |random }}

2 Likes

will try, many thanks! :slight_smile: