Counting down towards a date

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…

No worry. I found the right info after some digging, and it works fine :smiley: thanks

This actually doesn’t work right, as the json order keeps changing. The type of waste being picked up has an identifier which you have to check to get the right dates.

All garbage and recycling happens the same day where I live, maybe that is why I haven’t noticed.

Did you solve it?

Yeah, then it doesn’t matter, as you get notified.

Seems hass has problems the way the json structure is, so have to patch the code to fix it (or alter the json and feed it back). Too much work, so no…i just keep track in my head which type was last week. :wink: