Template sensor for automation with timestamp not updating

Hi all,

I use the Google Calender Event with great success. So I loaded all the dates in seperate calendars per bin for emptying the bins because there is no integration (yet) for that company.

I found a post where someone wanted exactly the same, but I don’t understand why mine doesn’t notify me.
I want to be notified 37800 seconds (10,5h) and 30600 seconds (8,5h) before the actual event.

So I made 6 template sensors, which relates with the sensor.date_time_iso of the Time & Date integration.

Here is one of them:

platform: template
sensors:
    red_bin_offset:
      entity_id: calendar.oud_papier1
      friendly_name: 'oud papier tijd'
      value_template: '{% if as_timestamp(states.calendar.oud_papier1.attributes.start_time) - as_timestamp( strptime(states.sensor.date_time_iso.state, "%H:%M" ) ) < 37800  %}on{% else %}off{% endif %}'

And I made a template sensor for the days it takes till they are going to empty the bin:

platform: template
sensors:
    datum_papier:
      entity_id: calendar.oud_papier1
      friendly_name: 'Oud papier'
      value_template: '{{ (( as_timestamp(strptime(states.calendar.oud_papier1.attributes.start_time, "%Y-%m-%dT%H:%M:%S")) - as_timestamp( strptime(states.sensor.date_time_iso.state, "%H:%M" )))/ (3600*24)) | round(0) }}'
      unit_of_measurement: 'dagen'

The sensors show up correctly in Home Assistant:

sensor.red_bin_offset	off	       friendly_name: oud papier tijd
sensor.datum_papier	    25	       unit_of_measurement: dagen
                                   friendly_name: Oud papier
                                   icon: mdi:delete-empty

If I use the template editor in the dev section, the state returns off like it should, if I change the seconds to longer than it actually takes for the time they empty it, it returns on.

So I made an automation to notify all the devices to put the bin out. And made a input boolean to activate when the bin actually is out.

automations:
- id: Oudpapierbak
  alias: '[Garbage] Red Bin Reminder'
  trigger:
  - entity_id: sensor.red_bin_offset
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.bins_out
    state: 'off'
  action:
  - data:
      data:
        push:
          category: redbins
      message: moet bij de weg.
      title: Oudpapier container
    service: notify.home_assistant

But for some reason, it doens’t update the sensor how many days is left, and the template sensor doesn’t turn to on.

I read a lot of articles about this problem, and the answer was that there needs to be a sensor that updates from itself, so I use

sensor.date_time_iso

because it is the only sensor with a timestamp.

It has become a long story, but I rather give to much information and be sure I told everything what I think is usefull to discover my error.

I found a fix for this, it may not be the cleanist one, but it works.

Home Assistant now has the home_assistant.update_entity service.

So I made an automation which triggers every 15 minutes.

- id: '1578142215796'
  alias: '[Update] Container sensoren'
  description: ''
  trigger:
  - minutes: /15
    platform: time_pattern
  condition: []
  action:
  - entity_id: sensor.red_bin_offset, sensor.green_bin_offset, sensor.grey_bin_offset,
      sensor.red_bin_offset1, sensor.green_bin_offset1, sensor.grey_bin_offset1, sensor.datum_grijs,
      sensor.datum_bio, sensor.datum_papier
    service: homeassistant.update_entity

and that fixed it for me