Copy the state of a light to another light

Hi all, I’ve got a bit stuck on an automation and wondering if anyone can help. I’m trying to make the 2 Lifx bulbs outside my courtyard toilet turn red when the toilet is in use to act as a sort of ‘busy’ indicator. When the toilet is no longer in use, I want the bulbs to copy the state of the other courtyard lights.

So far I have the following action:

  action:
    - service: light.turn_on
      data_template: 
        entity_id: 
          - light.outer_toilet_1
          - light.outer_toilet_2
        color_temp: >-
          {% if is_state("light.outer_lounge_sofa","on") and state_attr("light.outer_lounge_sofa","color_temp") != None %}
          {{ state_attr("light.outer_lounge_sofa","color_temp") }}
          {% else %}
          285
          {% endif %}

The problem with this is when the outer_lounge_sofa light is a non white color, then its attributes do not contain “color_temp” but instead contain rgb_color and hs_color. Similarly, when the outer_lounge_sofa is a white color, then its attributes do not contain either rgb_color or hs_color but instead contain “color_temp”.

This is why in the else section I have hard coded the warm white color of 285, but this is more of a failsafe than a solution.

Ideally I would like the following to work:

  action:
    - service: light.turn_on
      data_template: >-
          {% if is_state("light.outer_lounge_sofa","on") and state_attr("light.outer_lounge_sofa","color_temp") != None %}
         entity_id: 
          - light.outer_toilet_1
          - light.outer_toilet_2
        color_temp: {{ state_attr("light.outer_lounge_sofa","color_temp") }}
          {% elif is_state("light.outer_lounge_sofa","on") and state_attr("light.outer_lounge_sofa","rgb_color") != None %}
         entity_id: 
          - light.outer_toilet_1
          - light.outer_toilet_2
         rgb_color: {{ state_attr("light.outer_lounge_sofa","rgb_color") }}
         {% endif %}

however I haven’t figured out a way to encapsulate entity_id or rgb_color inside a template.
Any help much appreciated!

you could use the “choose:” function in the action section to do what you want.

I’m not at a place I can check the docs for the correct syntax but something like this should work:

action:
  choose:
    - conditions:
        - condition: state
          entity_id: light.outer_lounge_sofa
          state: on
        - condition: template
          value_template: '{{ state_attr("light.outer_lounge_sofa","color_temp") != None }}'
      sequence:
        - service: light.turn_on
          data_template:
            entity_id: 
              - light.outer_toilet_1
              - light.outer_toilet_2
            color_temp: '{{ state_attr("light.outer_lounge_sofa","color_temp") }}'
    - conditions:
        - condition: state
          entity_id: light.outer_lounge_sofa
          state: on
        - condition: template
          value_template: '{{ state_attr("light.outer_lounge_sofa","rgb_color") != None }}'
      sequence:
        - service: light.turn_on
          data_template:
            entity_id:
              - light.outer_toilet_1
              - light.outer_toilet_2
            rgb_color: '{{ state_attr("light.outer_lounge_sofa","rgb_color") }}'
1 Like

Wow I never knew that function existed! Excellent idea thank you I will try it out.

Great tip. It worked (eventually).

The issue I had was with setting the rgb_color to the same as the outer_lounge_sofa value. According to this thread the output of state_attr(“light.outer_lounge_sofa”,“rgb_color”) is a tuple converted into a string, however light.turn_on requires a list of RGB values to set the rgb_color.

So eventually, following this solution, I managed to set the rgb_color with the following:

- service: light.turn_on
            data_template:
              entity_id:
                - light.outer_toilet_1
                - light.outer_toilet_2
              rgb_color: [ '{{ state_attr("light.outer_lounge_sofa", "rgb_color")[0] }}',  '{{ state_attr("light.outer_lounge_sofa", "rgb_color")[1] }}',  '{{ state_attr("light.outer_lounge_sofa", "rgb_color")[2] }}' ]
              brightness: '{{ state_attr("light.outer_lounge_sofa","brightness") }}'

Pretty long winded for sure, but it works.

For completion sake, here is the full automation that is now working:

- id: '1600065937730'
  alias: Toilet Outside Lights Restore when Unoccupied
  description: ''
  trigger:
  - entity_id: input_select.toilet_activity
    from: active
    platform: state
    to: inactive
  condition: []
  action:
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ is_state("light.outer_lounge_sofa","off") }}'
        sequence:
          - service: light.turn_off
            data:
                entity_id:
                  - light.outer_toilet_1
                  - light.outer_toilet_2
          
      - conditions:
        - condition: template
          value_template: '{{ is_state("light.outer_lounge_sofa","on") }}'
        - condition: template
          value_template: '{{ state_attr("light.outer_lounge_sofa","color_temp") != None }}'
        sequence:
          - service: light.turn_on
            data_template:
              entity_id:
                - light.outer_toilet_1
                - light.outer_toilet_2
              color_temp: '{{ state_attr("light.outer_lounge_sofa","color_temp") }}'
              brightness: '{{ state_attr("light.outer_lounge_sofa","brightness") }}'
          
      - conditions:
        - condition: template
          value_template: '{{ is_state("light.outer_lounge_sofa","on") }}'
        - condition: template
          value_template: '{{ state_attr("light.outer_lounge_sofa","rgb_color") != None }}'
        sequence:
          - service: system_log.write
            data_template:
              level: info
              message: '{{ state_attr("light.outer_lounge_sofa","rgb_color") }}'
          - service: light.turn_on
            data_template:
              entity_id:
                - light.outer_toilet_1
                - light.outer_toilet_2
              rgb_color: [ '{{ state_attr("light.outer_lounge_sofa", "rgb_color")[0] }}',  '{{ state_attr("light.outer_lounge_sofa", "rgb_color")[1] }}',  '{{ state_attr("light.outer_lounge_sofa", "rgb_color")[2] }}' ]
              brightness: '{{ state_attr("light.outer_lounge_sofa","brightness") }}'
  mode: single
3 Likes

hey guys I’m struggling with my automation for a cover, in principle it’s the similar to the automation above, but it’s not working, maybe any1 got a proposal, please:
Left shutter should copy the current position of the left one and set the position.

- id: '1621446945771'
  alias: Treppenhaus Schalter
  description: ''
  trigger:
  - platform: state
    entity_id: cover.treppenhaus_rollo_rechts
    attribute: current_position
  action:
  - service: cover.set_cover_position
    data_template:
      entity_id: cover.treppenhaus_rollo_links
      current_position: '{{ state_attr("cover.treppenhaus_rollo_rechts","current_position") }}'
  mode: single

sorry I never replied to this. In case you never figured it out, did you see any errors in the log to help you figure out the problem?

On first look perhaps you simply needed to add ‘int’ to the current_position so that you are sure to you are providing a number. Like so:

current_position: '{{ state_attr("cover.treppenhaus_rollo_rechts","current_position") | int }}'