Hi
I’m working on tracking waste collection events from my calendar. My goal is to filter events by waste type (e.g., Food Waste, Non-Recyclable Waste, etc.) and use these filtered results to populate the template sensors for each waste type. My below configuration works and populates everything correctly, but I feel like there has to be a more efficient and cleaner way to do this.
Ideally, I’d like to perform the selectattr
operation just once for each waste type and reuse the result, minimizing redundant processing. Is there a way to do this?
Here’s an overview of my functional but bulky setup:
template:
- trigger:
- platform: time_pattern
seconds: /10
action:
- service: calendar.list_events
target:
entity_id: calendar.waste_collection
data:
duration:
days: 14
response_variable: scheduled_waste_events
sensor:
- name: Food Waste Collection Events
state: |
{{ scheduled_waste_events.events | selectattr('summary', 'search', 'Food', true) | list | count() }}
attributes:
scheduled_events: |
{{ scheduled_waste_events.events | selectattr('summary', 'search', 'Food', true) | list }}
next_collection_date: |
{{ (scheduled_waste_events.events | selectattr('summary', 'search', 'Food', true) | list | first).start }}
countdown: |
{{ int((as_timestamp((scheduled_waste_events.events | selectattr('summary', 'search', 'Food', true) | list | first).start) - as_timestamp(today_at('00:00')))/86400)}}
- name: Non-recyclable Waste Collection Events
state: |
{{ scheduled_waste_events.events | selectattr('summary', 'search', 'Non-Recyclable', true) | list | count() }}
attributes:
scheduled_events: |
{{ scheduled_waste_events.events | selectattr('summary', 'search', 'Non-Recyclable', true) | list }}
next_collection_date: |
{{ (scheduled_waste_events.events | selectattr('summary', 'search', 'Non-Recyclable', true) | list | first).start }}
countdown: |
{{ int((as_timestamp((scheduled_waste_events.events | selectattr('summary', 'search', 'Non-Recyclable', true) | list | first).start) - as_timestamp(today_at('00:00')))/86400)}}
- name: Paper Cardboard Waste Collection Events
state: |
{{ scheduled_waste_events.events | selectattr('summary', 'search', 'Paper', true) | list | count() }}
attributes:
scheduled_events: |
{{ scheduled_waste_events.events | selectattr('summary', 'search', 'Paper', true) | list }}
next_collection_date: |
{{ (scheduled_waste_events.events | selectattr('summary', 'search', 'Paper', true) | list | first).start }}
countdown: |
{{ int((as_timestamp((scheduled_waste_events.events | selectattr('summary', 'search', 'Paper', true) | list | first).start) - as_timestamp(today_at('00:00')))/86400)}}
# And so on for each waste type...
Thanks.