Sensor state not updating unless restart HASS

Following this guideline ( Template sensor not updating ) , I hoped this would solve my issue but I can’t seem to get it working.

I use a grabage collection sensor and use a ‘days remaining’ tile on tileboard;

days_until_recycling_plastic:
      friendly_name: 'Plastic'
    unit_of_measurement: 'days'
    entity_id: sensor.rova_plastic
    value_template: >- 
        {% set due_date = (as_timestamp(states.sensor.rova_plastic.state) | int) %}
        {% set todays_date = (now().strftime("%s") | int ) %}
        {% if todays_date > due_date %}
        {% set countdown = "?" %}
        {% else %}
        {% set countdown = ((due_date - todays_date | int) / 24 / 60 / 60) | round(0) %}
        {% endif %}
        {{countdown}}

As expected , in dev-template it works well, however, it doesn’t change sensor.days_until_recycling_plastic.

Appreciate your insights !

To understand how this works, we will need to see the configuration for sensor.rova_plastic

I use following sensor:

- platform: rova
    zip_code: 1234 AB
    house_number: 1
    house_number_suffix: b
    name: Rova
    monitored_conditions:
      - bio
      - paper
      - plastic
      - residual

This yields following states which are all correct.

sensor.rova_bio 2019-05-21T00:00:00
sensor.rova_paper 2019-05-14T00:00:00
sensor.rova_plastic 2019-05-28T00:00:00
sensor.rova_residual unknown

and
sensor.date 2019-05-13

Hope this is what you are requesting

Double-check the indenting.

    days_until_recycling_plastic:
      friendly_name: 'Plastic'
      unit_of_measurement: 'days'
      entity_id: sensor.rova_plastic
      value_template: >- 
        {% set due_date = (as_timestamp(states.sensor.rova_plastic.state) | int) %}
        {% set todays_date = (now().strftime("%s") | int ) %}
        {% if todays_date > due_date %}
        {% set countdown = "?" %}
        {% else %}
        {% set countdown = ((due_date - todays_date | int) / 24 / 60 / 60) | round(0) %}
        {% endif %}
        {{countdown}}

Try this, it’s not updating every day because there is nothing to update off of. So, you need to add the date time platform into your sensor section, then use that in the template.

This should update every night at midnight.

sensor:
  - platform: time_date
    display_options:
      - 'date'
  - platform: template
    sensors:
      days_until_recycling_plastic:
        friendly_name: 'Plastic'
        entity_id: sensor.date, sensor.rova_plastic
        unit_of_measurement: 'days'
        value_template: >- 
          {% set due_date = (as_timestamp(states.sensor.rova_plastic.state) | int) %}
          {% set todays_date = (now().strftime("%s") | int ) %}
          {% if todays_date > due_date %}
          {% set countdown = "?" %}
          {% else %}
          {% set countdown = ((due_date - todays_date | int) / 24 / 60 / 60) | round(0) %}
          {% endif %}
          {{countdown}}

Thanks @petro, i’ll give that a go and check back tomorrow
much appreciated.

@petro, seems I can’t have duplicate entity_id in the template, so I replace

entity_id: sensor.rova_plastic

by

entity_id: sensor.date

?

Ah yes, never noticed that.
I’ll update the code

perfect, thanks !

@petro, it seems it still doesn’t update over midnight, just checked again and adding sensor.date to entity_id didn’t have an affect.

sensor.date displays the correct date

Anything else I can look for , I’m out of ideas/talent :confused:

can you take a screenshot of this entity in your states page?

sensor.rova_plastic

I believe your calculation is off. If you restarted yesterday after noon, the calculation would remain the same until noon today. It’s because you are rounding. Every half day (12 hours) it rounds down or rounds up. It will change tomorrow. This calculation should count properly all the time, regardless of the time of day. It considers today as a full day.

sensor:
  - platform: time_date
    display_options:
      - 'date'
  - platform: template
    sensors:
      days_until_recycling_plastic:
        friendly_name: 'Plastic'
        entity_id: sensor.date, sensor.rova_plastic
        unit_of_measurement: 'days'
        value_template: >- 
          {% set dstring = states('sensor.rova_plastic') %}
          {% set day = now().utcnow().replace(hour=0).replace(minute=0).replace(second=0).replace(microsecond=0) %}
          {% set due = strptime(dstring, '%Y-%m-%dT%H:%M:%S') %}
          {{ (due - day).days }}

Wow, many thanks @petro, I’m still trying to get my head around this date calculation and this goes beyond my knowledge.
I’ll try and report back tomorrow.

Appreciate your support so far !

It’s hard for me to grasp what is going wrong, trying hard to learn but if logic fails to me :slight_smile:

Here is the output of the garbage collector sensors:

running the config in dev template I get the correct number of days: 12

however, the sensor still reports yesterday’s state:

Somehow date change doesn’t trigger a state update.

I have a script running now to restart HASS overnight but there must be some other way to do this.

Eager to learn and understand :wink:

Is your date set properly in your system? It’s possible that the date is changing on utc time which would make the calculation not work.

Yes it is, also date.sensor is correct in HASS

Our Cli:
$ hassio help

debug1: permanently_set_uid: 0/0
core-ssh:~# date
Thu May 16 18:54:30 CEST 2019
core-ssh:~#

What about in your configuration.yaml?

Timezone is set to Europe/Amsterdam
Home location is also defined and correct

Well I’m stumped then. I have no idea why it’s not updating. Tomorrow, don’t restart your system and check to see when the sensor last changed or last updated. I’m wondering if it’s triggering slightly before midnight.