MQTT Sensor, add thousand separator to value

Hi there,

I’m getting SQL data with Node-Red and published to HA via MQTT.

  - name: "Test Fall23"
    state_topic: "homeassistant/uretim/toplam/siparis-fall23"

This gave me this type of data:

181345

But I want to convert as

181.345

How can I add value_template for adding thousand seperator?

thanks in advance

Convert it to a number and divide by 1000:

value_template: {{ (value_json | float(0)) / 1000 }}}

That’s a botch, using the fact that the “standard” decimal point looks like the EU thousands separator, and doesn’t work over six digits.

Why do you want to do that? The standard dashboard will already render numbers like that — the state of the entity shown below is 296281, and I’m in the UK with comma separators:

image

If you force the separators into the state, HA will stop recognising it as a number via filters like float and int — you’ll lose the ability to see it in graphs, use it as a trigger etc.

If you really must do this, I’d recommend using a template sensor for when you need the separators, like this:

template:
  - sensor:
      - name: Test Fall23 Sep
        state: "{{ '{:,}'.format(states('sensor.test_fall23')|int(0)).replace(',','.') }}"

…which adds comma separators, which we then replace with dots. If your input isn’t an integer, it’ll need a bit more work.

Yes, dividing is not a solution, doesnt work with 6 digits.

I’m not using standard dashboard elements. there are a few custom elements like charts, bars etc. I think the problem is custom elements. they are not render correctly.

let me check another option to get SQL data.

Thank you again.

Oops yes, I misread the OP’s question and assumed it needed to be divided once I saw the “.” and hadn’t noted he asked to update the thousands separator (as where I live that would be “,”).

I agree that you wouldn’t want to turn it into a string by putting a separator in there.

Hi!
this sensor attribute:

{{ state_attr("sensor.speedtest_download", "bytes_received") }}

has value:

162413184

How can I display it as 162.413.184 (with dots separating every three zeros) and without having an error if the attribute value is not available?

Thanks

Look at your user profile (bottom right). Can you see this setting?

For me, that makes large numbers comma-separated:

image

…even though the entities aren’t:

If you desperately need to format the number like that, then:

{{ '{:,}'.format(state_attr('sensor.speedtest_download', 'bytes_received'))|replace(',','.') }}

but that’s then a string and cannot have a unit of measurement attached to it.

Yes, it is set up correctly, in fact from lovelace I see:

But if I try to directly view the attribute on a card (or in the template editor) the value is not formatted

The value will be formatted when you look at it using any standard HA card. The value will not contain formatting within a template. You do not want it containing a format within a template so that you can use it to perform math. xxx.xxx.xxx is not a valid number in Jinja but xxxxxxxxx is. If you want to format it, you have to use jinja formatting when outputting it from a template.

However, I would like to display the value in a custom:config-template-card.
For this reason I was asking for help with jinja formatting.
With particular attention to the fact that, if the attribute does not have a value or not is available, do not generate an error

Custom config-template-card uses JS not Jinja. Your best resource for that would be w3schools.

could be as simple as return Number(states['...']).toLocaleString(); with … replaced with your entity_id