Template Editor Puzzle

If I paste this line into the template editor in Developer tools, the result is a number , 0.069 which is what I expected.

{{states(‘input_number.charger_current_mean’) |float * 0.9 | round}}

If I then paste a second line into the editor even if it is the same expression so:

{{states(‘input_number.charger_current_mean’) |float * 0.9 | round}}
{{states(‘input_number.charger_current_mean’) |float * 0.9 | round}}

The digits are the same but the output is declared as a string. Why?

Because it’s 2 numbers which, when combined, can’t be a number anymore so it has to be a string,

The Result type should always be considered a “best guess”… “string” is basically the default, but it can give strange values:

{{ '2' }}   => number
{{ 2 }}{{ '2' }}   => number
{{ 2 }} {{ '2' }}   => string

Sorry but i’m not understanding this.
I’m putting two statements on different lines in the template editor. I’m not combing them in any way (knowingly). I expected them to be quite independent.

Or

Have I completely misunderstood how the template editor works which is always possible. Are separate lines interpreted together in some way even if they are not logically connected ?

Because the two templates produce values that are separated by a newline.

Put both on the same line, with no whitespace between them, and the resulting value will be a number.

If you put them on the same line but separate them with whitespace, the resulting value will be a string.

1 Like

Ah. Thank you.
Can’t say I fully understand the logic here but I wont get caught out again.

  • Your first template produces a number.

  • Your second template produces a number.

  • The two templates are on separate lines. That means there’s a (non-visible) newline character at the end of the first template.

The Template Editor looks at the complete result of both templates and sees two numbers separated by a newline. A pure number doesn’t contain newlines so it calls the final result a string.

So if your first template produces 5 and the second one produces 10 it looks like this in the Template Editor:

5
10

The 5 and 10 are separated by a newline character. There’s no integer value that looks like “5 on top of 10” so it reports the result is a string value, not a number.

1 Like

Thank you for your patient explanation. I really had misunderstood how the template editor and works and I am clear now as to what’s going on.

1 Like