Turn on light with random brightness & transiotion?

I’m working on a little “star light” script for my bathroom and would like to do this…
Now the example script won’t work obviously, but hopefully someone has figured this out already and can point me in the right direction :slight_smile:

- service: light.turn_on
  data:
    entity_id: light.office_desk
    brightness: '{{ range(1, 200)|random|int }}'
    transition: '{{ range(1, 10)|random|int }}'

I believe the entity should be between service and data.

- service: light.turn_on
  entity_id: light.office_desk
  data:
    brightness: '{{ range(1, 200)|random|int }}'
    transition: '{{ range(1, 10)|random|int }}'

@Klex1404 if I’m not mistaking it’s works either way.

My problem is the random part, as I con’t find a way of using a value template I tried with a random sensor but again it fails.

This is the new code I tried:

- platform: random
  name: brightness
  minimum: 0
  maximum: 200

- service: light.turn_on
  entity_id: light.office_desk
  data:
    brightness: sensor.brightness
    xy_color: [0.6237,0.3462]
    transition: 3

Invalid service data for light.turn_on: expected int for dictionary value @ data['brightness']. Go 'sensor.brightness'

I figured it out!

This script will turn on a Philips Hue lamp with random color and brightness and transition to the values over three seconds.
Then it will fade out over a random timeframe of 1-10 sec. The delay in the middle has to be the same as the transition-in time, without it the lamp will fade-out immediately.

It would be cool if the transition-in time could be random too, but then that value has to be stored as a variable and used for the delay too…

- service: light.turn_on
  entity_id: light.office_desk
    transition: 3
  data_template:
    xy_color: ['{{ range(1,10000)|random/10000|float }}','{{ range(1,10000)|random/10000|float}}']
    brightness: '{{ range(1,200)|random|int }}'
- delay: '{{ "00:00:%0.02d" | format(range(3, 10)|random|int) }}'
- service: light.turn_off
  entity_id: light.office_desk
  data_template:
    transition: '{{ range(1,10)|random|int }}'
3 Likes

@ZyberSE
Sorry to dredge up such an old thread but I’m interested in creating this starlight effect for myself. I have 12 ceiling spots which I would like to all fade in and out randomly. How are/were you triggering this script on a loop to maintain the effect over long periods, or was that not your use case?

Thanks!