Formatting template sensor works in development tool but not in value_template

I try to format a template sensor to show only 2 digit after the decimal point. With “{0:,.2f}”.format" in the development tool, all works fine. However, when I use the same in the configuration file, I get errors.

This works fine in the Developers tool:

{{ "{0:,.2f}".format((((states('sensor.verwachte_temp')|float + 30) ** 4)/75000) - (states('sensor.regen_verwacht')|float*16))}}

Here the screenshot of the developers tool:

In the config file, the platform sensor gives a configuration error.

  - platform: template
    sensors:
      irrigation_duration_for_1m2_all_day:
        friendly_name: "Sec/m² voor dag"
        unit_of_measurement: 's'
        value_template: "{{ "{0:,.2f}".format((((states('sensor.verwachte_temp')|float + 30) ** 4)/75000) - (states('sensor.regen_verwacht')|float*16))}}"

Any idea what is wrong?

  - platform: template
    sensors:
      irrigation_duration_for_1m2_all_day:
        friendly_name: "Sec/m² voor dag"
        unit_of_measurement: 's'
        value_template: "{{ '{0:,.2f}'.format((((states('sensor.verwachte_temp')|float + 30) ** 4)/75000) - (states('sensor.regen_verwacht')|float*16))}}"

Outer quotes should be different from inner quotes when the template is defined on the same line as the value_template option.

The alternative is a multiline format like this which doesn’t require outer quotes.

  - platform: template
    sensors:
      irrigation_duration_for_1m2_all_day:
        friendly_name: "Sec/m² voor dag"
        unit_of_measurement: 's'
        value_template: |
          {{ '{0:,.2f}'.format((((states('sensor.verwachte_temp')|float + 30) ** 4)/75000) - (states('sensor.regen_verwacht')|float*16)) }}

You should also supply float with a default value. For example, float(0) means it will use 0 if the entity’s value can’t be converted to a number.

Too many double quotes.

value_template: "{{ '{0:,.2f}'.format((((states('sensor.verwachte_temp')|float + 30) ** 4)/75000) - (states('sensor.regen_verwacht')|float*16))}}"

How dare you respond with the exact same response at the exact same time as me! For shame @123, for shame! :joy:

Blame it on the forum’s software. It sorts posts chronologically …

/s

2 Likes

Hi guys, many thanks. The trick with the multiline worked!!
Is there somewhere a comprehensive overview of the rules for single quotes, dubble quotes, brackets, intendation etc. This language is driving me crazy.

I suggest starting here:

Important Template Rules

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Note that the formatting won’t work. The result of a template is always a string. The template parser will try to parse it to a native Python type and then apply that type.

So if the result is something like 12.30 it will be parsed to a floating point number and the state of your sensor will be 12.3
So the result will be the same as rounding to 2 digits

You should also consider adding default values for the float filter or adding an availability template