thomasloven / lovelace-template-entity-row

🔹 Display whatever you want in an entities card row.
MIT License
210 stars 16 forks source link

Float output in Card differs from Dev Tools -> Template Editor #68

Closed GrizzlyAK closed 2 years ago

GrizzlyAK commented 2 years ago

I have the following in a Card. The value for the entity is 4.

- type: custom:template-entity-row
  entity: input_number.stale_zone_override_time_in
  name: SZO Timeout (hrs)
  state: >-
    {{ "%.1f" | format( states('input_number.stale_zone_override_time_in') | float ) }}

Which yields:

image

But in the HA Dev Tools -> Template Editor, I enter and get the following (which is what I would expect):

{{ "%.1f" | format( states('input_number.stale_zone_override_time_in') | float ) }}

4.0

Note that if I change the value of teh entity to 4.5, then the Card shows 4.5.

Is this a bug?

GrizzlyAK commented 2 years ago

Similarly, if I change the format to be:

"Temp                 %.1f"

with a bunch of spaces, in the Template Editor it renders as

Temp                 30.0

but in the Card it renders as

Temp 30.0

with only a single space. I would expect the former? Also, using something like %9.1f doesn't even add leading spaces as expected.

thomasloven commented 2 years ago

This is a combination of effects.

Templates nowadays evaluate to native types, so {{ "%.1f" | format( states('input_number.stale_zone_override_time_in') | float ) }} will not be "4.0" as a string of characters like before, but instead 4.0 as a number, which the frontend then interprets as it wants.

Same thing with all the spaces. Your browser will remove them as a feature.

The workaround in the first case is to force the output to be a string. This can be done by adding a null-byte character somewhere {{ "%.1f\x00" | format( states('input_number.stale_zone_override_time_in') | float ) }}.

In the second case, you'll have to explicitly add space characters to the string: "Temp\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0%.1f"

GrizzlyAK commented 2 years ago

which the frontend then interprets as it wants. Hmm... I thought the whole point of format() in templates was to actually format the result. I did know that templates were changed about 0.119 or so to evaluate to native types, which broke some things, and don't yet understand the full ramifications of the change.

Thanks for the workaround and for illustrating how to use hex characters in templates. Where is it defined that one is to use \x00 vs \0x00?

thomasloven commented 2 years ago

Yes, it is a bit of a special case you get here.

"%.1f" | format(4.01) becomes the string "4.0", but the template processor with the native typing interprets that as the number 4.0, and javascript likes to display that number as the string 4.

By adding the NULL byte, you get a result that the template processor cannot interpret as a number, and therefore tells the frontend is the string "4.0\0x00". \0x00 (or \x00 which is the same thing) is not printed, but still counts as a string character.

GrizzlyAK commented 2 years ago

If the reason the former doesn't work here is because of Javascript, dies that mean the reason it DOES work in the HA Dev Tools Template Editor is because of the way Python interprets it there? If so, that is something to consider when using the template editor to test templates for use in JS extensions.