Automation to change RBG of lights with python_scripts

I’m trying to create an automation which would allow me to be bit more dynamic in changing the light colours. Let’s call it a disco for now (although missing the 1sec timers here). I am using Ikea remote, Ikea rgb bulb and zigbee2mqtt. HA 0.114.4 and z2m 1.16.0.

And, well, it doesn’t work. I tried also to change that .py 1) replacing ’ by ", no effect 2) making more direct calls like row: rgb = [255, 0, 0], no effect.

Any hints? There is nothing in home-assistant.log. Just that the automation “Change light colours” has been triggered in the UI logbook. And by the way, my first python script :slight_smile:

Automation:

- id: ChangeRGB
  alias: 'Change light colours'
  trigger:
    platform: state
    entity_id: sensor.ikea_remote_1_action
    to: 'brightness_up_click'
  action:
    - service: python_script.random_colours
      data:
        entity_id: light.jalkalamppu
        brightness: 255

python script (in /python_scripts/random_colours.py):

entity_id = data.get("entity_id")
brightness = data.get("brightness")
r = random.randint(0,256)
g = random.randint(0,256)
b = random.randint(0,256)
rgb = [r, g, b]
if entity_id is not None:
  service_data = {'entity_id': entity_id, 'rgb_color': rgb, 'brightness': brightness}
  hass.services.call('light', 'turn_on', service_data, False)

and obviously in configuration.yaml:
python_script:

This is based on descriptions and hints from https://www.home-assistant.io/integrations/python_script and [SOLVED] Python script, need to build a for loop to turn on a random number of lights

Have you considered not using a python_script?

- id: ChangeRGB
  alias: 'Change light colours'
  trigger:
    platform: state
    entity_id: sensor.ikea_remote_1_action
    to: 'brightness_up_click'
  action:
    - service: light.turn_on
      data:
        entity_id: light.jalkalamppu
        brightness: 255
        rgb_color:
          - '{{ range(0, 256) | random }}'
          - '{{ range(0, 256) | random }}'
          - '{{ range(0, 256) | random }}'
1 Like

Wow! Super! I read from the link that it’s simply easier with python. This works like a dream! Thanks!

1 Like

You’re welcome!

Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. This helps other users find answers to similar questions.