Automation - Copy Other light state

Hey

I have a simple automation setup which alerts me when the washing machine is finished based on power use - it sets the light above the washing machine to a different colour - there’s a door sensor on the machine so it knows when its been emptied and at the moment simply turns that light off.

Instead of turning off the light how easy Is it to grab the status of another light and set that light to match?

Thanks!

  - service: light.turn_off
    data: {}
    target:
      device_id: bc39cfab72da481f54867e4b1a865482

This took me more time than I’d like to admit. If I get bored later in the week I may come back to it, but for now I’m just gonna leave you with what I have and you can tinker with it.

I tested this to work with color_mode: xy, but should work with rgb and hs as well (but will not work with color_temp without further editing). It passes json data (so it can dynamically use xy/rgb/hs in one service) to set the color and brightness in your laundry room to match those in your bedroom.

Obviously, change the light entities to the names of your lights.

  - service: light.turn_on
    data: |
      {
        "entity_id" : "light.laundry",
        "{{states['light.bedroom'].attributes.color_mode}}_color" : {{state_attr('light.bedroom',states['light.bedroom'].attributes.color_mode+'_color')}},
        "brightness" : {{state_attr('light.bedroom','brightness')}}
      }

Hey

Thank you did give this a go - at the moment it’s not seeming to do anything - have pasted the full automation - it should be setting light.unit_left_bottom to the same as light.unit_right_bottom

I will keep tinkering with it too - thank you again!


alias: "Helper: Set Washing Machine Status to Off"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.washing_machine_door_sensor_contact
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: input_select.washing_machine_status
    state: Unemptied
action:
  - service: notify.mobile_app_marcs_iphone
    data:
      message: Washing machine was emptied 🧺
    enabled: false
  - service: input_select.select_option
    data:
      option: "Off"
    target:
      entity_id: input_select.washing_machine_status
  - service: light.turn_on
    data: |
      {
        "entity_id" : "light.unit_left_bottom",
        "{{states['light.unit_right_bottom_bottom'].attributes.color_mode}}_color" : {{state_attr('light.unit_right_bottom',states['light.unit_right_bottom'].attributes.color_mode+'_color')}},
        "brightness" : {{state_attr('light.unit_right_bottom','brightness')}}
      }
mode: single

states['light.unit_right_bottom_bottom'].attributes.color_mode}}_color
                               ^^^^^^^

Remove the second _bottom.

Here’s an updated script that should work for color_mode of xy, rgb, hs, or color_temp (I tested it on xy and color_temp).

Just one place to put the light entities now, which I filled in for you.

FYI- it’ll throw an error if your copy_light is off when the script is called.

  - variables:
      copy_light: "light.unit_right_bottom"
      target_light: "light.unit_left_bottom"
      cmode: "{{states[copy_light].attributes.color_mode}}"
  - service: light.turn_on
    data: |
      {
        "entity_id" : "{{target_light}}",
        "{{cmode+'_color' if cmode|length<4 else 'color_temp'}}" : {{state_attr(copy_light,iif(cmode|length<4,cmode+'_color','color_temp'))}},
        "brightness" : {{state_attr(copy_light,'brightness')}}
      }

If the other light happens to be off, it won’t have a brightness attribute or any color-related attribute. The template should be designed to gracefully handle that possibility (otherwise it will fail with an error when it references a non-existent attribute).

This did it thank you - Its not a huge issue that it won’t do anything if the other light is off for me - would it crash out the automation and stop it doing other things to should it be OK in that respect?

anything after that code will not execute if there’s an error from the copy_light being off.

just nest my code (variables and service) in an if unit_right_bottom state == on action if that’s a problem.

feel free to mark my above code as the answer if this has taken care of everything you need.

1 Like

It will generate an error message in the Log and terminate the automation at the failed action. It will also report an error if the target light’s state is unavailable.

Generally speaking, after you have created an automation that achieves your goal, you should consider all of the failure situations it may encounter and then enhance the automation to mitigate them. It’s the difference between what works well ‘all of the time’ versus ‘only under ideal conditions’.