Automation to call on a Random Script from a defined list

First Post! Newb.

I’m still learning, but I have an idea of what I’m trying to accomplish. I need some help.

Basic idea -

Configure multiple scripts with actions that control lights and sound.

When my Ring Doorbell is pressed, HA will run automation that will randomly select a script based on a predefined list of potential scripts.

example-

- id: '1561380301537'
  alias: Halloween Doorbell
  trigger:
  - entity_id: binary_sensor.ring_front_door_ding
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    **service: script.1561387318987**  (Can this be random?)
1 Like

Yes. Something like this should do it:

  action:
    - service: script.turn_on
      data_template:
        entity_id: >
          {{ ['script.script1', 'script.script2', ...] | random }}
1 Like

I am struggling a bit translating this into my HASS deployment. A little more background. I’m running hassio, raspberry pi 3+, and hass 0.94.4. I’m unsure of the other versions and differences in syntax or features.

Here is what I programmed and the system accepted it at a proper syntax.

Script

here are the errors that I’m seeing.

extra keys not allowed @ data[‘entity_ID’]

2:04 PM core.py (ERROR)

Error executing script script.1561397164681. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘entity_ID’]

2:04 PM helpers/script.py (ERROR)

I don’t know if the Automation Editor supports the use of templates. I don’t use that (and most people, to my knowledge, don’t.) I would recommend editing the YAML file directly.

Okay following your advise I programmed in YAML. Initially, I had a very difficult time getting it to run correctly. After tinkering around I realized I was actually generating some randomness (matching what I’m trying to accomplish) in the error file and narrowed down what I needed to adjust.

This is the final outcome that works for me. Randomizing a Service_Call when someone rings the Doorbell. Now I can alternate between spooky scenes when Halloween rolls around…

FYI, I ended up writing this into a new Script and not an Automation.

alias: Random_Select
  sequence:
 - data_template:
      entity_id: >
        {{ ["script.1561387318987", "script.1561381411884", "script.1561385469276"]
          | random}}
    service: script.turn_on```
4 Likes

im trying to do the samething but for scenes, chaning your code to

alias: Random_Select
  sequence:
 - data_template:
      entity_id: >
        {{ ["scene.blue1", "scene.green1", "scene.red1", "scene.purple1", "scene.pink1", "scene.orange1"]
          | random}}
    service: scene.turn_on```

but i just get
“Message malformed: Integration ‘’ not found”
any idea?

Hi, I have limited experience writing such templates, so not the most elegant solution:

I think this is what you wanted to do:

alias: Random Select Script
sequence:
  - service: "{{ ['script.say_get_outta_here', 'script.say_what_you_do_you_do', 'script.say_sorry_this_room_is_occupied'] | random }}"
    data: {}
mode: single

You can also define a variable and use choose to choose the script:

alias: Define Variable and Choose Random Message to Say
sequence:
  - alias: Set a templated variable
    variables:
      selection_variable: "{{ range(0,3) | random | int }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ selection_variable == 0 }}"
        sequence:
          - service: notify.alexa_media
            data:
              target: media_player.living_room_echo
              message: Get outta here!
              data:
                type: tts
      - conditions:
          - condition: template
            value_template: "{{ selection_variable == 1 }}"
        sequence:
          - service: notify.alexa_media
            data:
              target: media_player.living_room_echo
              message: Sorry this room is occupied!
              data:
                type: tts
      - conditions:
          - condition: template
            value_template: "{{ selection_variable == 2 }}"
        sequence:
          - service: notify.alexa_media
            data:
              target: media_player.living_room_echo
              message: What you do you do?
              data:
                type: tts
  - service: notify.alexa_media
    data:
      target: media_player.living_room_echo
      message: "{{ ['chicken', 'pot roast', 'cotton candy'] | random }}"
      data:
        type: tts
mode: single

You can also choose directly:

alias: Say Random Message
sequence:
  - service: notify.alexa_media
    data:
      target: media_player.living_room_echo
      message: >-
        {{ ['Get outta here!', 'Sorry, this room is occupied', 'What you do you
        do?'] | random }}
      data:
        type: tts
mode: single
2 Likes