Math operations for sensor values

Hello again, sorry for bothering you guys again :slight_smile:

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.” :slight_smile:

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 :slight_smile: )