Variables in automation code

Hi all,

I have automation in place that works brilliantly, I have a pi with a button attached in the kitchen and when pressed it sends an MQTT message and triggers the lights on and off via scenes. I love it.

Anyway I’d like to take it up a notch by having it select a random scene each time the ON condition is met or I might also like to cycle through a number of schemes each time the button goes on. I’m racking my brains with {} type things but can’t see how to get variables into the automation, i tried a few things like { scene1, scene2, scene3 | random } but all i got were errors (that was pseudo code by the way, i did try various syntax). Can anyone help me out here?

Thanks

automation.yaml

- alias: 'button pressed on'
  trigger:
    platform: mqtt
    topic: dayroom/button_on
  action:
    - service: scene.turn_on
      entity_id: scene.evening_lights
    - service: notify.notify
      data:
        message: "Lights on"
        title: "Button press"

- alias: 'button pressed off'
  trigger:
    platform: mqtt
    topic: dayroom/button_off
  action:
    - service: scene.turn_on
      entity_id: scene.lights_out
    - service: notify.notify
      data:
        message: "Lights off"
        title: "Button press"

Not sure but this may help with syntax:

1 Like

Thanks that might well help me.

This brace expansion {{ etc , can it be used in other places… like in a scene for example to set random RGB values for hue lights?

I’ve never tried it myself and couldn’t find anything in a quick search but I would bet you that someone in the forums has. You may want to do a deep er search and see if you can locate anything. If I find anything relevant I will post it here.

i’m thinking that this sort of flexibility is often required but can’t find too many relevant examples.

Me either. It might be worth bringing it up in a Gitter chat. A lot of higher level devs hang out there.