Hi,
here is my locale settings:
here i a template that do not honor decimal and thousand separator:
what I expect is 2.603,0 Wh
Thanks
Hi,
here is my locale settings:
here i a template that do not honor decimal and thousand separator:
what I expect is 2.603,0 Wh
Thanks
No, Jinja does not use the Python locale functions so it will apparently not generate anything except 1,234,567.89 and only if you format it.
If you want to change your result then you have to do this yourself. There are fancy ways of pulling in Python code, however here is a ‘fix’ to swap the ‘,’ and ‘.’ over. It is not pretty, but it does appear to work.
{%- macro fnum(val, post) %}
{{- "{:,.2f}".format(val) |replace(".","&") |replace(",",".") |replace("&",",")}} {{post}}
{%- endmacro %}
{{ fnum(state_attr('sensor.consumo_caldaia', 'last_period') | float, 'Wh') }}
I have written this up using a macro, so you can call it from wherever you need as
fnum( value, units)
The .format function string at the front says use commas and ‘.’ 2 dp. Then use replace in sequence to swap out the ‘.’ to ‘&’ (anything but a number or comma), then the ‘,’ to ‘.’, then finally the ‘&’ back to ‘.’
It will not win any awards for being neat but it does seem to work.
I could be wrong - but probably this is not only about a jinja - this is also about a way how a numerical value is stored in objects.
For example, states are strings. So probably “states(‘sensor.pressure’)” is stored as “1234.56” - not “1 234.56” or “1234,56” or “1,234.56”.
And (just a guess) a “states()” function returns an exact value from objects - w/o any “regional conversions”.
As for attributes.
They could have a type (i.e. not always a string).
Theoretically numerical values (numbers) could be “regionally converted” to “1 234.56” or “1234,56” or “1,234.56” in “state_attr()”.
But imagine 2 users with diff. “regional settings” - one with “1234.56”, another with “1,234.56” - using same HA server. And whose regional settings should be followed in template sensors, automations etc???
So - templating should be processed independently on “regional settings”.
This is what we have now (I hope).