Choosing random effect for light from list?

Hey Everyone!

I am trying to create a button that will choose a random Nanoleaf effect from a list whenever I tap the button, but I keep getting an error.

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: light.turn_on
  data:
    effect: {{ ['Effect 1', 'Effect 2', 'Effect 3', 'Effect 4', 'Effect 5'] | random | string }}
  target:
    entity_id: light.nanoleaf
name: Random Canvas Scene
icon_height: 50px
icon: mdi:crop-square

The effect list above is just an example. The effect code correctly returns a string when used in the Template Editor. The random examples I found online seem to match this format, however it fails with this error:

Failed to call service light/turn_on. Attempting to apply effect not in the effect list: ‘{{ [‘Effect 1’, ‘Effect 2’, ‘Effect 3’, ‘Effect 4’, ‘Effect 5’] | random | string }}’

What could be the problem and how do I fix it?

Thanks again!!

Core dashboard cards do not accept templates with very few exceptions. This is not one of them.

Unless you use this:

Also you need to quote single line templates.

Also also it is called service_data in the tap action.

tap_action:
  action: call-service
  service: light.turn_on
  service_data:
    effect: "{{ ['Effect 1', 'Effect 2', 'Effect 3', 'Effect 4', 'Effect 5'] | random | string }}"
  target:
    entity_id: light.nanoleaf
2 Likes

Interesting. The UI button creator used “data” instead of “service_data”. Would this be an error in the UI button creator or are these two synonyms?

Huh. It seems that this has been changed. Ignore that bit about service_data. Not sure when it happened.

Ahh, ok.

Would there be a way out-of-box to have a button choose a random value from a list without the need to import a custom card?

Thanks again!

Make the button call a script. Make the script turn on the light and select a random effect.

1 Like

:man_facepalming:

Sometimes it’s the most obvious things that are overlooked…

Thanks for the pointer. That fixed it!!

1 Like