How to refresh a calculated template value?

Hello,
I created a template with a countdown to show me the number of days till the event will happen.
Therefor I have a Calendar Entity connected via a google calendar:

calendar.grune_tonne	on	message: Bio in xxx
all_day: true
start_time: 2020-02-20 00:00:00
end_time: 2020-02-21 00:00:00
location: xxx; yyy
description: 
offset_reached: false
friendly_name: Grüne Tonne

And here is my template definition in configuration.yaml

next_grunetonne:
  friendly_name: Termin Grüne Tonne
  value_template: "{{ (( as_timestamp(states.calendar.grune_tonne.attributes.start_time) - as_timestamp(now()) ) / 86400 + 0.5 ) | round(0) }}"

The calculation works perfectly. But only if I restart the server. If I wait one day the calculated day stays on the day before.
How can I refresh the calculation automatically?

I saw already the hint to use homeassistant.update_entity, but I can’t see how to integrate this if I want to update the value just once a day.

thanks
Joerg

I’d create a time sensor if you did not already do this and then use it in your template like this:

next_grunetonne:
  friendly_name: Termin Grüne Tonne
  entity_id: sensor.time
  value_template: "{{ (( as_timestamp(states.calendar.grune_tonne.attributes.start_time) - as_timestamp(now()) ) / 86400 + 0.5 ) | round(0) }}"

This way, the template will be evaluated every minute.
You could also write an automation that updates the entity every night, up to you.

1 Like

Ah, thanks.
I see how it should work. I will implement it and reply later how it works.
Joerg

Hi Buningstone,
I think it is very close. Last night I tried to do this, just with sensor.date. I thought it might be better to save performance of the server.
The calculation was refreshed as expected, but the date (states.calendar.grune_tonne.attributes.start_time) should have been refreshed as well, but stayed at the date before.
I will switch to sensor.time and wait for the next week, when the calendar event date will change again.
But thanks so far.
Joerg

Why should the start time of the calendar.grune_tonne be changed as well? The calendar entry doesn’t change when a new day starts.

The calendar entry alway point the next event. Yesterday it pointed to yesterday. The next event is at March 3rd.
So at night start_time switched from
2020-02-18 00:00:00
to
2020-03-03 00:00:00
It should have calculated with March, but the result was “-1”, which indicates it calculated with the day before.

Something like this:

      bom_forecast_2:
        entity_id:
          - sensor.bom_gosford_max_temp_c_2
        friendly_name_template: >

With your template. This will force the template to be re-evaluated every time the sensor.bom_gosford_max_temp_c_2 updates which is pretty much what you want (using your sensor of course)

I could imagine, that the sensor.date changed before the calendar updated the start time and therefore the template still used the old value.

I did a test this morning with a calendar event just for test purposes. (Don’t know why I have waited a night - stupid).
Everything was setup with sensor.time
Right after one or two minutes all template values were refreshed as expected.

But I figured out one trap: After deleting the test events in my google-calendar, all values stayed at the test event.
Reason: the google calendar connector seems to look at the events in the paper bin as well. After cleaning the paper bin, the events were back to normal.

@Davidfw1960: Interesting approach. I will give it a try.
At the end now I understand this chapter much better. Maybe an example here would have helped.

thanks at all, I’m happy now.

Thx, this helped me too!