Read RGB value from other device

2 days Ago I wrote an entire story about my rgb strips and 4 layers corrosponding as 1 through automations. By now I’ve managed to make a script to turn on the other 3 strips once the first one is turned on (yeah!) but reading values has always been an issue for me with hass (if only we could write in a real script language instead of pourly editing yaml, just my 2 cents). But I’m still looking for a way to match and more importantly slightly manipulate the set RGB colour to a slightly different RGB value. Could someone help me? My lights are as follows:

platform: flux_led
scan_interval: 10
automatic_add: False
devices:
  192.168.178.31:
    name: RGB_TV
    mode: "rgb"
  192.168.178.34:
    name: RGB_ART_LAYER_03
    mode: "rgb"
  192.168.178.32:
    name: RGB_ART_LAYER_01
    mode: "rgb"
  192.168.178.35:
    name: RGB_ART_LAYER_02
    mode: "rgb"
  192.168.178.33:
    name: RGB_ART_LAYER_04
    mode: "rgb"
  192.168.178.39:
    name: RGB_SLAAPKAMER
    mode: "rgb"

Where it’s concering the RGB_ART_LAYER_XX scripts… RGB_ART_LAYER_04 is controlled by either google assistant or just through HASS :slight_smile:

It’s not exactly clear what you’re asking. But have you looked at using Python Scripts?

EDIT: As an example of what you can do, check out this script I wrote to save & restore lights.

1 Like

Hi pnbruckner, first of all I didn’t know you could do Python in home assistant! So that’s already a great step ahead for me as a software engineer with years of experience in Javascript, PHP, C++, C#, ‘Adruino’ etc , I’ll look into your example later tonight when I get home!

What I basically want to do is:

RGB_ART_LAYER_04 is turned on or changes colour trigger script:
R = RGB_ART_LAYER_04.r
G = RGB_ART_LAYER_04.g
B = RGB_ART_LAYER_04.b

RGB_ART_LAYER_03.r = ( R / 1.3 )
RGB_ART_LAYER_03.r = ( B / 1.3 )
RGB_ART_LAYER_03.r = ( G / 1.3 )

etc or better yet apply a staturation to the total value etc etc but I think that this python thing can be very very usefull in this scenario! Any hints into getting the rgb from said light and a way to trigger the script upon state change or colour change is great but the rest I guess is just python

Thanks again!

Just to keep the terms straight in HA, an automation has a trigger, a script doesn’t. :slight_smile:

You can use a state trigger for the automation. If you don’t use to: or from:, then a state trigger “fires” when any part of an entity’s state changes, including attributes (such as rgb.) So, e.g.:

automation:
  - alias: Blah, blah, blah
    trigger:
      platform: state
      entity_id: light.RGB_ART_LAYER_04
    action:
      service: python_script.my_script

Within the script you’ll get the rgb (as a tuple), then the individual r, g & b values, using something like:

rgb = hass.states.get('light.RGB_ART_LAYER_04').attributes['rgb_color']
r, g, b = rgb

Of course you can pass parameters into the script (such as entity_id’s, etc.) depending on how flexible you want it to be.

Wow mate! you made my day… this sounds just what I need!!

1 Like

Thanks pnbruckner!! I’ve used your automation along with below python code which works like a charm (though I will tune this a bit more depending on the selected hue)

It gets the state and either turns on/off the entire ‘array’ of leds, if turned on it gets the HS_Color and manipulates the saturation (which currently can fail if below 0 making a negative saturation value) and applies these to the other 3 led strips!

# get the HS_COLOR from light.RGB_ART_LAYER_04
current_state   = hass.states.get('light.RGB_ART_LAYER_04').state

logger.info( "===================== artwork_autocolor.py =====================" )
logger.info( current_state )

if current_state == "on":
    brightness      = hass.states.get('light.RGB_ART_LAYER_04').attributes['brightness']
    hs              = hass.states.get('light.RGB_ART_LAYER_04').attributes['hs_color']
    h, s            = hs


    # Set saturation - 2 from original
    service_data_03 = {'entity_id': 'light.RGB_ART_LAYER_03', 'hs_color': [ h, s - 5 ], 'brightness': brightness }
    service_data_02 = {'entity_id': 'light.RGB_ART_LAYER_02', 'hs_color': [ h, s - 10 ], 'brightness': brightness }
    service_data_01 = {'entity_id': 'light.RGB_ART_LAYER_01', 'hs_color': [ h, s - 20 ], 'brightness': brightness }

    hass.services.call('light', 'turn_on', service_data_03, False)
    hass.services.call('light', 'turn_on', service_data_02, False)
    hass.services.call('light', 'turn_on', service_data_01, False)

    logger.info( service_data_03 )
    logger.info( service_data_02 )
    logger.info( service_data_01 )

else:
    hass.services.call('light', 'turn_off', {'entity_id': 'light.RGB_ART_LAYER_03' }, False)
    hass.services.call('light', 'turn_off', {'entity_id': 'light.RGB_ART_LAYER_02' }, False)
    hass.services.call('light', 'turn_off', {'entity_id': 'light.RGB_ART_LAYER_01' }, False)
    logger.info( "Turned all off" )
1 Like

FWIW, at the very least, I would only grab the state of light.RGB_ART_LAYER_04 once. Not only is that a bit more efficient, it makes sure that the rest of the script uses a consistent value (in case it changes quickly.) Also, when turning off, you can do all three lights with one service call.

# get the HS_COLOR from light.RGB_ART_LAYER_04
current_state   = hass.states.get('light.RGB_ART_LAYER_04')

logger.info( "===================== artwork_autocolor.py =====================" )
logger.info( current_state.state )

if current_state.state == "on":
    brightness      = current_state.attributes['brightness']
    hs              = current_state.attributes['hs_color']
    h, s            = hs


    # Set saturation - 2 from original
    service_data_03 = {'entity_id': 'light.RGB_ART_LAYER_03', 'hs_color': [ h, s - 5 ], 'brightness': brightness }
    service_data_02 = {'entity_id': 'light.RGB_ART_LAYER_02', 'hs_color': [ h, s - 10 ], 'brightness': brightness }
    service_data_01 = {'entity_id': 'light.RGB_ART_LAYER_01', 'hs_color': [ h, s - 20 ], 'brightness': brightness }

    hass.services.call('light', 'turn_on', service_data_03, False)
    hass.services.call('light', 'turn_on', service_data_02, False)
    hass.services.call('light', 'turn_on', service_data_01, False)

    logger.info( service_data_03 )
    logger.info( service_data_02 )
    logger.info( service_data_01 )

else:
    hass.services.call('light', 'turn_off', {'entity_id': [
        'light.RGB_ART_LAYER_03',
        'light.RGB_ART_LAYER_02',
        'light.RGB_ART_LAYER_01'] }, False)
    logger.info( "Turned all off" )

Great advice! will it help to not do logging once it works for speed also? :slight_smile: (just trying to figure out what the overhead of hass.* and logger.* is)

I’m sure it makes some difference, but how much, couldn’t really say. If you don’t need it anymore, then might as well remove it.