Jinja2 waste collection help

Hello! I’m trying to use a Jinja2 template to correctly show me the dates for my waste collection. This is the JSON I’m using to inform two rest sensors. One for the recycling and one for the waste. As far as I’m aware, “grey bins” (landfill) is always position 0 and recycling is always position 1. What I’m finding is, I am needing to manually swap the < for an > every week so far to 'correct the template and get it to show the correct collection! If I don’t do this, it always skips a week. i.e. this morning it was showing me the next collection being on the 4th of March for grey bins, and as soon as I swapped the < for >, I now get recycling happening on 25/2 !

Any ideas?! Thank you!!!

{% set binbags = "states.sensor.testbin_d.state" %}{% set recycle = "states.sensor.testbin_e.state" %}{% if strptime(binbags, "%d/%m/%y") < strptime(recycle, "%d/%m/%y") %}Bin bags on {{ states.sensor.testbin_d.state }}{% else %}Recycling on {{ states.sensor.testbin_e.state }}{% endif %}

My

this should work for you.

{% set binbags = states("sensor.testbin_d") %}
{% set recycle = states("sensor.testbin_e") %}
{% if strptime(binbags, "%d/%m/%y") < strptime(recycle, "%d/%m/%y") %}
  Bin bags on {{ binbags }}
{% else %}
  Recycling on {{ recycle }}
{% endif %}

You only were doing 1 thing wrong. You were putting your state object a string.

{% set binbags = "states.sensor.testbin_d.state" %}
{% set recycle = "states.sensor.testbin_e.state" %}

If you simply change to this, it will work

{% set binbags = states.sensor.testbin_d.state %}
{% set recycle = states.sensor.testbin_e.state %}

But, that doesn’t add safety as the object on startup. So use the states() method instead, which is what I posted in the first code section.

1 Like

Thanks Petro!

@Petro, curious thing if you have a moment? Normally my “put the bloody bins out!!!” trigger triggers on Sunday evening. Which is correct. Bins get collected Monday morning.
Today, out of the blue, it triggers today (a saturday!)
Any ideas why?!

{% set d = (strptime(states('sensor.testbin_d'), '%d/%m/%y')
            .timestamp() - 24*60*60)|timestamp_custom('%Y-%m-%d') %}
{{ states('sensor.date_time') == d ~ ', 21:00' }}

Sensor.testbin_d = 01/04/19

52

Thanks for any assistance. Just a bit confusing!!

I don’t see anything that would cause that to be honest. Does your system have the correct time?

Yes, it does.

For interest, I did NOT get a notification on Sunday evening. The system clearly thought I’d already had it on Saturday…
WEIRD!!

I wonder if it’s got something to do with DST? Daylight saving time in United Kingdom began at 01:00 on Sunday 31 March

It could. What’s your time difference from utc? 1 hour?

Yes, I’m one hour ahead of UTC.

Seems like that would be the cause. The good news is that this will only happen every 5 years.

I really don’t understand how DST had that effect on the automation but if it’s only once every five years I suppose I can just forget about it! Just don’t understand the logic.

Well you said that daylight savings occurs 1 hour past midnight, and you are 1 hour ahead. In UTC that makes the time change happen at UTC midnight. It’s possible that the time change occurred slightly before midnight UTC which caused the sensor to be off by a day.

Oh I see - I think! Thank you

Weirdly, petro, this happened again last night !

I think it’s a yearly DST thing?