Random rgb_color in scene

You’re such a bad ass! Thank you so much, couldn’t have spotted that in a hundred years!

Edit: This is a workaround for setting an effect on Tasmota Magic Home LED. By change you know how to let this loop for about a minute, it only changes once now… Cant seem to find any guides, saw another post of yours but not really apliccable in my case.

You want it to change colour every few seconds for a minute? If so, how random do you want it to be? Like does it always go red, yellow, blue, green, purple but just the start color is different each time? Or is the first colour based on something? Or do you just want a completely random colour every time?

Well it really doesnt matter, id like to use it in my alarm setup to draw attention so it just has to switch fast until the state of the alarm is disarmed, or as lomg as its triggered.

I tried the snippit from above followed by turn off an then copied that like 100 times, didnt work and ofcourse a nasty workaround.

Thanks for your help!
Ezra

maybe my party lights script helps:

  lights_partying_on:
    alias: Lights partying on
    sequence:
      service: light.turn_on
      data_template:
        entity_id: light.{{['dining_corner','cabinet','kist','home_theater']|random}}
        rgb_color: ['{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}']
        brightness: '{{ (range(50, 250)|random) }}'
        transition: '{{ (range(1, 3)|random) }}'

call this with the following automation each 4 seconds:

automation:
  - alias: 'Lights partying'
    id: 'Lights partying'
    initial_state: 'off'
    trigger:
      platform: time
      seconds: '/4'   # {{ "/" ~ ((range(2, 5) | random) | int) }}
    condition:
      condition: template
      value_template: >
        {{is_state('automation.sense_lights_change','off') }}
    action: 
      service: script.lights_partying_on

and this input_boolean for setting the mode:

  - alias: 'Lights Party Mode'
    id: 'Lights Party Mode'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_boolean.home_mode_party
    action:
      service_template: >
        script.lights_mode_party_{{ trigger.to_state.state }}
4 Likes

OK, off the top of my head I would probably do it like this:

automation:
  - alias: Police
    trigger:
      - platform: state
        entity_id: alarm_control_panel.house
        to: 'triggered'
    action:
      - service: script.light_cycle
      - delay: '00:01:00'
      - service: homeassistant.turn_off 
        entity_id:
          - script.light_cycle
          - script.loop_lights

