I have a template sensor which calculates the difference between a timestamp read from a MQTT sensor and current time.
My problem: the template sensor obviously only updates, when sensor.landroid_last_update_timestamp changes, resulting in the template sensor showing a difference of ‘0 min’ most of the times, except from when I restart HA.
There is a warning in the Templating doc regarding templates with time:
Rendering templates with time is dangerous as updates only trigger templates in sensors based on entity state changes.
I think, this is related to my problem, but while I think my understanding of english language is quite good, I have to admit, I don’t understand the meaning of this warning…
Could someone please explain this warning to me in other/simple words or with a short example? How I’d have to change my config to circumvent this behaviour?
Template sensors only update based on actual sensors in your template equation/code. now() is not considered a sensor. So your template will only update when sensor.landroid_last_update_timestamp updates.
To have this update live, every minute, you need to use a home assistant entity that will update every minute. You can use the date time component:
it would look like this, mind you, you have to combine this with your current template sensors, don’t copy the top most level yaml sensor, platform: template, and sensors into your config.
If somebody else wants to use this, below is my final configuration. I shortened it to a one-liner, ommiting the usage of the temporary variable current_time.
Please note, that the time_date sensor’s internal name will be sensor.date__time (yes, with two underscores), as it is derived from the friendly name “Date & Time”, which is hard coded in time_date.py and can’t be overridden by an own friendly_name or internal_id. Don’t know why, but anyways.
Just for the record: I also excluded both timestamp sensors (sensor.date__time and sensor.landroid_last_update_timestamp) from being recorded in the history database. Recording timestamps of the current time represented as strings isn’t very useful in most cases. U don’t get readable history graphs from them, just a lot of useless data filling up the database. If you want to configure something similar, you may also consider excluding your timestamp sensors: