The first three columns come from the Blitzortung integration, the fourth is a min value helper from the statistics integration :
You notice the “Distance éclair” shows decimals only when they are significant.
How can I achieve the same with the helper?
Here are my helper settings:
If I increase the number of decimals, I get non significant zeros. If I set it to 0, I get 2 when I should get 1.7
mirekmal
(Mirek Malinowski)
October 18, 2024, 5:38pm
2
I do not think it is possible with helper directly. Most likely you should create additional template sensor, just to be used to display these vaslues a syopu want. Then the template should use different number formatting (rounding) depending on the value of helper.
Ildar_Gabdullin:
trimming trailing zeros.
Can I setup the helper to trim trailing zeros?
Ildar_Gabdullin:
trimming trailing zeros.
I couldn’t find anything on Google with these keywords. Most people have the opposite problem, they want to display 3.00 instead of 3.0. Any chance you could share a link or a bit of code?
For instance, here’s one state to which I’d like to apply this treatment:
state: "{{ state_attr('climate.radiateur_salon', 'temperature') | float }}"
EDIT: jinja2 is so mysterious to me…
But in my template.yaml, I have to use quotation marks to define state, so it will always be a string. Should I convert it to float(1), check the value of the decimal and convert it again to in if it’s 0? That seems like a very complicated way of achieving this…
I tried comparing SENSOR | int with sensor | float and it seemed to work with the templating tool.
If temp is 7°C:
If it is 14.5°C:
But once in template.yaml:
- name: "Température cible radiateur salon"
unique_id: "aulhat_salon_radiateur_target_temperature"
device_class: temperature
state_class: measurement
unit_of_measurement: "°C"
state: >
{% if ((state_attr('climate.radiateur_salon', 'temperature') | float) - (state_attr('climate.radiateur_salon', 'temperature') | int)) == 0 %}
"{{ state_attr('climate.radiateur_salon', 'temperature') | int }}"
{% else %}
"{{ state_attr('climate.radiateur_salon', 'temperature') | float }}"
{% endif %}
icon: >
{% if (state_attr('climate.radiateur_salon', 'temperature') | float) < 14 %}
mdi:snowflake
{% elif (state_attr('climate.radiateur_salon', 'temperature') | float) == 21 %}
mdi:fire
{% else %}
mdi:thermometer-alert
{% endif %}
It doesn’t work:
Any input?
I also tried this syntax:
But it doesn’t work either:
I persevered and here’s a working code:
state: >
{% if (state_attr('climate.radiateur_salon', 'temperature') | int) == (state_attr('climate.radiateur_salon', 'temperature') | float) %}
{{ state_attr('climate.radiateur_salon', 'temperature') | int }}
{% else %}
{{ state_attr('climate.radiateur_salon', 'temperature') | float }}
{% endif %}
Or, if you prefer:
state: >
{% set court = (state_attr('climate.radiateur_salon', 'temperature') | int) %}
{% set long = (state_attr('climate.radiateur_salon', 'temperature') | float) %}
{% if court == long %}
{{ state_attr('climate.radiateur_salon', 'temperature') | int }}
{% else %}
{{ state_attr('climate.radiateur_salon', 'temperature') | float }}
{% endif %}
The main issue was the quotation marks , I never know when to use them or not in jinja2.
I still find that a bit cumbersome. If there’s an easier solution to trim trailing zeros, please let me know.