From cb1415e9ffeb8da36f681f13c08cb21956d63d86 Mon Sep 17 00:00:00 2001 From: akasma74 Date: Sun, 17 Nov 2019 14:25:31 +0000 Subject: [PATCH] Working without entities: conversion example added (#10848) * Working without entities: conversion example added Thought it's an interesting to mention the trick based on this discussion - https://community.home-assistant.io/t/template-platform-binary-sensor-not-updating/68059/2 * resolving comments * raw/endraw fix * :pencil2: Tweak Co-authored-by: Franck Nijhof --- source/_integrations/template.markdown | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/source/_integrations/template.markdown b/source/_integrations/template.markdown index 979c97edf0d..8918d530d75 100644 --- a/source/_integrations/template.markdown +++ b/source/_integrations/template.markdown @@ -311,7 +311,7 @@ sensor: The `template` sensors are not limited to use attributes from other entities but can also work with [Home Assistant's template extensions](/docs/configuration/templating/#home-assistant-template-extensions). -This template contains no entities that will trigger an update, so we add an `entity_id:` line with an entity that will force an update - here we're using a [date sensor](/integrations/time_date) to get a daily update: +This template contains no entities that will trigger an update (as `now()` is a function), so we add an `entity_id:` line with an entity that will force an update - here we're using a [date sensor](/integrations/time_date) to get a daily update: {% raw %} @@ -328,8 +328,25 @@ sensor: {% endraw %} -Useful entities to choose might be `sensor.date` which update once per day or `sensor.time` which updates once per minute. +In this case it is also possible to convert the entity-less template above into one that will be updated automatically: +{% raw %} + +````yaml +sensor: + - platform: template + sensors: + nonsmoker: + value_template: "{{ (( as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d')) - as_timestamp(strptime('06.07.2018', '%d.%m.%Y')) ) / 86400 ) | round(2) }}" + friendly_name: 'Not smoking' + unit_of_measurement: "Days" +```` + +{% endraw %} + +Useful entities to choose might be `sensor.date` which update once per day or `sensor.time`, which updates once per minute. +Please note that the resulting template will be evaluated by Home Assistant state engine on every state change of these sensors, which in case of `sensor.time` happens every minute and might have a negative impact on performance. + An alternative to this is to create an interval-based automation that calls the service `homeassistant.update_entity` for the entities requiring updates. This modified example updates every 5 minutes: {% raw %}