# Math operations for sensor values

Hello again, sorry for bothering you guys again

I made the cheap multi sensor using MQTT, but I think there is something I can improve.

The values in the light sensor go from 0 to 1023, where 0 is darkness and 1023 is maximum brightness.

I would like to transform those values in %, so I need to do some math with those values.

This should do it:

100 * ( 1023 - [sensor_value] ) / 1023 = % of light

The thing is that I donât know how to include that in the configuration.yaml file. I tried a lot of ways but nothing. This is what I have now:

state_topic: âbruh/sensornode1â
name: âMultisensor lightâ
unit_of_measurement: â%â
value_template: â{{ value_json.ldr }}â

Can someone help me or point me to the right direction?

Thanks !!!

1 Like

Create another template sensor that takes the value from the MQTT sensor and does the necessary calculations.

If youâre still unsure where to start, look here for template sensors:

and here for templating:

Also, take a look at jinja2 and itâs documentation.

Why are you subtracting the sensor value from 1023? Shouldnât it just be `sensor_value / 1023 * 100`?

How about changing the current value_template to:

``````value_template: '{{ (value_json.ldr|float/1023*100)|round(0) }}'
``````
2 Likes

Hi !

Because 1023 is darkness and 0 is total brightness. I would like the % of light, so I would have to inverse the original values.

Ok, but the reason I asked is because you originally said, âwhere 0 is darkness and 1023 is maximum brightness.â

In that case:

``````value_template: '{{ ((value_json.ldr|float-1023)/1023*100)|round(0) }}'
``````

BTW, you can change the argument of the round() function to the number of decimal places you want in the ultimate result. 0, which is the default, will create an integer (i.e., no fractional part.)

3 Likes

Thanks !!!

That worked like a charm !

1 Like

Hi everyone, I have an issue with Math operations and templating sensor values.

I want to have a customized sensor that shows the difference between two sensors (energy production and energy bought to electric company).
I coded:

``````- platform: template
sensors:
consumo_elec:
friendly_name: consumo
unit_of_measurement: W
value_template: "{{ (states('sensor.solar_consumo-vertido') | float) - (states('sensor.solar_potencia_activa') | float)}}
``````

but I have as output a copy of âsensor.solar_potencia_activaâ

Thanks for your help!

`sensor.solar_consumo-vertido` should perhaps be `sensor.solar_consumo_vertido` with two underscores? And youâre missing the quotes (`"`) at the end of the line.

Thanks a lot Troon. It was _ and not - âŚ I checked many times, but I was sooo dumb!!

1 Like

Hello, sorry for bringing this up again after such a long time, but I am having difficulty in doing almost the same thing as the person who started this thread with slightly different values and I am using the value of an input number helper.

This is what I have written but it does not seem to work.

``````data:
entity_id: light.living_room_lights
brightness_pct: "{{ ((states.input_number.lr_brightness|float-255)/255*100)|round(0) }}"
service: light.turn_on
``````

The error I am getting is this:

``````'{{ ((states.input_number.lr_brightness|float)/255*100)|round(0) }}' but no default was specified
``````

Any ideas?

This sensor might be in an unknown state.

Try this:

``````brightness_pct: "{{ ((states('input_number.lr_brightness') | float(0)-255)/255*100)|round(0) }}"
``````

Iâve been following some if the info here and itâs been really helpful

Iâve managed to get the maths to work ( / 20) but Iâd really like it to display 2 decimal point values whatever (since itâs ÂŁ)

I looked at the Jinja doc here but the `"%0.2f" |` led to the value breaking

``````- platform: template
sensors:
a_dadpoints_pounds: # this is the name of the sensor
value_template: "{{ states('input_number.a_s_dad_points')|float / 20 | float(2) }}"
e_dadpoints_pounds: # this is the name of the sensor
value_template: "{{ states('input_number.e_s_dad_points')|float / 20 | float(2) }}"
``````

Finally, Iâm only using this for display on a card, so Iâd actually then like to convert that to text and concatenate with the ÂŁ symbol at the front - struggling to find how to do thatâŚ

Thanks

``````ÂŁ{{ "%0.2f" % (states('input_number.a_s_dad_points')|float(0) / 20) }}
``````
1 Like

Fantastic - thanks, worked a treat (and I leaned a couple of things along the way )