Create binary sensor from Light state/Brightness

I am trying to trigger Automation based on the State and brightness of a couple of lights:
In my Room i have 3x smart bulbs and a light controlled by an ESPHome. I am able to controll the bulbs with a controller directly not over HA but cannot control the ESPHome light. To do this I am trying to create a Template binary sensor that is triggered when any of the lights is (on and at 100% brightness).

{% if is_state('light.gu10_1', 'on') and (state_attr('light.gu10_1', 'brightness') | int ) == '100' %}
    {{ true }}
{% endif %}

I am doing it wrong because it always returns “Off”. Can someone help me fugure this out?

When the light is off, its brightness is not 0 but none, and your template will fail. Though the and should prevent that, it is safest to have a default value.

A binary sensor template also must return a boolean, while this template will return true or a blank string, which is not allowed.

Edit: Also as suggested by @tom_l, the brightness value is not in percent but an 8-bit integer. Be sure to check that your lights actually cap out at 255, as I know at least many Zigbee lights cap out at 254 for some weird reason.

(There is no brightness_pct attribute, at least not according to the Light entity developer documentation, it is only a parameter that can be used on the light.turn_on action which is then translated to the 0–255 range.)

1 Like

Remove the quotes from around 100. That is a string, not an integer. You also don’t need the if statement and you should supply a default for the int() filter for when the light is off and the attribute does not exist. e.g. This resolves to true or false:

{{ is_state('light.gu10_1', 'on') and state_attr('light.gu10_1', 'brightness') | int(0) == 100 }}

Be aware that the brightness attribute has a value from 0 to 255, the brightness_pct attribute is 0 to 100.

1 Like

Hmm.
https://www.home-assistant.io/integrations/light/

It could only be available for the service call / action.

It is only available as a parameter on the action/service call, or at least it is not specified as a light property in the developer documentation I linked and it is not available as an attribute on any of my light entities which are provided by the ZHA and Philips Hue integrations. I assume other integrations could choose to provide it as an attribute if they wished.