Template sensor not updating until hass is restarted

value_template: ‘{{ (as_timestamp(states.calendar.hass_calendar_fgo.attributes.start_time) / 86400 - as_timestamp(as_timestamp(now()) | timestamp_custom("%Y-%m-%d 00:00:00",true)) / 86400) | int(0) }}’

I have the following template sensor, but it doesn’t seem to update unless home assistant is restarted. I’ve tested this in the developer/templates area and it updates fine. But not the sensor.

Can anyone help? Thanks.

1 Like

A template sensor will only update when one of the referenced entities changes (which would be calendar.hass_calendar_fgo in your case.) It does not update simply because time has gone by. If you want it to update when time changes, then you need to use an entity that changes with time in your template, like the Time & Date sensor.

2 Likes

You sound like a broken record, lol. HA really needs to come up with a solution that does this natively.

1 Like

Thanks @pnbruckner. I added time and date and did this which has resolved my issue.

From:

value_template: >-
  {{ (as_timestamp(states.calendar.hass_calendar_fgo.attributes.start_time) / 86400 - as_timestamp(as_timestamp(now()) | timestamp_custom("%Y-%m-%d 00:00:00",true)) / 86400) | int(0) }}

To:

value_template: >-
  {{ (as_timestamp(states.calendar.hass_calendar_fgo.attributes.start_time) / 86400 - as_timestamp(as_timestamp(states.sensor.date.state+' '+"00:00:00") | timestamp_custom("%Y-%m-%d 00:00:00",true)) / 86400) | int(0) }}
1 Like

I wouldn’t call that a solution… you have invalid characters like and . People coming to copy and paste that code will be met with errors.

I’ve revised it now.

1 Like

You can probably simplify that a bit:

{{ ((as_timestamp(state_attr('calendar.hass_calendar_fgo','start_time')) - strptime(states('sensor.date'),'%Y-%m-%d').timestamp()) / (60*60*24))|int(0) }}
1 Like

Awesome! Thanks for that.