Newbie question - subtract one sensor value from another

Have two sensors:

  • platform: mqtt
    state_topic: “weather/3/temp”
    name: “s3_temp”
    qos: 0
    unit_of_measurement: “°C”
  • platform: yr
    name: outside_temp
    monitored_conditions:
    • temperature

That both give value in °C.
I would like to make a third sensor with the difference between outside_temp and s3_temp.
In other words: outside_temp - s3_temp

I have tried a few different ways but can’t seem to get it to work.
Anyone here that could help me?

you want to make a new sensor of type template.

Your template would be {{states.sensor.outside_temp.state - states.sensor.s3_temp.state}}

You can check the template in the templates developer tool (it is on the bottom left of your HA instance frontend). You may have to cast the state for each sensor to a float or int. This can be done by using |int or |float.

Thank you very much! That was great information, that tool was great!

This works:

  • platform: template
    name: in_vs_out
    sensors:
    outside_vs_inside_temp:
    value_template: ‘{{(states.sensor.s3_temp.state | float ) - (states.sensor.outside_temp_temperature.state | float)}}’
    friendly_name: “Indoor Vs Outdoor”

Thanks again!

2 Likes

Any idea why this gives an unknown in the value ?

  - platform: template
    sensors:
      steve_total_weight_loss:
        friendly_name: "Steve's Weight Loss"
        value_template: "{{( states('input_number.box1'|float) - states('sensor.steve_weight_lbs'|float))}}"
        unit_of_measurement: 'lbs'