More Bin Collection over thinking: Wanting a reminder the day BEFORE an event

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?

Thanks guys

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.

1 Like

this is my package:

#   Package to track bin days based on Google Calendar entries and perform reminders

input_boolean:
  bin_reminder:
    name: Bin Reminder
    icon: mdi:delete-outline
    initial: on

#  https://community.home-assistant.io/t/need-template-assistance-please/80237/7

sensor:
  - platform: template
    sensors:
      recycling_bin_tomorrow:
        entity_id:
          - calendar.recycling_bin_day
          - sensor.time
        friendly_name: "Recycling Bin Tomorrow"
        value_template: >
          {% set e = strptime(
               states.calendar.recycling_bin_day.attributes.start_time,
               '%Y-%m-%d %H:%M:%S') %}
          {{ e.strftime('%j')|int - now().strftime('%j')|int == 1 and now().hour >= 12 }}
  
  - platform: template
    sensors:
      green_waste_bin_tomorrow:
        entity_id:
          - calendar.green_waste_bin_day
          - sensor.time
        friendly_name: "Green Waste Bin Tomorrow"
        value_template: >
          {% set e = strptime(
               states.calendar.green_waste_bin_day.attributes.start_time,
               '%Y-%m-%d %H:%M:%S') %}
          {{ e.strftime('%j')|int - now().strftime('%j')|int == 1 and now().hour >= 12 }}

  - platform: template
    sensors:
      recycling_bin_today:
        friendly_name: "Recycling Bin Today"
        value_template: "{{ is_state('calendar.recycling_bin_day', 'on') }}"

  - platform: template
    sensors:
      green_waste_bin_today:
        friendly_name: "Green Waste Bin Today"
        value_template: "{{ is_state('calendar.green_waste_bin_day', 'on') }}"


automation:
  - alias: 'Bin Reminder TTS'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.recycling_bin_tomorrow
        to: 'True'
      - platform: state
        entity_id: sensor.green_waste_bin_tomorrow
        to: 'True'
      - platform: state
        entity_id: sensor.recycling_bin_today
        to: 'True'
      - platform: state
        entity_id: sensor.green_waste_bin_today
        to: 'True'
    condition:
      - condition: state
        entity_id: input_boolean.bin_reminder
        state: 'on'
    action:
      - wait_template: "{{ is_state('binary_sensor.kitchen_multi_sensor_sensor', 'on') }}"
        timeout: '10:00:00'
        continue_on_timeout: 'false'
      - wait_template: "{{ is_state('media_player.googlehome3019', 'off' or 'idle') }}"
      - service: tts.google_say
        entity_id: media_player.googlehome3019
        data_template:
          message: "Dont forget you need to put out the {{ trigger.to_state.attributes.friendly_name }}"

  - alias: 'Reset Bin Reminder'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.recycling_bin_today
        to: 'True'
        for:
          hours: 24
    action:
      - service: homeassistant.turn_on
        entity_id: input_boolean.bin_reminder

I had help from the usual suspects (a.k.a. pnbruckner & petro) to get the templates sorted :grin:

1 Like

I then use this in lovelace:

  - type: conditional
    card:
      entity: sensor.recycling_bin_tomorrow
      image: /local/images/recycling_bin_tomorrow.png
      show_name: false
      show_state: false
      type: picture-entity
    conditions:
      - entity: sensor.recycling_bin_tomorrow
        state: 'True'


  - type: conditional
    card:
      entity: sensor.recycling_bin_today
      image: /local/images/recycling_bin_today.png
      show_name: false
      show_state: false
      type: picture-entity
    conditions:
      - entity: sensor.recycling_bin_today
        state: 'True'


  - type: conditional
    card:
      entity: sensor.green_waste_bin_tomorrow
      image: /local/images/green_waste_bin_tomorrow.png
      show_name: false
      show_state: false
      type: picture-entity
    conditions:
      - entity: sensor.green_waste_bin_tomorrow
        state: 'True'



  - type: conditional
    card:
      entity: sensor.green_waste_bin_today
      image: /local/images/green_waste_bin_today.png
      show_name: false
      show_state: false
      type: picture-entity
    conditions:
      - entity: sensor.green_waste_bin_today
        state: 'True'

I’m pretty sure that sensor changed to sensor.date_time (only one _ ) in the latest couple of versions of HA

1 Like

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.

4 Likes

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…

1 Like

It worked, of course! Silly me…
Sorry. And thanks for the help!

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.

1 Like

that is brilliant, well done mate!

Well it seemed a much easier solution than google calendar integrations for something that is on a regular schedule to me…

i set an (nexcloud but like google) calendar named “wasted”
entry (time all day) “plastic” or “card” or … (someone every 2 day, some every 15…)

on lovelace an custom calendar-card, view only the “waste” and one day, label “tomorrow pick:”. it’s good for me.

Could you share yours lovelace and notification configuration too :slight_smile:

Notification Automation here:

automation:
  - alias: 'Update Bin Night'
    trigger:
      - event: start
        platform: homeassistant
      - platform: time_pattern
        hours: '/1'
    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'
    trigger:
      - platform: time
        at: '19:00:00'
    condition:
      condition: and
      conditions:
        - "{{ 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.david_ios_notify
        data:
          message: "{%- if states('sensor.recycling_bin_today') == 'True' -%} Recycling Bin Night {%- else -%} Green Bin Tonight {%- endif -%}"
          title: Bins Out Tonight

Lovelace here:

          - type: conditional
            conditions:
              - entity: sensor.recycling_bin_today
                state: 'True'
            card:
              type: picture-entity
              entity: sensor.recycling_bin_today
              show_name: false
              show_state: false
              image: /local/icons/recyclinggreenbin/recycling_bin_today.png
          - type: conditional
            conditions:
              - entity: sensor.recycling_bin_tomorrow
                state: 'True'
            card:
              type: picture-entity
              entity: sensor.recycling_bin_tomorrow
              show_name: false
              show_state: false
              image: /local/icons/recyclinggreenbin/recycling_bin_tomorrow.png
          - type: conditional
            conditions:
              - entity: sensor.green_bin_today
                state: 'True'
            card:
              type: picture-entity
              entity: sensor.green_bin_today
              show_name: false
              show_state: false
              image: /local/icons/recyclinggreenbin/green_waste_bin_today.png
          - type: conditional
            conditions:
              - entity: sensor.green_bin_tomorrow
                state: 'True'
            card:
              type: picture-entity
              entity: sensor.green_bin_tomorrow
              show_name: false
              show_state: false
              image: /local/icons/recyclinggreenbin/green_waste_bin_tomorrow.png
1 Like

This is the Bomb @DavidFW1960 Thanks for sharing.
Do you still use this or have you worked out a way to link into the ACT datasets?