Set a lighting effect based on the color of an RGB light in Homekit

Hello, I use Homekit mainly as the front end of my home assistant installation as I am heavily involved in the apple ecosystem. However, one downside that comes with this is that Homekit doesn’t offer lighting effects for RGB lights, whereas home assistant does. I was wondering if there was a way to create a configuration that would detect when I have a specific color set to automatically set a lighting effect for my RGB lights. This would make it possible to save that specific color in homekit as a preset below the color wheel, and use that to set different lighting effects for my lights. I’m fairly new to home assistant, so I’m not sure if this would be something easy or difficult to do, if possible at all. Any feedback is appreciated, thank you.

Sure, just create an automation to check for that color and have it do something.

BUT, homekit can call scripts. Why not just use that instead of some hacky thing that is prone to give adverse effects?

Well, I guess the reason would be because scripts are just switches in homekit, so you can’t pass any data. You’d have to create a script for each effect…

- alias: homekit_rbg_hack
  trigger:
    platform: state
    entity_id: light.rgb_light
  condition:
    # Only care if the light is on
    - condition: template
      value_template: "{{ is_state(trigger.entity_id, 'on') }}"
    # Only care if the light reports rgb_color
    - condition: template
      value_template: "{{ state_attr(trigger.entity_id, 'rbg_color') != 'None' }}"
    # Compare the colors...not sure how to do list compare
    # If it gives color_name of the color, would be way easier to use that....
    - condition: template
      value_template: >
        {% set r = state_attr(trigger.entity_id, 'rgb_color')[0] | int %}
        {% set g = state_attr(trigger.entity_id, 'rgb_color')[1] | int %}
        {% set b = state_attr(trigger.entity_id, 'rgb_color')[2] | int %}
        {{ r == 255 and g == 0 and b == 0 }}
  action:
    # However your RGB lights (whatever they are) do effects. 
    - service: light.effect
      data_template:
        # Hopefully the effect doesn't change the rbg color....that could get messy lol
        entity_id: "{{trigger.entity_id }}"
        # The effect depends on what the color was. 
        effect: # What effect do you want? 

The condition template for comparing the color is kind of ugly. Someone smarter than me could chime in with how you compare that easily. And this is just an example of how you might do it. I would have to think longer to know the best way.

I’d probably create a template sensor that follows the rgb_color who’s state output is the effect. Something like Petro’s solution here. Map values for an input_select?

Then you could use that template sensor in both the condition and the action without having to repeat the dumb templates and color comparing.

I know this is all going to be a lot for you just starting out. Especially the templates. My solutions are rarely the ‘best way to do it’, but I go for ‘make it work, THEN make it better at some point in the future, maybe, or not, who’s keeping track’

1 Like