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

* ✏️ Tweak


Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
akasma74 2019-11-17 14:25:31 +00:00 committed by Franck Nijhof
parent 7eb4b6dad0
commit cb1415e9ff

View File

@ -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). 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 %} {% raw %}
@ -328,7 +328,24 @@ sensor:
{% endraw %} {% 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: 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: