jono
(Jono)
July 18, 2017, 9:06pm
1
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
roflmao
(Rofl)
July 19, 2017, 8:35am
2
something in regatrds of {{ value_json.state.lightlevel | int//1000 }}
?
jono
(Jono)
July 19, 2017, 9:13am
3
Thanks but that doesn’t work. It just shows the thousands numbers (first 2 numbers only)
roflmao
(Rofl)
July 19, 2017, 9:28am
4
I guess i misunderstood. Do you just want to show 22806.0 ?
jono
(Jono)
July 19, 2017, 9:47am
5
roflmao:
22806
No, I’m wanting to show 22,806 instead of 22806 (add the comma in there)
ih8gates
(Scott Reston)
July 19, 2017, 1:01pm
6
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.
jono
(Jono)
July 19, 2017, 4:22pm
7
ih8gates:
%.1f
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
{{ '{0:,.0f}'.format(123456789) }}
in the templates dev tool returns:
123,456,789
Found here .
6 Likes
jono
(Jono)
July 19, 2017, 5:52pm
9
Haha, I did quite a bit of Googling. I guess I’m not very good at it
(Or not ‘Googling in the right places’)
This works great, thanks a lot!
Vasco
March 9, 2020, 12:09pm
10
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
osmoen
March 10, 2020, 8:24pm
11
What about:
{{ '{0:,.0f}'.format(states('sensor.xyz') | int) }}
celair
(Flo)
April 27, 2020, 3:35pm
12
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 }}"
osmoen
April 27, 2020, 6:42pm
13
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?
celair
(Flo)
April 29, 2020, 9:12am
14
Yeah, absolutely right. That was one of my mistakes.
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
mibane
(Mike)
January 10, 2024, 10:19pm
17
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.