Creating a sensor with light brightness as a percentage

This issue is quite nit-picky, but it’s driving me crazy.

I created a sensor to show my light brightness as a percentage, rather than 1-255 scale. This is my value template:

{{ state_attr('light.office_light','brightness')|int(1)*100//255 }}

At this point in time, it shows: 38%.

But if I click the light, it shows the brightness at 39%:

Any idea how to update my value template to match what is shown on the light itself?

I do not know the code of the light, but my guess is it is done as a float calculation instead of integer, and then rounded. I do not know why you chose 1 as a default for unavailable, I’d use 0 to assume off.

{{ (state_attr('light.office_light','brightness')|float(0)*100/255)|round(0) }}

Thank you! That works perfectly. And I don’t know why I used “int(1)” either. I don’t know YAML, so I’m just trying to get by based on what I can find online.

This is how you learn, step by step.

In order to have entities that can be anything, the state is a string. Home Assistant knows it should actually be a number if an entity has a unit of measure. Attributes can have any type, but still it is wise not to assume that.

In templates that means that string must be converted to a number for calculations. Templates use the jinja2 language, which has many relations to the python language Home Assistant is written in.

The float and int filters van do that conversion to either an integer or floating point value for you, and you use the | symbol (called pipe) to run the string through the filter to convert it. the int(1) and float(0) notations are saying to apply a default if the conversion isn’t possible. So if the sensor is not available, either 1 or 0 from the examples is used instead. 0 is a more logical default for brightness.

The state must be a string, attributes however do not. The brightness attribute of a light is an int when the light is on, but annoyingly it is not 0 when the light is off but instead none

Also Python/Jinja doesn’t really have the concept of int and float, they are all just a number. The only difference when filtering to either type is whether the decimals are cut off or not, or if testing is int / is float whether a number has decimals or not.

Doesn’t your light have the brightness_pct attribute?

Lights in general do not have a brightness_pct attribute, or at least none of mine have it (ZHA and Philips Hue integrations). While you can use it as a parameter to set the brightness during a light.turn_on action, you cannot retrieve it. It also is not listed as an existing attribute in the developer documentation for the Light entity.

Oh you are right, I’m pretty sure it used to be there, but not used it or looked for it a long while

It is there in the light.turn_on action. But the attributes hold only one single truth, and that is the one with the most accuracy. So the percentage is always a calculated value, and you can unfortunately not use the percentage directly in conditions.