So I’ve got a sensor (informed by a RESTful call) which gives me the calendar date dd/mm/yyyy for my recycling collection and another for my refuse collection.
I’m really struggling with the simplest, leanest way of getting an alert the evening before. Found LOADS of examples for if you’re using Google Calendar, but I’m not.
So far I’m thinking this might be the way to go (if I can get that code to accept my sensors as inputs).
Any better ideas?
So, let’s say your sensor is named sensor.collection, and you’ve enabled the Time & Date Sensor with 'date_time' selected (resulting in a sensor named sensor.date__time.) Then you could do this:
automation:
- alias: Alert if tomorrow is collection day
trigger:
platform: template
value_template: >
{% set d = (strptime(states('sensor.collection'), '%d/%m/%Y')
.timestamp() - 24*60*60)|timestamp_custom('%Y-%m-%d') %}
{{ states('sensor.date__time') == d ~ ', 18:00' }}
action:
...
This takes the collection date, converts it to a Python datetime object, then uses its timestamp function to turn it into a timestamp. Then subtracts a day’s worth of seconds (so now you have the date of the day before the next collection.) Then converts that back into a date string in the same format that sensor.date__time uses. Lastly, it concatenates the time you want the alert to go off, and compares that to sensor.date__time. When they are the same that will trigger the automation. The automation will not trigger again until that evaluates to false, and then back to true, so you should only get one trigger per collection date.
Note that I’m still using 0.84.6, and I think there might have been a change after that that causes the date/time sensor’s entity_id to be different. So, adjust accordingly.
Is it a regular schedule like every 2 weeks? or any king or regular schedule? If it is then you don’t need a calendar. I use this:
# Download the Bin icons from my github https://github.com/DavidFW1960/home-assistant/tree/master/www/icons/recyclinggreenbin
# and save them under /www/icons/recyclinggreenbin/ in your configuration folder.
# Show Bins Sensors
sensor:
- platform: template
sensors:
recycling_bin_tomorrow:
entity_id: []
friendly_name: "Recycling Bin Tomorrow"
value_template: "{{ ((as_timestamp(now()) - as_timestamp('2019-01-21 00:00:00'))) / 86400 |int % 14 < 1 }}"
entity_picture_template: >-
{{ '/local/icons/recyclinggreenbin/recycling_bin_tomorrow.png'}}
recycling_bin_today:
entity_id: []
friendly_name: "Recycling Bin Today"
value_template: "{{ ((as_timestamp(now()) - as_timestamp('2019-01-22 00:00:00'))) / 86400 |int % 14 < 1 }}"
entity_picture_template: >-
{{ '/local/icons/recyclinggreenbin/recycling_bin_today.png'}}
green_bin_tomorrow:
entity_id: []
friendly_name: "Green Bin Tomorrow"
value_template: "{{ ((as_timestamp(now()) - as_timestamp('2019-01-28 00:00:00'))) / 86400 |int % 14 < 1 }}"
entity_picture_template: >-
{{ '/local/icons/recyclinggreenbin/green_waste_bin_tomorrow.png'}}
green_bin_today:
entity_id: []
friendly_name: "Green Bin Today"
value_template: "{{ ((as_timestamp(now()) - as_timestamp('2019-01-29 00:00:00'))) / 86400 |int % 14 < 1 }}"
entity_picture_template: >-
{{ '/local/icons/recyclinggreenbin/green_waste_bin_today.png'}}
automation:
- alias: 'Update Bin Night'
trigger:
- event: start
platform: homeassistant
- platform: time_pattern
hours: '/1'
minutes: 0
seconds: 0
action:
- service: homeassistant.update_entity
entity_id: sensor.recycling_bin_today, sensor.recycling_bin_tomorrow, sensor.green_bin_today, sensor.green_bin_tomorrow
- alias: 'Bin Night'
initial_state: 'on'
trigger:
- platform: time
at: '19:00:00'
condition:
condition: and
conditions:
- condition: template
value_template: '{{ now().strftime("%H:%M") == "19:00" and now().weekday() == 1 }}'
- condition: or
conditions:
- condition: state
entity_id: sensor.recycling_bin_today
state: 'True'
- condition: state
entity_id: sensor.green_bin_today
state: 'True'
action:
- service: notify.notify
data_template:
message: '{%- if (states.sensor.recycling_bin_today.state) == "True" -%} Recycling Bin Night {%- else -%} Green Bin Tonight {%- endif -%}'
title: Bins Out Tonight
That is a package… I also have some conditional cards in lovelace to display as well as the notification I use on the due date. The dates I have put in are any date in the past (or future actually) when the condition will be true and it just looks for an even number of 2 weeks in my case… (that’s what the int % 14 does)
I can give you the lovelace cards as well if you want them. I am only interested in notifications the night I put out the bins but the lovelace cards tell me when it’s going to be tomorrow or today as well.
This is cool, definitely one to remember. I guess the reason I went with a calendar is because I’m currently renting and changing a calendar to suit my new location (and thus bin day) is nicer than editing code and restarting HA.
Thank you - this looks brilliant, however, even when I remove the double underscore, I still get Error during template condition: UndefinedError: 'str object' has no attribute 'timestamp'
lots of times in my log.
Have tried fiddling with it in template editor but no joy so far. Will continue to Google and fiddle.
Also, thank you @sparkydave - but I can’t (don’t think I can?) use your kind example because I’m not using Google calendar. Still working at getting the value template working.
You said the date was in this format: dd/mm/yyyy. If that is not the case, then the strptime function will just “pass through” the string instead of converting it to a Python datetime object. What exactly does the state of that sensor look like?
I think I’ve noticed where I’m going wrong. So sorry. My date is dd/mm/yy. Not yyyy. I’ve amended %Y to %y to account for my misinformation and am excitedly restarting HA now…
Sorry…Fingers crossed…
Hi @DavidFW1960 I managed to get this to work by adding 4 separate conditional cards. Next thing I don’t get is where do you define what night is bin night for you?
It’s in the template with the date… the date is any night that correcponds with the bin night and then it just looks for every even 2 weeks from that date.