Template to read brightness and multiply it to use on a second light?

Currently, my Tuya ceiling light has a brightness sensor set up:

sensor:
  - platform: template
    sensors:
      brightness_decke:
        friendly_name: "Brightness Ceiling"
        value_template: '{{ state_attr("light.ceiling", "brightness") * 1.5 }}'

This generally works… a second light (LED strip) is using an automation to change its brightness to match the ceiling light, but with a factor of 1.5, since I want it to be brighter than the ceiling light.

alias: LED Strip Brightness match
description: ''
trigger:
  - entity_id: light.ceiling
    platform: state
    attribute: brightness
condition:
  - condition: device
    type: is_on
    device_id: 6ef81025fd62944bce6a24193850779e
    entity_id: light.ceiling
    domain: light
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 10
  - data_template:
      brightness: '{{ states.sensor.brightness_decke.state }}'
    entity_id: light.led_strip_1
    service: light.turn_on
mode: single

However, my log is repeatedly showing this:
TemplateError('TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'') while processing template 'Template("{{ state_attr("light.ceiling", "brightness") * 1.5 }}")' for attribute '_attr_native_value' in entity 'sensor.brightness_decke'

No matter if the light is on or not.
Any idea how to clean this up properly?

1 Like
value_template: >
  {% set bri = state_attr('light.ceiling', 'brightness')|float(none) }}
  {{ bri * 1.5 if bri != none else none }}

Also in your automation change this:

  - data_template:
      brightness: '{{ states.sensor.brightness_decke.state }}'

to this:

  - data:
      brightness: '{{ states('sensor.brightness_decke') }}'

Hrm… when I try to apply this, I get:
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘}’) for dictionary value @ data[‘sensors’][‘brightness_decke’][‘value_template’]. Got “{% set bri = state_attr(‘light.ceiling’, ‘brightness’)|float(none) }} {{ bri * 1.5 if bri != none else none }}\n”. (See ?, line ?).
from the configuration check.

Because this:

{% set bri = state_attr('light.ceiling', 'brightness')|float(none) }}

Should be:

{% set bri = state_attr('light.ceiling', 'brightness')|float(none) %}
1 Like

That did the trick, thank you.

1 Like