Take over a device' attribute value to another device attribute in if-statement

Hi Guys,

I’m trying to create an automation that turns on a lamp as soon as another is turned on by a motion sensor. However, the lamp should take over the attribute values of the previously turned on lamp, such as “brighness” and “color_temp” - and that’s the point where I get stuck.

I tried the folowing:

- alias: Badezimmer - Beleuchtung - Waschbecken (ON)
  trigger:
  - entity_id: light.badezimmer_wc
    for: 00:00:01
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: light.badezimmer_dusche_1
    for: 00:00:01
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: light.badezimmer_dusche_2
    for: 00:00:01
    from: 'off'
    platform: state
    to: 'on'
  action:
  - data_template:
      entity_id: light.badezimmer_waschbecken
      brightness: >
        {% if is_state('light.badezimmer_dusche_1', 'on') %}
          '{{ states.light.badezimmer_dusche_1.attributes["brightness"] }}'
        {% elif is_state('light.badezimmer_dusche_2', 'on') %}
          '{{ states.light.badezimmer_dusche_1.attributes["brightness"] }}'
        {% elif is_state('light.badezimmer_wc', 'on') %}
          '{{ states.light.badezimmer_wc.attributes["brightness"] }}'
        {% else %}
          255
        {% endif %}
      color_temp: >
        {% if is_state('light.badezimmer_dusche_1', 'on') %}
          '{{ states.light.badezimmer_dusche_1.attributes["color_temp"] }}'
        {% elif is_state('light.badezimmer_dusche_2', 'on') %}
          '{{ states.light.badezimmer_dusche_1.attributes["color_temp"] }}'
        {% elif is_state('light.badezimmer_wc', 'on') %}
          '{{ states.light.badezimmer_wc.attributes["color_temp"] }}'
        {% else %}
          255
        {% endif %}
    service: light.turn_on

Espacially this part of my automation does not work:

{% if is_state('light.badezimmer_dusche_1', 'on') %}
          '{{ states.light.badezimmer_dusche_1.attributes["color_temp"] }}'
{% endif %}

Does anybody know how to read out a device’ attribute value and set this value to another device’ attribute in an if-statement?

Jan

Why do you need to do this? How do you turn on the lights “badzimmer_wc”, “badzimmer_dusche_1” and “badzimmer_dusche2”? With a switch connected to home assistant or with an automation?

Imagine a bathroom divided into 3 small rooms (look at my masterpiece below).

If you enter ROOM 1 a motion sensor (Philips Hue) turns on the light in ROOM 1 (“light.badezimmer_waschbecken”) for a duration of 2 minutes.

If you enter ROOM 3, another motion sensor turns on the lights in ROOM 3 (“light.badezimmer_dusche_1” and “light.badezimmer_dusche_2”) for a duration of 3 minutes and disables the motion sensor in ROOM 1 to prevent the by Philips Hue automated switch off of the light in ROOM 1 (“light.badezimmer_waschbecken”).

Now, if you still are in ROOM 3 and you do not move long enough so the motion sensor “thinks” there is no presence - the lights in ROOM 3 and ROOM 1 will be turned off. After the lights turned off you maybe want to turn it on again with a movement. So if you do that, the light in ROOM 1 should be switched on and take over the brightness and color temperature of the lights in ROOM 3.

The brighness and temperature of the lights are depending on the daytime.

It’s a little bit complex for a little room like that… :smiley:

      _____________________________
      |               |           |
      |               |           |
      |     ROOM 1    |   ROOM 2  |
            (sink)       (toilet) |
                                  |
      |__       ______|___________|
      |                           |
      |                           |
      |                           |
      |          ROOM 3           |
      |         (shower)          |
      |                           |
      |                           |
      |___________________________|

Why not just include the light in room 1 in the action when the motion sensor in room 3 detects motion?

Cause I configured a timeout per motion sensor in the Philips Hue app so the lights smoothly be turned off by the Philips Hue Bridge.

