Eliminate the text description from the attribute of a sensor

I would like to use the numeric portion of a string sensor attribute to display as a graph on a dashboard. The Value String I’m attempting to create a template from is {{ state_attr(‘sensor.front_multilevel_sensor_1’, ‘luminance’) }} which results in “999 lux”. I simply want to remove the ’ lux’ and use the numeric value as an Integer. Thanks in advance for your assistance.

You can use a Helper to template the sensor

Settings>Devices & services> Helpers

Thank you. Yes, I understand the Template sensor. But I need help with the Jinja statement. I need an addition to the statement I have now to modify the raw result (999 lux) of the attribute to eliminate the text and convert the remaining numerics to an integer. Thanks again for your help.

I made some progress, I discovered “Replace”. I updated the Jinja statement: {{ state_attr(‘sensor.front_multilevel_sensor_1’, ‘luminance’) | replace(" lux",“”) | int }} THis works, it eliminated the “lux”, but it still returned a ‘string’. The ‘int’ appears to have had no affect. I created a Numeric entity from the result and it appears to work but I’m still wondering what I’m missing here. Again, thanks for your patience with a relative newby.

The reason I suggested the template is because it will remove the lux

What do you see in developer tools with this

{{ state_attr('sensor.front_multilevel_sensor_1', 'luminance') | int(0) }}

It doesn’t matter. Every entity’s state value is handled as a string.

INT() returns this error: ValueError: Template error: int got invalid input ‘900 lux’ when rendering template ‘{{ state_attr(‘sensor.front_multilevel_sensor_1’, ‘luminance’) | int() }}’ but no default was specified If I add a 0, INT(0) I get zero, which I assume is the default.

May be presented as “… | int(default=…)”.

It’s kinda weird that the value for that illuminance attribute includes the unit of measurement, but if you want to remove it, you could do something like:
{{ state_attr('sensor.front_multilevel_sensor_1', 'luminance') | default('', true), replace(' lux', '') }}

1 Like