Change rubbish collection sensor every Thursday - Help?

My rubbish collection is weekly on a Wednesday. Schedule is one week, general waste and recycling, the following week, recycling only. I’ve come up with the following which works…

bintype:
  entity_id: sensor.date
  friendly_name: 'Rubbish Collection'
  value_template: >-
    {%- if (((now().strftime("%W") | int) % 2) == 0) -%}
      General Waste and Recycling
    {%- else -%}
      Recycling Only
    {%- endif -%}
  icon_template: >-
    {%- if (((now().strftime("%W") | int) % 2) == 0) -%}
      mdi:delete
    {%- else -%}
      mdi:recycle
    {%- endif -%}

But because I’m using %W, the sensor is only changing on the Sun/Mon which means that on Thurs/Fri/Sat/Sun the sensor is showing the collection which has been. It works for the weekly Tuesday night reminder automation but I’d like to improve it for any displays.

Any ideas for getting it to change on the Wed/Thurs? I’m hoping that there’s an elegant mathematical solution using days perhaps +offset to Wed/Thurs, but I just can’t see it.

Thanks.

This is how I do mine I have normal trash every week with green waste and recycling alternating weeks.

    #################################################################
    #                                                               #
    #                    Packages/Trash Recycle                     #
    #                                                               #
    #################################################################

    #################################################################
    #                                                               #
    #                          Customize                            #
    #                                                               #
    #################################################################

    homeassistant:
      customize:
        sensor.trash_day:
          friendly_name: Is it Trash Day today?
          icon: mdi:delete-variant
        device_tracker.trash_bin:
          friendly_name: Trash Bin
          icon: fas:trash-alt
        sensor.recycle_day:
          friendly_name: Is it Recycle Day today?
          icon: mdi:recycle
        sensor.green_day:
          friendly_name: Is it Green Waste Day today?
          icon: mdi:leaf
        sensor.trash_pickup_day:
          friendly_name: Trash Pickup Day
          icon: mdi:calendar-today
          hidden: true
        sensor.recycle_pickup_day:
          friendly_name: Recycle Pickup Day
          icon: mdi:calendar-today
          hidden: true
        sensor.recycle_pickup_week:
          friendly_name: Recycle Pickup Week
          icon: mdi:calendar-today
          hidden: true
        sensor.green_pickup_day:
          friendly_name: Green Pickup Day
          icon: mdi:calendar-today
          hidden: true
        sensor.green_pickup_week:
          friendly_name: Green Pickup Week
          icon: mdi:calendar-today
          hidden: true
        sensor.current_week:
          friendly_name: Current Week is
          icon: mdi:calendar-question
        group.trash_recycle:
          friendly_name: Trash & Recycle
          icon: mdi:recycle
          
    #################################################################
    #                                                               #
    #                           Group                               #
    #                                                               #
    #################################################################

    group:
      trash schedule:
        name: Trash Schedule
        view: no
        entities:
          - sensor.trash_day
          - input_select.trash_reminders
          - sensor.trash_pickup_day
          - input_select.trash_pickup_day

      Recycle Schedule:
        control: hidden
        entities:
          - sensor.recycle_day
          - sensor.recycle_pickup_day
          - sensor.recycle_pickup_week
          - input_select.recycle_pickup_day
          - sensor.current_week
          - input_select.recycle_pickup_week
          
      Green Waste Schedule:
        control: hidden
        entities:
          - sensor.green_day
          - sensor.green_pickup_day
          - sensor.green_pickup_week
          - input_select.green_pickup_day
          - sensor.current_week
          - input_select.green_pickup_week

      Date Time:
        control: hidden
        entities:
          - sensor.time
          - sensor.date
          - sensor.time_utc
          - sensor.date_time
          - sensor.time_date
          - sensor.internet_time
          - sensor.rocket_launch_time
          - sensor.beat
          
    #################################################################
    #                                                               #
    #                        User Inputs                            #
    #                                                               #
    #################################################################

    input_select:
      trash_pickup_day:
        name: Current Trash Pickup Day (Evey Week)
        options:
         - Monday
         - Tuesday
         - Wednesday
         - Thursday
         - Friday
         - Saturday
         - Sunday
        icon: mdi:delete-variant
      recycle_pickup_day:
        name: Current Recycle Pickup Day (Every Other Week)
        options:
         - Monday
         - Tuesday
         - Wednesday
         - Thursday
         - Friday
         - Saturday
         - Sunday
        icon: mdi:recycle
      recycle_pickup_week:
        name: Select Recycle Pickup Week based on Current Week above
        options:
         - Even Weeks
         - Odd Weeks
        icon: mdi:recycle
      green_pickup_day:
        name: Current Green Pickup Day (Every Other Week)
        options:
         - Monday
         - Tuesday
         - Wednesday
         - Thursday
         - Friday
         - Saturday
         - Sunday
        icon: mdi:leaf
      green_pickup_week:
        name: Select Recycle Pickup Week based on Current Week above
        options:
         - Even Weeks
         - Odd Weeks
        icon: mdi:leaf
      trash_reminders:
        name: Trash Reminder
        options:
         - Bins Taken Out
         - Remind Later
         - Reset Trash Reminders
         
    input_boolean:
      trash_reminders:
        name: Trash Reminders
        initial: on
        icon: mdi:delete-variant
        
    #################################################################
    #                                                               #
    #                           Sensors                             #
    #                                                               #
    #################################################################

    sensor:

    ####################################################
    #                                                  #
    #                   Sensor - MQTT                  #
    #                                                  #
    ####################################################

      - platform: mqtt
        state_topic: "/home/trashpickupday"
        name: "Trash Pickup Day"
        value_template: "{{ value }}"
        qos: 1
      - platform: mqtt
        state_topic: "/home/recyclepickupday"
        name: "Recycle Pickup Day"
        value_template: "{{ value }}"
        qos: 1
      - platform: mqtt
        state_topic: "/home/recyclepickupweek"
        name: "Recycle Pickup Week"
        value_template: "{{ value }}"
        qos: 1
      - platform: mqtt
        state_topic: "/home/greenpickupday"
        name: "Green Pickup Day"
        value_template: "{{ value }}"
        qos: 1
      - platform: mqtt
        state_topic: "/home/greenpickupweek"
        name: "Green Pickup Week"
        value_template: "{{ value }}"
        qos: 1


    ####################################################
    #                                                  #
    #                 Sensor - Template                #
    #                                                  #
    ####################################################
        
    ## Sensor to hold info about current week is an odd week or an even week of the year

      - platform: template
        sensors:
          current_week:
            value_template: >-
              {% set year = states.sensor.date_time.state.split(',')[0].split('-')[0] %}
              {% set month = states.sensor.date_time.state.split(',')[0].split('-')[1] %}
              {% set date = states.sensor.date_time.state.split(',')[0].split('-')[2] %}
              {% set today = strptime(year ~ '-' ~ month ~ '-' ~ date , '%Y-%m-%d') %}
              {%- if (as_timestamp(today) | timestamp_custom('%U', true) | int ) % 2 == 0 -%}
                Even Week (Week# {{ as_timestamp(today) | timestamp_custom('%U', true) }})
              {%- else -%}
                Odd Week (Week# {{ as_timestamp(today) | timestamp_custom('%U', true) }})
              {%- endif -%}
              
    ## Trash  - Pickup schedule is EVERY week.
    ## Set the day to a day before the actual day leaving time for reminders

      - platform: template
        sensors:
          trash_day:
            value_template: >-
              {% set year = states.sensor.date_time.state.split(',')[0].split('-')[0] %}
              {% set month = states.sensor.date_time.state.split(',')[0].split('-')[1] %}
              {% set date = states.sensor.date_time.state.split(',')[0].split('-')[2] %}
              {% set today = strptime(year ~ '-' ~ month ~ '-' ~ date , '%Y-%m-%d') %}
              {%- set pickupDay = states.sensor.trash_pickup_day.state | lower -%}
              {%- if as_timestamp(today)| timestamp_custom('%A', true) | lower == pickupDay -%}
                yes
              {%- else -%}
                no
              {%- endif -%}

    ## Recycle - Pickup schedule is every other week.
    ## Set the day to a day before the actual day leaving time for reminders

      - platform: template
        sensors:
          recycle_day:
            value_template: >-
              {% set year = states.sensor.date_time.state.split(',')[0].split('-')[0] %}
              {% set month = states.sensor.date_time.state.split(',')[0].split('-')[1] %}
              {% set date = states.sensor.date_time.state.split(',')[0].split('-')[2] %}
              {% set today = strptime(year ~ '-' ~ month ~ '-' ~ date , '%Y-%m-%d') %}
              {%- set pickupDay = states.sensor.recycle_pickup_day.state | lower -%}
              {% if states.input_select.recycle_pickup_week.state | lower == "even weeks" %}
                {%- set evenWeekPickup = "true" %}
              {% else %}
                {%- set evenWeekPickup = "false" %}
              {% endif %}
              {%- macro day_of_week(timestamp) -%}
                {{ as_timestamp(timestamp)| timestamp_custom('%A', true) | lower }}
              {%- endmacro %}
              {%- macro week_number_of_year() -%}
                {{ as_timestamp(today) | timestamp_custom('%U', true) | int }}
              {%- endmacro %}
              {%- macro is_it_this_week() -%}
                {%- if as_timestamp(today) | timestamp_custom('%U', true) | int % 2 == 0 -%}
                  {%- if evenWeekPickup == "true" -%}
                    true
                  {%- else -%}
                    false
                  {%- endif -%}
                {%- else -%}
                  {%- if evenWeekPickup == "true" -%}
                    false
                  {%- else -%}
                    true
                  {%- endif -%}
                {%- endif -%}
              {%- endmacro -%}
              {%- macro is_it_today() -%}
              {%- if is_it_this_week() == "true" -%}
                {%- if day_of_week(today) | lower == pickupDay -%}
                  yes
                {%- else -%}
                  no
                {%- endif -%}
              {%- else -%}
                no
              {%- endif -%}
              {%- endmacro -%}
              {{- is_it_today() -}}
              
    ## Green Waste - Pickup schedule is every other week.
    ## Set the day to a day before the actual day leaving time for reminders

      - platform: template
        sensors:
          green_day:
            value_template: >-
              {% set year = states.sensor.date_time.state.split(',')[0].split('-')[0] %}
              {% set month = states.sensor.date_time.state.split(',')[0].split('-')[1] %}
              {% set date = states.sensor.date_time.state.split(',')[0].split('-')[2] %}
              {% set today = strptime(year ~ '-' ~ month ~ '-' ~ date , '%Y-%m-%d') %}
              {%- set pickupDay = states.sensor.green_pickup_day.state | lower -%}
              {% if states.input_select.green_pickup_week.state | lower == "even weeks" %}
                {%- set evenWeekPickup = "true" %}
              {% else %}
                {%- set evenWeekPickup = "false" %}
              {% endif %}
              {%- macro day_of_week(timestamp) -%}
                {{ as_timestamp(timestamp)| timestamp_custom('%A', true) | lower }}
              {%- endmacro %}
              {%- macro week_number_of_year() -%}
                {{ as_timestamp(today) | timestamp_custom('%U', true) | int }}
              {%- endmacro %}
              {%- macro is_it_this_week() -%}
                {%- if as_timestamp(today) | timestamp_custom('%U', true) | int % 2 == 0 -%}
                  {%- if evenWeekPickup == "true" -%}
                    true
                  {%- else -%}
                    false
                  {%- endif -%}
                {%- else -%}
                  {%- if evenWeekPickup == "true" -%}
                    false
                  {%- else -%}
                    true
                  {%- endif -%}
                {%- endif -%}
              {%- endmacro -%}
              {%- macro is_it_today() -%}
              {%- if is_it_this_week() == "true" -%}
                {%- if day_of_week(today) | lower == pickupDay -%}
                  yes
                {%- else -%}
                  no
                {%- endif -%}
              {%- else -%}
                no
              {%- endif -%}
              {%- endmacro -%}
              {{- is_it_today() -}}
              
          trash_bin_location:
            value_template: >-
              {% if is_state('device_tracker.trash_bin', 'home') %}
                Home
              {% elif is_state('device_tracker.trash_bin', 'not_home') and is_state('sensor.trash_day', 'yes') %}
                Out for Collection
              {% elif is_state('device_tracker.trash_bin', 'not_home') and is_state('sensor.trash_day', 'no') %}
                Ready to be Returned
              {% else %}
                Unknown
              {% endif %}
            icon_template: >-
              {% if is_state('device_tracker.trash_bin', 'home') %}
                fas:trash-alt
              {% elif is_state('device_tracker.trash_bin', 'not_home') %}
                fas:trash-alt
              {% else %}
                mdi:alert
              {% endif %}

          trash:
            friendly_name: Trash
            entity_id: sensor.trash_day
            value_template: >-
              {% if is_state('sensor.trash_day', 'yes') %}
                100
              {% elif is_state('sensor.trash_day', 'no') %}
                0
              {% endif %}
              
          recycle:
            friendly_name: Recycle
            entity_id: sensor.recycle_day
            value_template: >-
              {% if is_state('sensor.recycle_day', 'yes') %}
                100
              {% elif is_state('sensor.recycle_day', 'no') %}
                0
              {% endif %}
              
          green:
            friendly_name: Green
            entity_id: sensor.green_day
            value_template: >-
              {% if is_state('sensor.green_day', 'yes') %}
                100
              {% elif is_state('sensor.green_day', 'no') %}
                0
              {% endif %}  
              
    #################################################################
    #                                                               #
    #                       Binary Sensors                          #
    #                                                               #
    #################################################################
              
    ### Lovelace UI ###

    binary_sensor:

    ####################################################
    #                                                  #
    #             Binary Sensor - Template             #
    #                                                  #
    ####################################################
      - platform: template
        sensors:

          trash:
            friendly_name: Trash
            entity_id: sensor.trash_day
            value_template: "{{ is_state('sensor.trash_day', 'yes') }}"
            icon_template: >-
              {% if is_state('sensor.trash_day', 'yes') %}
                fas:trash-alt
              {% elif is_state('sensor.trash_day', 'no') %}
                fas:trash-alt
              {% else %}
                mdi:alert
              {% endif %}
              
          recycle:
            friendly_name: Recycle
            entity_id: sensor.recycle_day
            value_template: "{{ is_state('sensor.recycle_day', 'yes') }}"
            icon_template: >-
              {% if is_state('sensor.recycle_day', 'yes') %}
                fas:recycle
              {% elif is_state('sensor.recycle_day', 'no') %}
                fas:recycle
              {% else %}
                mdi:alert
              {% endif %}
              
          green:
            friendly_name: Green
            entity_id: sensor.green_day
            value_template: "{{ is_state('sensor.green_day', 'yes') }}"
            icon_template: >-
              {% if is_state('sensor.green_day', 'yes') %}
                mdi:leaf
              {% elif is_state('sensor.green_day', 'no') %}
                mdi:leaf
              {% else %}
                mdi:alert
              {% endif %}
              
    #################################################################
    #                                                               #
    #                          Automations                          #
    #                                                               #
    #################################################################

    ## Some automations for this package have now been moved to Node-RED please see Node-Red Flows folder within this package for more infomation! ##

    automation:

      - alias: Reset Trash Reminders
        initial_state: true
        hide_entity: true
        trigger:
          - platform: time
            at: '09:00:00'
        action:
          - service: input_boolean.turn_on
            entity_id: input_boolean.trash_reminders
          - service: input_select.select_option
            data_template:
              entity_id: input_select.trash_pickup_day
              option: "{{states.sensor.trash_pickup_day.state}}"
          - service: input_select.select_option
            data_template:
              entity_id: input_select.recycle_pickup_day
              option: "{{states.sensor.recycle_pickup_day.state}}"
          - service: input_select.select_option
            data_template:
              entity_id: input_select.recycle_pickup_week
              option: "{{states.sensor.recycle_pickup_week.state}}"
1 Like

This reddit post might help - this approach works great for me:

Try this:

bintype:
  entity_id: sensor.date
  friendly_name: 'Rubbish Collection'
  value_template: >-
    {% set week = now().isocalendar()[1] %}
    {% set week = week + 1 if now().isoweekday() > 3 else week %}
    {{ 'Recycling Only' if week % 2 == 0 else 'General Waste and Recycling' }}
  icon_template: >-
    {% set week = now().isocalendar()[1] %}
    {% set week = week + 1 if now().isoweekday() > 3 else week %}
    {{ 'mdi:recycle' if week % 2 == 0 else 'mdi:delete' }}

A week starts on Monday (day 1) for isoweekday. If the current day is beyond Wednesday ( the day is > 3) then the template increments week before performing the calculation.

This template uses isocalendar and isoweekday which don’t produce the same results as strftime('%W') and weekday.

Screenshot%20from%202019-05-11%2014-53-25

In your template, if the week is even, it’s ‘General Waste and Recycling’. Because isocalendar produces a different result, I have chosen that if the week is odd then it reports ‘General Waste and Recycling’. Should you discover this is incorrect, just swap the order of ‘Recycling Only’ and ‘General Waste and Recycling’.


EDIT
Corrected inline if statement. It was missing else.

2 Likes

I have collection days marked on my Google calendar, then an automation that gets the details and sets a light on in the house that alerts what the collection day is.
This is my recycling week automation, my recycling bin has a yellow lid, so the indication in yellow.

- id: '1557238632971'
  alias: Recycling Week
  trigger:
  - entity_id: calendar.steve_menheere
platform: state
to: 'on'
  condition:
  - condition: template
value_template: '{{ states.calendar.steve_menheere.attributes.message == ''Recycling''
  }}'
  action:
  - data:
  brightness: '50'
  color_name: yellow
  entity_id: light.lounge_1
  power: 'true'
service: light.lifx_set_state

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'
    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: 'true'
    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.david_ios_notify
        data_template:
          message: '{%- if (states.sensor.recycling_bin_today.state) == "True" -%} Recycling Bin Night {%- else -%} Green Bin Tonight {%- endif -%}'
          title: Bins Out Tonight

You set the dates so the first date is correct and then it runs on a 2 week cycle… One week we have green bin and the other week yellow recycle bin.

So I don’t screw around setting up calendars - as long as the dates correspond to the correct week I’m golden. Then I use a lovelace conditional card to display it the day before and the day of the collection.

          - 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
2 Likes

Thank you all for the replies, and my apologies for my extremely late reply!

I’ve used the suggestion by @123 and it works great, thank you very much.

For anyone who comes to this post, there is now an integration and lovelace card that provides this same functionality.
The integration;


and card;
2 Likes