Counting down towards a date

I have restful sensor that gets a new date (state change) every 14 days.

The restful sensor has a force_update: true and a polling interval of half a day

Then I have a second sensor that calculates the number of days from now() until the restful sensor date.

My problem is that since the restful sensor updates but does not get a state change (well, every 14 days it does) my countdown sensor never gets updated. It just shows the number of days that was calculated at the last time Hass was restarted.

Is there any ways I van get the countdown to work? It needs to count down once or twice a day

Can you share your config? As far as I know template sensors should update in line with the entity_id it’s taking the value from. Unless you use a different method>

Should I see the updates on the graphic state bar of the rest sensor, or does it need a state change to show a “break point”? I wonder if my rest sensor is not updating… Currently I have polling set to 7200 secs (2 hours)

I am not in front og my computer, will post config later. Thanks!

Here’s my restful sensor:

- platform: rest
  name: Paper recycling
  value_template: '{{ value_json[1].Tommedatoer[0] }}'
  method: GET
  scan_interval: 7200 #once every 2 hours
  resource: https://norkartrenovasjon.azurewebsites.net/proxyserver.ashx?server=https://komteksky.norkart.no/komtek.renovasjonwebapi/api/tommekalender/?REDACTED
  force_update: true
  headers:
     Host: norkartrenovasjon.azurewebsites.net
     User-Agent: Home Assistant
     Referer: https://folloren.custompublish.com/
     RenovasjonAppKey: REDACTED
     Kommunenr: 217

Here’s the template sensor updating only when hass restarts, seemingly not on updates:

days_until_recycling:
  friendly_name: Paper recycling in
  value_template: '{{ (( as_timestamp(strptime(states.sensor.paper_recycling.state, "%Y-%m-%dT%H:%M:%S")) - as_timestamp(now()) )/ (3600*24)) | round(1) }}'
  unit_of_measurement: days

Silly question : does the sensor return a value that changes? If it returns a date which doesn’t change, its state will not change and therefore the template sensor will not update.

That is the problem. The date will only change every 14 days, but I would like the countdown to update its value every day or twice a day…

Any way you could template your original sensor to have it return a number of days instead of a date?

value_template: '{{ (( as_timestamp(strptime(value_json[1].Tommedatoer[0] , "%Y-%m-%dT%H:%M:%S")) - as_timestamp(now()) )/ (3600*24)) | round(1) }}'

This would probably solve your problem?

To solve the issue you may use a timer as entity_id.
Following is working for me for updating the sensor every minute:

- platform: template
  sensors:
    countdown:
      value_template: '{{ (as_timestamp(states.calendar.dates.attributes.start_time) - as_timestamp(now())) | timestamp_custom("%d")| int }}'
      friendly_name: 'Countdown to whatever'
      unit_of_measurement: 'day(s)'
      icon_template: mdi:newspaper
      entity_id: sensor.time

- platform: time_date
  display_options:
  - 'time'

@leinich Problem with that is when it changes months, it will report an incorrect countdown if you are subtracting days (for example due date is January 1st and today is December 19) 1 - 19 = -18.

What I’d do is convert the restful sensor into a UTC datestamp (in seconds) and then subtract now (in seconds) and then convert into days ( / 24 / 60 / 60)

@Jer78
following give me:

{{ (as_timestamp('2017-12-20 00:00:00') - as_timestamp(now())) | timestamp_custom("%d")| int }}
{{ (as_timestamp('2017-12-31 00:00:00') - as_timestamp(now())) | timestamp_custom("%d")| int }}
{{ (as_timestamp('2018-01-19 00:00:00') - as_timestamp(now())) | timestamp_custom("%d")| int }}
{{ (as_timestamp('2018-01-20 00:00:00') - as_timestamp(now())) | timestamp_custom("%d")| int }}

{{ (( as_timestamp(strptime('2017-12-20 00:00:00' , "%Y-%m-%dT%H:%M:%S")) - as_timestamp(now()) )/ (3600*24)) | round(1) }}
{{ (( as_timestamp(strptime('2017-12-31 00:00:00' , "%Y-%m-%dT%H:%M:%S")) - as_timestamp(now()) )/ (3600*24)) | round(1) }}
{{ (( as_timestamp(strptime('2018-01-19 00:00:00' , "%Y-%m-%dT%H:%M:%S")) - as_timestamp(now()) )/ (3600*24)) | round(1) }}
{{ (( as_timestamp(strptime('2018-01-20 00:00:00' , "%Y-%m-%dT%H:%M:%S")) - as_timestamp(now()) )/ (3600*24)) | round(1) }}

1
12
31
1

0.0
11.0
30.0
31.0

For my usecase it is one day off. But you are right my template will fail for more than 31 days in the future.

Yes, this will probably work. Thanks, I Will give it a try tomorrow

Yes, this will work for me since the timedelta in my case allways will be less than 31 days. Thanks!

@Marius @leinich

Try this:

  days_until_recycling:
    friendly_name: 'Paper recycling in'
    unit_of_measurement: 'days'
    entity_id: sensor.paper_recycling
    value_template: >-
      {% set due_date = (as_timestamp(states.sensor.paper_recycling.state) | int) %}
      {% set todays_date = (now().strftime("%s") | int ) %}
      {% if todays_date > due_date %}
        {% set countdown = "Past Due" %}
      {% else %}
        {% set countdown = ((due_date - todays_date | int) / 24 / 60 / 60) | round(0) %}
      {% endif %}
      {{countdown}}

Calculating and showing number of days directly in the rest sensor iself solved this. I then get a new countdown value for each rest sensor poll interval

How did you get the API key btw? Im trying to snoop the app but nothing shows.

I used developer tools in Firefox to find the inner workings of this web site: https://www.folloren.no

Ah…thanks. Will try my local county site ,)

Does that still work? seems they changed how they do it…

Yes, I actually got the alert yesterday night that I should place the paper bin outside, so I guess it works. I will check logs when I get home.

I think that the appkey is municipality wide, or even maybe region wide (Follo), so redacting it from this post was not really that necessary.

I can Pm the actual yaml if the log is fine. Might not work for your address if you are outside my region, tho…

Tip: I think the referer header field has to be right for this to work, sorry, don’t remember the details…