Reduce number of decimals to sensor

Hi!

Trying to reduce the number of decimals to a KNX sensor that measures voltage.
from: 230.326547326345V
to: 230.3V

entity_id: sensor.electric_voltage_l1.

Have tried various suggestions from community but noone has succeeded.

# sensor.electric_voltage_l1
   - platform: knx
     name: Electric_Voltage_L1
     address: '2/1/4'
     type: 'electric_potential'


# my last try
  - platform: template
    sensors:
      electric_voltage_l1_round:
        value_template: '{{ sensor.electric_voltage_l1.state | round (1) | float }}'
        friendly_name: 'Fas L1'
        unit_of_measurement: 'V'

grateful for help!

what does the actual value of the sensor look like, before the round and float?

Edit, here you go

- platform: template
  sensors:
    electric_voltage_l1_round:
      value_template: '{{ states.sensor.electric_voltage_l1.state.split('.')[0] + "." + states.sensor.electric_voltage_l1.state.split('.')[1][:1] }}'
      friendly_name: 'Fas L1'
      unit_of_measurement: 'V'
1 Like

Thank you, it works.

# example 230.2V
  - platform: template
    sensors:
      electric_voltage_l1_round:
        value_template: "{{ states.sensor.electric_voltage_l1.state.split('.')[0] + '.' + states.sensor.electric_voltage_l1.state.split('.')[1][:1] }}"
        friendly_name: 'Fas L1'
        unit_of_measurement: 'V'

# example 230V
  - platform: template
    sensors:
      electric_voltage_l1_int:
        value_template: "{{ states.sensor.electric_voltage_l1.state.split('.')[0] }}"
        friendly_name: 'Fas L1'
        unit_of_measurement: 'V'
2 Likes

What does the [:1] at the end do? Round to one decimal place?

it just takes the string and counts from the beginning [:x] numbers into the string and displays that.

ie “Password” is our string

[:4] would display “Pass” while on the flipside [4:] would display “word”

so for the OP’s need it takes a long number ie “240.4334324234” and splits it at the “.” which gives us “240” and “4334324234” so we take the 2nd part and just get the first number because the longer one isn’t needed. [:1]

4 Likes

Thanks :smiley:

So, you can just use a string format. Not all this fancy stuff other people are doing.

  - platform: template
    sensors:
      electric_voltage_l1_round:
        value_template: "{{ '%.2f'%(states('sensor.electric_voltage_l1') | float) }}"
        friendly_name: 'Fas L1'
        unit_of_measurement: 'V'

'%.2f' means a float is getting plopped there and it will have 2 numbers after the decimal place. You can place a number before like this '%2.2f' and it will only have 2 numbers before and 2 numbers after. You need to pass this as a float, not a string which is why the pipe float filter is there.

EDIT and lastly, if the history graph isn’t showing up properly, you may need to convert it back into a float (because format turns it into a string)

  - platform: template
    sensors:
      electric_voltage_l1_round:
        value_template: "{{ '%.2f'%(states('sensor.electric_voltage_l1') | float) | float }}"
        friendly_name: 'Fas L1'
        unit_of_measurement: 'V'
6 Likes

Hi!

Sorry, noob question.

I know I revived an old post but this is similar to my case but I don’t know where to edit this file mentioned in this thread. Is it on Configuration.yaml or somewhere else?

Thanks!

if things haven’t changed you edit the appropriate sensor.yaml file at the appropriate entry or create a new sensor file for modified entities…

That being said, I don’t know how things are done these days, or where files are located and so on.

1 Like