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.****

  • 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).

    • CondorWonder@lemmy.ca
      link
      fedilink
      English
      arrow-up
      3
      ·
      9 days ago

      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) }}