Add a separator (comma) to thousands?

I have a sensor that shows the Lux (amongst other things), but I’d like to add a thousands separator (comma) in there

Here’s the sensor code

- platform: rest
  name: 'Kitchen Lux'
  resource: http://192.168.0.18/api/[API KEY]/sensors/11
  value_template: '{{ value_json.state.lightlevel }}'
  unit_of_measurement: Lux

Is there a way of adding the comma in there?
1 Like

something in regatrds of {{ value_json.state.lightlevel | int//1000 }} ?

Thanks but that doesn’t work. It just shows the thousands numbers (first 2 numbers only)

I guess i misunderstood. Do you just want to show 22806.0 ?

No, I’m wanting to show 22,806 instead of 22806 (add the comma in there)

Not a full answer, but hopefully a push in the right direction: I format my sun angle with the following:

{{ "%.1f"|format(states.sun.sun.attributes.elevation) }}

%.1f is the format (a plus sign and one digit after the . )

I can’t seem to work out the formatting for including commas, though. But you can experiment with the templates dev tool.

Thanks it looks along the right lines, but I can’t find the correct code to add at the beginning.

Let me google it for you :slight_smile:
{{ '{0:,.0f}'.format(123456789) }} in the templates dev tool returns:
123,456,789
Found here.

6 Likes

Haha, I did quite a bit of Googling. I guess I’m not very good at it :sweat_smile:
(Or not ‘Googling in the right places’)

This works great, thanks a lot!

How would i put a sensor info in the place where you have 123456789

I tried:


{{ ‘{0:,.0f}’.format(’states.sensor.xyz.attributes.state’) }}

{{ ‘{0:,.0f}’.format(states(’sensor.xyz’)) }}

Sorry for the noob question @jono And @VDRainer

What about:

{{ '{0:,.0f}'.format(states('sensor.xyz') | int) }}

I tried your suggestion in my sensors.yaml. Unfortunately I always get the state “unknown”. Is it maybe the wrong yaml or is the code wrong?

- platform: template
  sensors:
    sensorname_new:
      value_template: "{{ '{0:,.0f}'.format(states('sensor.sensorname')) | int }}"

Your code is slightly off. Your closing parenthesis should be after int instead of a double-closed after sensor name. Does that fix the issue?

Yeah, absolutely right. That was one of my mistakes. :+1:

My code now looks like this (i’ve also added dots as separators):

value_template: "{{ '{0:_.0f}'.format(states.sensorname.state | int).replace('_', '.')}}"

1 Like

I’d like to pick up on this.
I actually copied your value template code and made it work for my case.
(showing current covid19 numbers)
My question is, if my approach using it with templates does make sense to you or not.
(it basically is a noob question)

I’m actually wondering if I have to do it that way for every single sensor, or if there might be a way to define the value_template once and apply it to multiple sensors at the same time.

- platform: template
  sensors:
    germany_confirmed:
      entity_id: sensor.germany_coronavirus_confirmed
      value_template: "{{ '{0:_.0f}'.format(states.sensor.germany_coronavirus_confirmed.state | int).replace('_', '.')}}"

    germany_deaths:
      entity_id: sensor.germany_coronavirus_deaths
      value_template: "{{ '{0:_.0f}'.format(states.sensor.germany_coronavirus_deaths.state | int).replace('_', '.')}}"

    germany_current:
      entity_id: sensor.germany_coronavirus_current
      value_template: "{{ '{0:_.0f}'.format(states.sensor.germany_coronavirus_current.state | int).replace('_', '.')}}"

cheers

2 Likes

question. How do i get only the numbers after the komma? Like the picture below i only want the 01

afbeelding

Sorry, I know it’s somewhat OT as this topic has been already solved but you can achieve it this way for example:

{% set stroomkosten = 1.01 %}
{% set decimal_part = (stroomkosten - (stroomkosten | round(0, floor))) | round(2) %}
{{ decimal_part }}

It results in a float with the value 0.01. The | round(2) part is required due to issues with the internal floating point representation.

But in case you need it as a string (e.g. ‘01’) just use

{% set decimal_part_string = (stroomkosten | string).split('.')[1] %}

I hope this helps.