formicro
(Formicro)
March 21, 2021, 8:01pm
1
Hi all , it seems that my value_template is incorrectly coded, but i don’t find my error.
I aggregate energy sensors in another one, using this sensor code :
energy_total_kwh:
friendly_name: 'Energy Total'
value_template: "{{ (states('sensor.Priska_plus_01_cumul') | float + states('sensor.shelly_mobile_plug_energy') | float + states('sensor.shelly_parents_chauffage_energy') | float + states('sensor.shelly_chauffage_matteo_energy') | float + states('sensor.shelly_clim_enfants_energy') | float + states('sensor.shelly_couloir_chauffage_energy') | float + states('sensor.shelly_bureau_chauffage_energy') | float + states('sensor.shelly_bureau_energy') | float + states('sensor.shelly_1pm_clim_salon_energy') | float + states('sensor.shellyplug_s_ab8f3a_energy.') | float + states('sensor.shelly_sous_sol_frigos_energy') | float + states('sensor.shelly_sous_sol_chauffe_eau_energy') | float + states('sensor.shelly_frigo_plaque_terrasse_energy') | float) | round(2) }}"
unit_of_measurement: kWh
I used the round(2) attribute at the end of the template, but it’s not correctly rounded in frontend :
My utility_meter :
energy_total_monthly_ch_house:
source: sensor.energy_total_kwh
cycle: monthly
Any advice about my mistake ?
Thanks, regards, Thomas.
123
(Taras)
March 21, 2021, 8:49pm
2
I believe that’s due to a phenomenon known as Machine Epsilon . You can read more about it here:
What your seeing is called machine epsilon
This happens sometimes, you can’t do anything about it other than convert the number to a string (format or string filter).
Try this version and see if it works any better:
energy_total_kwh:
friendly_name: 'Energy Total'
unit_of_measurement: kWh
value_template: >
{% set sensors = expand('sensor.Priska_plus_01_cumul', 'sensor.shelly_mobile_plug_energy',
'sensor.shelly_parents_chauffage_energy', 'sensor.shelly_chauffage_matteo_energy',
'sensor.shelly_clim_enfants_energy', 'sensor.shelly_couloir_chauffage_energy',
'sensor.shelly_bureau_chauffage_energy', 'sensor.shelly_bureau_energy',
'sensor.shelly_1pm_clim_salon_energy', 'sensor.shellyplug_s_ab8f3a_energy',
'sensor.shelly_sous_sol_frigos_energy', 'sensor.shelly_sous_sol_chauffe_eau_energy',
'sensor.shelly_frigo_plaque_terrasse_energy') %}
{{ '{:.2f}'.format(sensors | map(attribute='state') | map('float') | sum) }}
formicro
(Formicro)
March 21, 2021, 10:02pm
3
Thanks for pointing me to the right direction, i did’nt know about Machine Epsilon.
I tried your syntax, but unfortunately got the same results.
I think this has been asked multiple times, but i imagine there’s no way to force the rounding in frontend ?
123
(Taras)
March 21, 2021, 10:12pm
4
I don’t think you’ve understood that it is rounding and what you seeing is a rare byproduct of the rounding process.
You can try the suggestion offered in the linked post (convert to string and slice off the undesirable parts).
formicro
(Formicro)
March 21, 2021, 10:15pm
5
U’re right, i’ll give it a shot tomorrow. Thanks again, i would have been looking for days if u didn’t mention this “rare byproduct”.