Sensor minus a value

Is it possible to have a new sensor that takes and existing sensor and deducts a fixed number from it ? I have a weight in KG coming in from a Withings Sensor and i want to count down on it showing total weight loss.

Is this possible?

Thanks, have created a new sensor that gives me my weight in lbs, I now want to have a sensor that shows weight lost from my starting weight, so can i use a template sensor like that ?

i.e. 202 lbs - my current weight in lbs sensor ?

  - 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'

This gives me an unknown value in the new sensor.

The float goes outside the parenthesis. Also, word of advice: Donā€™t over complicate your templates with parenthesis. It makes it harder to understand. Parenthesis in code follow the same order of operation as math. Do you need parenthesis around a number to subtract 2 numbers? e.g. (5) - (4)? No, so donā€™t add it in your templates. I see too many people do this and itā€™ll just confuse you in the long run.

  - 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'

got it working, and thanks for all the help. One last thing is it possible to round the result to 1 or 2 decimal places ? its the sensor.steve_weight_lbs that comes in from withings in 12 decimal places.

  - 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 ) round(1) }}"
        unit_of_measurement: 'lbs'

That does not compile, this is how it looks now and is working (but obviously gives a silly amount of decimal places)

  - 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'

Pipe before round

  - 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 ) | round(1) }}"
        unit_of_measurement: 'lbs'

Ya, as @Mattias_Persson said, forgot the pipe

also, you donā€™t need those parenthesis around

(states('input_number.box1')|float)

it does nothing for the code.

compiles but still giving a huge number

image

Thatā€™s epsilon. Code is funky and does that sometimes. Use this:

I broke things out a bit so you can see the math.

  - platform: template
    sensors:
      steve_total_weight_loss:
        friendly_name: "Steve's Weight Loss"
        value_template: >
          {% set box1 = states('input_number.box1') | float %}
          {% set weight = states('sensor.steve_weight_lbs') | float %}
          {% set value = ( box1 - weight ) | round(1) %}
          {{ '{:.1f}'.format(value) }}
        unit_of_measurement: 'lbs'
1 Like

perfect Petro !!! and thanks for showing me how to break the code out better !!!

'#legend