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?
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'
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
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'
perfect Petro !!! and thanks for showing me how to break the code out better !!!
'#legend