Picnic
(DjR)
September 27, 2019, 5:28pm
1
Can’t seem to get my values to round. I’ve tried adding and removing brackets and changing the round value.
- platform: template
sensors:
consumed_power_1:
value_template: >
{{ (states('sensor.solar_power_1')|float
+ states('sensor.utility_power_1')|float) | round(2)}}
friendly_name: 'Consumed Power 1'
unit_of_measurement: 'W'
consumed_power_2:
value_template: >
{{ (states('sensor.solar_power_2')|float
+ states('sensor.utility_power_2')|float)) | round(2)}}
friendly_name: 'Consumed Power 2'
unit_of_measurement: 'W'
total_solar_power:
value_template: >
{{ (((states('sensor.solar_power_1')|float
+ states('sensor.solar_power_2')|float)) / 1000) | round(2) }}
friendly_name: 'Total Solar Power'
unit_of_measurement: 'kW'
total_utility_power:
value_template: >
{{ (((states('sensor.utility_power_1')|float
+ states('sensor.utility_power_2')|float)) / 1000) | round(2) }}
friendly_name: 'Total Utility Power'
unit_of_measurement: 'kW'
Maybe try the format filter instead:
total_solar_power:
value_template: >
{{ '%0.2f' | format(
(states('sensor.solar_power_1')|float
+ states('sensor.solar_power_2')|float) / 1000) }}
friendly_name: 'Total Solar Power'
unit_of_measurement: 'kW'
Picnic
(DjR)
October 2, 2019, 6:48pm
3
This is what ended up working.
- platform: template
sensors:
solar_power_1:
value_template: >
{{ (states('sensor.solar_power_1_raw')|float - 3) |round(2) }}
friendly_name: 'Solar Power 1'
unit_of_measurement: 'W'
solar_power_2:
value_template: >
{{ (states('sensor.solar_power_2_raw')|float - 3) |round(2) }}
friendly_name: 'Solar Power 2'
unit_of_measurement: 'W'
consumed_power_1:
value_template: >
{{ (states('sensor.solar_power_1')|float
+ states('sensor.utility_power_1')|float) |round(2) }}
friendly_name: 'Consumed Power 1'
unit_of_measurement: 'W'
consumed_power_2:
value_template: >
{{ (states('sensor.solar_power_2')|float
+ states('sensor.utility_power_2')|float) |round(2) }}
friendly_name: 'Consumed Power 2'
unit_of_measurement: 'W'
total_consumed_power:
value_template: >
{{ (states('sensor.consumed_power_1')|float
+ states('sensor.consumed_power_2')|float)| round(2) }}
friendly_name: 'Total Solar Power'
unit_of_measurement: 'W'
Honestly, you haven’t fixed the problem, you’ve only worked around it (or maybe I should say avoided it temporarily.) The issue is the way floats work. They don’t have infinite precision, so sometimes a result (from rounding) will actually cause what you saw. The only way to really fix the problem is to control how the output is formatted when converted to a string, which is what I suggested.