Formatting large numbers in automation notifications

I’m using an automation to send myself a message containing the latest corona confirmed value. However, in NL, it’s exceeding 10.000, which makes the number large and not easy to read.

So now the number in my message is ‘10923’, and I’d like to format that into ‘10.923’. I figure it could be done with templating, but I can’t find an example to start with.

Anyone has an idea?

Can you not just divide by 1’000?

I’d rather have a ‘neat’ solution, with the added bonus of learning how to apply formatting :slight_smile:

What is not neat about dividing the number by 1000?

{% mynumber = 10923 %}

Dividing by 1000
{{ mynumber /1000 }}

Applying Python string formatting
{{ "{:,.3f}".format(mynumber / 1000) }}

Both lead to the same result:

Thanks @Burningstone, that does like nice :slight_smile: I’ll use that.

For even larger numbers (ie. millions) I guess I could use an if/else statement to check the size, split the number and then stitch it back together with multiple dots in between. Or do you have a better suggestion?

divide I’d say
it depends on your use case so you need to decide how to you’d like to see your data as it can be as flexible as your imagination :wink:

I use the following approach when displaying time in compact mode - if it’s just minutes I generate 5 min but if it’s hours it becomes 2h 5m or 3 hours (if minutes is 0) so it takes about the same space on screen :wink:

Here’s one of examples for you to get the idea

{% set etime = states.sensor.ender_3_pro_time_remaining.state | int %}
{% set seconds = etime % 60 %}
{% set minutes = ((etime % 3600) / 60) | int %}
{% set hours = ((etime % 86400) / 3600) | int %}
{% set days = (etime / 86400) | int %}
{%- if days -%}
  {{days}} day{{'s, ' if days > 1 else ', '}}
{%- endif -%}
{%- if hours -%}
  {{hours}} hour{{'s, ' if hours > 1 else ', '}}
{%- endif -%}
{{minutes}} minute{{'s' if minutes > 1 else ''}}

Thanks a lot for the writeup! Will do

1 Like