Want Automation to Randomly Pick One

I have a list of 42 curl commands that I configured to run from HA. Two things:

  1. I want to make them shorter. Which Helper can be used as an alias, or should I do this in a script? Script seems pretty easy, so I’ll do that.
  2. How can I configure an automation to randomly pick one from a predefined list of scripts?

When you want “randomly pick one from a predefined list” the solution is probably Templates.

For example, here’s the Action step from one of my automations:

service: tts.cloud_say
entity_id: media_player.dining_room
data_template:
  message: >-
    {{ [ "Your tea is ready!", "Time for a cuppa!", "Don’t forget to drink your tea.", "That brew smells good!", "Hey, it’s tea time!", "Tea, Earl Grey, hot."] | random }}

I hope that’s enough for you to get to what you want. If not, please give us some more details about the automation you are building.

Can those phrases be replaced with script.a, scribt.b, etc? The curl commands I have are very long, so having everything in one line would be too much.

Basically I want one of the 42 curl commands I’ve already configured to be run based on a Time trigger.

So, for me, would this be accurate?

service: shell_command.curl
data:
  input: >-
    {{ [ script.curl_a, script.curl_b] | random }}

I’m not sure how to format a script in this instance. I don’t think it would be states(

Shell commands are called directly by their entity id, so it’s hard for us to offer suggestion without some idea how you have named them. If these commands don’t need descriptive entity id and you’re not dead set on specific names already, the easiest way to randomize would be to have each curl command set up as it’s own shell command entity with a numeric tail:

shell_command:
  curl_1: curl -# -O ftp://ftp.example.com/file.zip
  curl_2: curl --silent ftp://ftp.example.com/file.zip
  ...
  curl_42: curl --silent ftp://ftp.example.com/file_42.zip

Then your randomizing service call could be:

  - service: shell_command.curl_{{ range(1, 43) | random }}

And this would be within the configuration.yaml?

Yes, it can go in configuration.yaml.

Originally I had this:

shell_command:
  curl_1: > 
    curl -X POST "http://10.0.4.90/json/state" -d '{{input}}' -H "Content-Type: application/json"

where '{{input}}' would be populated in an automation. I guess that wouldn’t work for randomizing.

So, do you have 42 different possible values for input?

Correct, possibly more in the future. I don’t mind entering them all in the configuration.yaml, but I was hoping for a cleaner solution.

No, you can do it that way… You can set it up as a script:

variables:
  value: >
    {{ [ 'value_1', 'value_2'..... 'value_42'] | random }}
sequence:
  - service: shell_command.curl_1
    data:
      input: "{{value}}" 

Then have your automation(s) call the script.

So the above example would be the entire script?

Does it matter that the curl commands are over 2000 characters long each?

Yes, that would be the main body of the script. If you are setting it up directly in configuration.yaml you will need to add the script header and id info for the script:

script:
  random_curl:
    unique_id: random_curl_command
    variables:
      value: >
        {{ [ 'value_1', 'value_2'..... 'value_42'] | random }}
    sequence:
      - service: shell_command.curl_1
        data:
          input: "{{value}}"

I have no idea whether there is a character limit when passing variables to a shell command.

I wouldn’t set up the script in configuration.yaml. I’d likely leave the entry as is and set up the script separately.

Lemme do some tests…

This works. Can’t get anything else to work right with scripts. If it has to be this way for now, that’s fine.