script:
  light_cycle:
    sequence:
      - service: homeassistant.turn_on 
        entity_id: group.all_lights
        data_template:
          brightness: 250
          color_name: >
            {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
            {{ colors|random }}
      - service: script.loop_lights

  loop_lights:
    sequence:
      - delay: '00:00:02'
      - service: script.light_cycle

Fairly simple, but does the business.

3 Likes

cool this loop. you’re a magician!
—> Cookbook this goes.

1 Like

Super!! thanks mate!

1 Like

is there a way to use mili seconds instead of seconds? To have it change the color faster perhaphs?
Thanks again!

Edit: got the miliseconds now, but it still doesnt loop, it turns on and does not change just once.

- alias: "[Alarm] Police on"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'triggered'
  action:
    - service: script.light_cycle
    
- alias: "[Alarm] Police off"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'disarmed'
  action:
    - service: homeassistant.turn_off 
      entity_id:
        - script.light_cycle
        - script.loop_lights

  light_cycle:
    sequence:
      - service: homeassistant.turn_on
        entity_id: group.all_lights
        data_template:
          brightness: 100
          color_name: >
            {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
            {{ colors|random }}
      - service: script.loop_lights

  loop_lights:
    sequence:
      - delay:
          milliseconds: 10
      - service: script.light_cycle

Ok i got it down to about 3 changes right now with this config:

scripts.yaml:

  light_cycle:
    sequence:
      - service: light.turn_on
        entity_id: group.all_lights
        data_template:
          brightness: 250
          color_name: >
            {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
            {{ colors|random }}
      - service: script.loop_lights

  loop_lights:
    sequence:
      - delay:
          milliseconds: 5
      - service: script.light_cycle

automations.yaml:

- alias: "[Alarm] Police on"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'triggered'
  action:
    - service: light.turn_off
      entity_id: group.all_lights
    - service: script.light_cycle

- alias: "[Alarm] Police off"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'disarmed'
  action:
    - service: homeassistant.turn_off
      entity_id:
        - script.light_cycle
        - script.loop_lights

Any advice?

The problem is that when you’re looping, if the first script is still running when the second one tries to start it again then everything stops. So by bringing the time down to milliseconds, your lights are probably still changing colour from the last go, so the script is still running, so it stops.

I suppose you could try stopping the first script before restarting it.

Good one, it loops alright now. but not as fast as i want. Could also be that it are mqtt lights?

  light_cycle:
sequence:
  - service: homeassistant.turn_off
    entity_id: script.loop_lights
  - service: light.turn_on
    entity_id: group.all_lights
    data_template:
      brightness: 250
      color_name: >
        {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
        {{ colors|random }}
  - service: script.loop_lights

  loop_lights:
    sequence:
      - service: homeassistant.turn_off
        entity_id: script.light_cycle          
      - delay:
          milliseconds: 5
      - service: script.light_cycle

Yeah, it’s tempting to want the lights to strobe like you’re trying to induce a seizure, but unless they’re specifically designed as an anti-epileptic device lights normally don’t react that quickly so you have to have a bit of a delay.

In fairness, I bet if you stand over the road in the dark and trigger this it will still be pretty eye-catching :slight_smile:

1 Like

Haha, nice one. Yeah indeed, I’ll tinker on for a bit to tweak it. I’m glad it works so far with your help. Thanks a bunch my friend!

1 Like

No worries :+1:

I like the concept of these party light animations. But I cant trace the linkages between the input_boolean, the script and the automation? (their seems to be missing scripts?)

Do you have a link to your package yaml?

sure, here you go. Please note the input_boolean.home_mode_party isn’t in here, because it is triggering some other things too, its in a another package, but nothing fancy, just:

##########################################################################################
# Package Home Modes
# @Mariusthvdb 
##########################################################################################

homeassistant:
  customize:

    input_boolean.home_mode_party:
      templates:
        icon: >
          if (state === 'on') return 'mdi:cake-layered';
          return 'mdi:hotel';
        icon_color: >
          if (state === 'on') return 'rgb(251, 210, 41)';
          return 'rgb(54, 95, 140)';

##########################################################################################
# Inputs
##########################################################################################

input_boolean:

  home_mode_party:
    name: Party mode
    initial: off

##########################################################################################
# Sensors
##########################################################################################

##########################################################################################
# scripts
##########################################################################################

script:


  home_mode_party:
    alias: Home mode party
    sequence:
      - service: homeassistant.turn_off
        entity_id: input_boolean.motion_detection

#########################################################################################
# automation
##########################################################################################

automation:

  - alias: 'Home mode Party'
    id: 'Home mode Party'
    initial_state: 'on'
    trigger:
      platform: state
      to: 'on'
      entity_id: input_boolean.home_mode_party
    action:
      service: script.home_mode_party



##########################################################################################
# Small package Partymode
# @Mariusthvdb  02102018
##########################################################################################

#homeassistant:
#  customize:

#    switch.mode_party:
#      templates:
#        icon: >
#          if (state === 'on') return 'mdi:cake-layered';
#          return 'mdi:hotel';
#        icon_color: >
#          if (state === 'on') return 'rgb(251, 210, 41)';
#          return 'rgb(54, 95, 140)';

##########################################################################################
# Switch
##########################################################################################

#switch:
#  - platform: template
#    switches:
#      mode_party:
#        friendly_name: Party mode switch
#        value_template: >
#          {{ is_state('automation.lights_partying', 'on') }}
#        turn_on:
#          - service: script.home_mode_party #(turn off motion sensors, package home_mode)
#          - service: script.lights_mode_party_on #(turn off sense lights automation, on party Lights
#        turn_off:
#          - service: script.lights_mode_party_off

##########################################################################################
# Scripts
##########################################################################################

script:

  lights_mode_party_on:
    alias: Party on
    sequence:
      - service: homeassistant.turn_off
        entity_id: automation.sense_lights_change
      - delay:
          seconds: 5
      - service: homeassistant.turn_on
        entity_id: automation.lights_partying

  lights_mode_party_off:
    alias: Party off
    sequence:
      - service: automation.turn_off
        entity_id: automation.lights_partying
      - delay: 
          seconds: 2
      - service: script.lights_partying_reset
      - delay: 
          seconds: 2
      - service: script.lights_partying_off
      - delay:
          seconds: 10
      - service: homeassistant.turn_on
        entity_id: automation.sense_lights_change

  lights_partying_on:
    alias: Lights partying on
    sequence:
      service: light.turn_on
      data_template:
        entity_id: light.{{['dining_corner','cabinet','kist','home_theater']|random}}
        rgb_color: ['{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}']
        brightness: '{{ (range(50, 250)|random) }}'
        transition: '{{ (range(1, 3)|random) }}'

  lights_partying_reset:
    alias: Lights partying reset
    sequence:
      service: light.turn_on
      data:
        entity_id:
          - light.dining_corner
          - light.cabinet
          - light.kist
          - light.home_theater
        profile: relax

  lights_partying_off:
    alias: Lights partying off
    sequence:
      service: light.turn_off
      entity_id: 
        - light.dining_corner
        - light.cabinet
        - light.kist
        - light.home_theater

##########################################################################################
# Automations
##########################################################################################

automation:
  - alias: 'Lights partying'
    id: 'Lights partying'
    initial_state: 'off'
    trigger:
      platform: time
      seconds: '/4'   # {{ "/" ~ ((range(2, 5) | random) | int) }}
    condition:
      condition: template
      value_template: >
        {{is_state('automation.sense_lights_change','off') }}
    action: 
      service: script.lights_partying_on

  - alias: 'Lights Party Mode'
    id: 'Lights Party Mode'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_boolean.home_mode_party
    action:
      service_template: >
        script.lights_mode_party_{{ trigger.to_state.state }}
3 Likes

I love this concept… Thanks for sharing
I 'm trying to modify it to only flicker red and blue lights in order to simulate police lights.
This kind of flickers the red and blue but it is mixed in with gentle transitions of yellow green blue red and purple.

  alias: Police
  description: ''
  trigger:
  - entity_id: alarm_control_panel.home_alarm
    platform: state
    to: 'triggered'
  condition: []
  action:
  - data: {}
    entity_id: script.light_cycle
    service: homeassistant.turn_off
  - data: {}
    entity_id: script.loop_lights
    service: homeassistant.turn_off
  mode: single

light_cycle:
  alias: Light Cycle
  #mode: single
  sequence:
  - service: homeassistant.turn_on
    entity_id: switch.office_on_off
    data_template:
      brightness: 255
      color_name: >
        {% set colors = ['red', 'blue'] %}
#        {{ colors|random }}
  - service: script.loop_lights


loop_lights:
  alias: Loop Lights
  #mode: single
  sequence:
    - delay: 
        milliseconds: 5
    - service: script.light_cycle

action:
  - service: light.turn_on
    entity_id: light.{{['hue_color_lamp_1','hue_color_lamp_2'] | random }}
    data:
      brightness: 250
      color_name: >
        {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %} {{
        colors|random }}
mode: single

HI Team

I get the error - not a valid value for dictionary value @ data[‘action’][0][‘entity_id’]
I can see in the sample before a data_template is used, but can I do what I’m trying in an automation - so selecting randomly between a few ligths…

/Joern

1 Like

think you need to change it to:

entity_id: >
  light.{{['hue_color_lamp_1','hue_color_lamp_2'] | random }}

or use quotes:

entity_id: "light.{{['hue_color_lamp_1','hue_color_lamp_2'] | random }}"

not sure about the latter though, I use the former (now)… this is an old post

  lights_partying_on:
    alias: Lights partying on
    mode: restart
    sequence:
      service: light.turn_on
      data:
        entity_id: >
          light.{{['frontdoor','dining_corner',
                   'lounge_chair_long','home_theater','world_map']|random}}
        rgb_color: >
          {{[range(0,255)|random,range(0,255)|random,range(0,255)|random]}}
        brightness: >
          {{range(50,250)|random}}
        transition: >
          {{range(1,3)|random}}
3 Likes

thank you.

Works perfectly.

/Joern