this post was submitted on 05 Nov 2024
13 points (100.0% liked)

homeassistant

12102 readers
14 users here now

Home Assistant is open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server. Available for free at home-assistant.io

founded 1 year ago
MODERATORS
 

Solved!

Solution was to create a group and perform an action on that:

action: light.turn_on
target:
  entity_id: light.kitchen_cabinet_sink
data_template:
  brightness_pct: "{{100*state_attr('light.kitchen_sink_ceiling','brightness')/255}}"

Original:

Trying to run an automation to match one light's state (on/off/dim) to another's. Have this currently:

alias: Sync cabinet lights with sink light
if:
  - condition: device
    type: is_on
    device_id: [something]5710
    entity_id: [something]a438
    domain: light
then:
  - type: turn_on
    device_id: [something]b447
    entity_id: [something]470f
    domain: light
    brightness_pct: 100
else:
  - type: turn_off
    device_id: [something]b447
    entity_id: [something]470f
    domain: light

That works fine to turn the lights on or off, and I have triggers in the automation for that and changes in brightness. But using a non-static number for brightness_pct (yes, I know I'll probably have to math the 0-100 scale instead of 0-255) is giving me trouble. When I try something like this:

alias: Sync cabinet lights with sink light
if:
  - condition: device
    type: is_on
    device_id: [something]5710
    entity_id: [something]a438
    domain: light
then:
  - type: turn_on
    device_id: [something]b447
    entity_id: [something]470f
    domain: light
    brightness_pct: {{state_attr("light.kitchen_sink_ceiling", "brightness")}}
else:
  - type: turn_off
    device_id: [something]b447
    entity_id: [something]470f
    domain: light

I have also tried {{states.light.kitchen_sink_ceiling.attributes.brightness}} instead. Both seem to have the correct value when I play around in the developer tools. But when I put it in the automation, I get an error that a float value was expected. I see some similar issues online, but it always seems to be in a different context and people fix it by changing some value I never had.****

top 11 comments
sorted by: hot top controversial new old
[–] [email protected] 4 points 2 weeks ago (1 children)

Out of curiosity, is it something as simple as needing to wrap the template in quotes? I may be mixing up my YAML with the Ansible work I've been doing, but I think you need to have templates double quoted like this in order to resolve the jinja2 properly: "{{ state_attr('light.etc', 'brightness') }}

I don't think you need the quotes in the Templates section of dev tools but you do in YAML files. I could be wrong though, let me know if you try it.

Definitely use the state_attr() form over states.etc.etc form. I think there's something about how HA handles startup that may mess with templates if you use states.etc.etc .

[–] [email protected] 1 points 2 weeks ago (1 children)

Combining this with similar comments, plus adding in the math to convert to a percent, I tried this:

brightness_pct: "{{state_attr('light.kitchen_sink_ceiling', 'brightness') | float(0) /255*100}}"

Still getting the same message

Message malformed: expected float for dictionary value @ data['brightness_pct']

For what it's worth, if I try to set brightness instead of brightness_pct, I get a different message

Message malformed: extra keys not allowed @ data['brightness']

(I'm assuming that device just doesn't accept a brightness attribute - not a big deal to math it out though)

[–] [email protected] 2 points 2 weeks ago

Taking a different approach of starting simple and working up,

100.0 works

{{100.0}} does not work

"{{100.0}}" also does not work

[–] [email protected] 3 points 2 weeks ago (2 children)

Have you considered just putting the lights in the same group? If you can make your switches control the group you should be all set.

https://www.home-assistant.io/integrations/group/

You might also be able to link them together on the Z-Wave/Zigbee level, depending on your hardware.

[–] [email protected] 2 points 2 weeks ago

That, plus a couple other things I saw in different places, ended up doing exactly what I wanted! I posted my final solution above. Thanks for finding a piece to the puzzle!

[–] [email protected] 1 points 2 weeks ago

That just might work! It's definitely an easy way to control both at the same time. I just need to figure out a way to change the group's dimness when the dimness on one light's physical switch (a Kasa, unlike the Zigbee one next to it). Maybe an automation for that, targeting the group instead of a single device

[–] InEnduringGrowStrong 1 points 2 weeks ago (1 children)

Hmmm if it's just complaining about expecting a float, you could maybe get away with simply multiplying by 1.0

{{state_attr("light.kitchen_sink_ceiling", "brightness") * 1.0}}

I think... {{state_attr("light.kitchen_sink_ceiling", "brightness") | float}} also works these days.

My lights return brightness=None when they're off... and None * 1.0 probably breaks something, so this might be more consistent: {{(state_attr("light.kitchen_sink_ceiling", "brightness") or 0) | float}}

PS: I can't say much about brightness_pct, I normally use brightness instead (0-255).

[–] [email protected] 3 points 2 weeks ago (2 children)

You’ll need to use | float(0) in templates. All state values and attributes start out as strings. Also setting a default value in the float(#) cast will ensure templates don’t break when the value is invalid.

That means use this style: {{ state\_attr("light.kitchen\_sink\_ceiling", "brightness") | float(0) }}

[–] InEnduringGrowStrong 2 points 2 weeks ago

If OP somehow needs to translate between beignes and brightness_pct.
Probably something like this:

brightness: "{{state_attr('light.kitchen_sink_ceiling', 'brightness') | float(0) /255*100}}"
[–] InEnduringGrowStrong 2 points 2 weeks ago

I think OP might just have needed the quotes around the template brackets in the yaml.

[–] InEnduringGrowStrong 1 points 2 weeks ago* (last edited 2 weeks ago)

Oh.. I think you also need double quotes around template brackets when used as the value in a service call..? Which conflicts with the quotes around the entity and attribute so just use single quotes there.

brightness_pct: "{{state_attr('light.kitchen_sink_ceiling', 'brightness')}}"

Just whipped up a partial example with my living room lights. It is missing a trigger and an else butI focused on theactionyou had trouble with.
Using brightness instead of brightness_pct seemed simpler. (Or at least if both can usethe same attribute...)

alias: Example
description: ""
trigger: []
condition:
  - condition: state
    entity_id: light.living_room_floor_lamp_1
    state: "on"
action:
  - action: light.turn_on
    metadata: {}
    data_template:
      brightness: "{{state_attr('light.living_room_floor_lamp_1', 'brightness')}}"
    target:
      entity_id: light.living_room_floor_lamp_2
mode: single