Why does my template sensor doesn't work?

I made a template sensor, but I don’t understand why there is always an error when reloading…

sensor.yaml:

# Berechne den verbleibenden NAS-Speicher
- sensor:
  - name: freier NAS-Speicher
    unique_id: freier_NAS-Speicher
    state: {{ '%0.2f' | format((states('sensor.diskstation_volume_1_total_size') | float) - (states('sensor.diskstation_volume_1_used_space') | float)) }}

The error:
Error loading /config/configuration.yaml: while scanning for the next token
found character ‘|’ that cannot start any token
in “/config/template.yaml”, line 5, column 23

You’re trying to pipe ‘%0.2f’ into a sensor value (invalid action) a pipe is used to push a value into a function and you are instead directing it to a sensor output…

So what are you actually trying to do?

I would like to subtract the values ​​of the two sensors from each other and output them in a new sensor.

‘%0.2f’ just for format

In the development tools, the template between the curly brackets gives the result that the new sensor is looking for!

Is that example in sensor.yaml or template.yaml?

I will assume it’s in template.yaml.

You can use the round filter like this:

# Berechne den verbleibenden NAS-Speicher
- sensor:
  - name: freier NAS-Speicher
    unique_id: freier_NAS-Speicher
    state: >
      {{ (states('sensor.diskstation_volume_1_total_size') | float(0) - 
          states('sensor.diskstation_volume_1_used_space') | float(0)) | round(2) }}
1 Like

You are right! It‘s in template.yaml…
Your solution work fine! Thanks

1 Like

I’ve tried a little and the following works as well:

# Berechne den verbleibenden NAS-Speicher
- sensor:
  - name: freier NAS-Speicher1
    unique_id: freier_NAS-Speicher1
    state: >
      {{ '%0.2f' | format(states('sensor.diskstation_volume_1_total_size') | float(0) -
                          states('sensor.diskstation_volume_1_used_space') | float(0) }}