Home Assistant Community

Counting down towards a date

#5

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.

0 Likes

#6

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…

0 Likes

#7

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?

0 Likes

#8

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'
0 Likes

#9

@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)

0 Likes

#10

@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.

0 Likes

#11

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

0 Likes

#12

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

0 Likes

#13

@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}}
0 Likes

Bin / Waste Collection
#14

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

0 Likes

#15

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

0 Likes

#16

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

0 Likes

#17

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

0 Likes

#18

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

0 Likes

#19

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…

0 Likes

#20

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

0 Likes

#21

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

0 Likes

#23

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.

0 Likes

#24

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

Did you solve it?

0 Likes

#25

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:

0 Likes