The sensor in ROOM 3 always would turn off the light in ROOM 1, even if there is a movement in ROOM 1.

„Cross-over“ configurations like this are not applicable in Philips Hue.

Oh, I somehow assumed that you do all these automations within home assistant not with the Hue Bridge.

The code you posted should work. What is the problem, does it turn the light on, but not with the desired brightness or does it not turn the light on at all?

I haven’t gone thru the whole thing yet but I see a few things that you could do to simplify things:

I see that you are going “from off to on for 1 second”. Why? since the motion sensor likely can only be in two states (either on or off) and no other state then you don’t really need the “from” part.

And I think the “for 1 second” is superfluous unless it’s there for some specific quirk in the functionality of your sensor that I’m not aware of. So you can probably remove that too.

And you don’t need to use the ["…"] notation for the attributes. just use a . notation and it will work fine (again unless I’m missing some quirk in how your lights report their attributes).

and try removing the quotation marks from around the brackets in template between the “if-else” statements. by putting quotes around those you are telling the system to use the literal string ‘{{ states.light.badezimmer_dusche_1.attributes[“color_temp”] }}’. And that won’t work.

so putting all of that together try this:

- alias: Badezimmer - Beleuchtung - Waschbecken (ON)
  trigger:
    - entity_id: light.badezimmer_wc
      platform: state
      to: 'on'
    - entity_id: light.badezimmer_dusche_1
      platform: state
      to: 'on'
    - entity_id: light.badezimmer_dusche_2
      platform: state
      to: 'on'
  action:
    - data_template:
        entity_id: light.badezimmer_waschbecken
        brightness: >
          {% if is_state('light.badezimmer_dusche_1', 'on') %}
            {{ states.light.badezimmer_dusche_1.attributes.brightness }}
          {% elif is_state('light.badezimmer_dusche_2', 'on') %}
            {{ states.light.badezimmer_dusche_1.attributes.brightness }}
          {% elif is_state('light.badezimmer_wc', 'on') %}
            {{ states.light.badezimmer_wc.attributes.brightness }}
          {% else %}
            255
          {% endif %}
        color_temp: >
          {% if is_state('light.badezimmer_dusche_1', 'on') %}
            {{ states.light.badezimmer_dusche_1.attributes.color_temp. }}
          {% elif is_state('light.badezimmer_dusche_2', 'on') %}
            {{ states.light.badezimmer_dusche_1.attributes.color_temp }}
          {% elif is_state('light.badezimmer_wc', 'on') %}
            {{ states.light.badezimmer_wc.attributes.color_temp }}
          {% else %}
            255
          {% endif %}
      service: light.turn_on
2 Likes

To leave nothing unanswered…

HA throws the following error:

Wed Oct 09 2019 23:28:14 GMT+0200 (Mitteleuropäische Sommerzeit)

Error while executing automation automation.badezimmer_beleuchtung_waschbecken_on. Invalid data for call_service at pos 1: expected int for dictionary value @ data['color_temp']
Verbindung getrennt. Verbinde erneut...

Thank you very much for your detailed instructions! :slight_smile:
After removing the quotation marks it works fine!

I think I will now optimize my automations a bit along to your description. :grimacing:
But the 1 second of each trigger has a sense, cause my lights are fading on with a transition time of a few milliseconds. If I remove the 1 second the automation would probalby trigger immediately with wrong values for brightness and color temperature.

that’s good! :+1:

I think you marked the wrong post as the solution. :wink:

An alternative approach:

- alias: 'Badezimmer - Beleuchtung - Waschbecken (ON)'
  trigger:
    - platform: state
      entity_id:
        - light.badezimmer_wc
        - light.badezimmer_dusche_1
        - light.badezimmer_dusche_2
      to: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.badezimmer_waschbecken
        brightness: "{{ trigger.to_state.attributes.brightness }}"
        color_temp: "{{ trigger.to_state.attributes.color_temp }}"
2 Likes

I marked your post as the solution… :thinking: