From a45785cd88c148e01f48423242b82ad1c5f051cf Mon Sep 17 00:00:00 2001 From: Philip Abbey Date: Sun, 28 Jan 2024 13:16:57 +0000 Subject: [PATCH] Update Templates.md Added template example with float number formatting. --- examples/Templates.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/Templates.md b/examples/Templates.md index 8c78fe2..da09712 100644 --- a/examples/Templates.md +++ b/examples/Templates.md @@ -45,6 +45,16 @@ The first two keep to the simple proposal above. The last combines them into a s } ``` +In order to keep the formatting of floating point numbers under control, you might also like to include a format string as follows. `states()` seems to return a `string` that needs converting to a `float` before the `format()` call can manage the conversion to the required number fo decimal places. + +```json +{ + "name": "Hallway", + "type": "template", + "content": "T:{{ '%.1f'|format(states('sensor.hallway_temperature')|float) }}°C, H:{{ '%.1f'|format(states('sensor.hallway_humidity')|float) }}%" +}, +``` + ## Conditionals Anything between `{%` and `%}` is a directive (`if`, `else`, `elif`, `endif`, etc.). Conditionals are used to dynamically change the content based on the state of the entity.