Cut off decimals in a sensor value

Hiya!
I’m trying to cut off the decimal values from a sensor value in the dashboard.

I tried putting this code snippet in my configuration.yaml:

template:
  - sensor 
      sensor.hobbyzimmer_temperature_int
        value_template: "{{ states.sensor.hobbyzimmer_temperature.state.split('.')[0] }}"
        friendly_name: "Hobbyzimmer Temperatur int"
        unit_of_measurement: "°C"

However, when trying to save, HA gives me the following error message:

Unable to parse YAML: YAMLException: bad indentation of a sequence entry (152:23) 149 | template: 150 | - sensor 151 | sensor.hobbyzimmer_temperature_int 152 | value_template: "{{ states.sensor.hobbyzimmer ... -----------------------------^ 153 | friendly_name: "Hobbyzimmer Temperatur int" 154 | unit_of_measurement: "°C"

Upon checking with a yaml validator, I get the following:

mapping values are not allowed in this context at line 4 column 23

In general, is the configuration.yaml the right place to do this?
If yes, could anyone point me in the right direction?
Thanks!

That ‘code snippet’ is invalid.

Here’s the corrected version:

template:
  - sensor:
      - name: Hobbyzimmer Temperatur int
        state: "{{ states('sensor.hobbyzimmer_temperature') | int(0) }}"
        unit_of_measurement: "°C"

Reference: State-based Template Sensors

To “cut off” the decimals you can use the int filter. Alternately, if you want to round the value, use the round filter.

Reference: Templating - Numeric functions and filters


EDIT

Correction. Use state not value_template for Template Sensors in modern format.

1 Like

I think I’ve tried something similar. I’ve got a watermeter that is counting liters and I wanted HA to display it like on the watermeter itself; cubic meters in black the rest in red.

De watermeterstand is op  dit moment

<h1><font color=#000000>{{ states('sensor.watermeterstand') | round(0, 'floor') }}</font><font color=#fc9892>,{{ (( ((states('sensor.watermeterstand') | round(3) - states('sensor.watermeterstand') | round(0, 'floor')) | round (3)) * 1000 )) | round(0)}} </font></h1>

I think you need to look at Templating - Home Assistant and in particular:

* `round(precision, "floor", default)` will always round down to `precision` decimals
* `round(precision, "ceil", default)` will always round up to `precision` decimals
* `round(1, "half", default)` will always round to the nearest .5 value. `precision` should be 1 for this mode

Thanks for the swift reply, @123 !
Unfortunately, my configuration.yaml doesnt accept this snippet:


Is this the right place to put it or does it go somewhere else?

Thanks!

/edit:
I put it under another, already existing, “- platform: template” section and it works as desired now!

That’s due to a mistake I made. Template Sensors in modern format use state not value_template as was used in legacy format. I have corrected the example I posted above.

If you are creating new Template Sensors then I recommend you define them using modern format as opposed to legacy format. Legacy format wasn’t deprecated but it doesn’t have the additional features supported by modern format and won’t receive enhancements.