Variable Help

Hey,

So I’m trying to simply create a To-Do the day before our Garbage is due to go out. I have a calendar that has the upcoming Garbage days and the types of collection:

image

My goal is to create a To-Do List Item the day before the event. I’d like to include a due date and have the Pickup Types in the collection.

service: calendar.get_events
metadata: {}
data:
  start_date_time: "{{ as_timestamp(now()) | timestamp_custom(\"%Y-%m-%d\") }}"
  end_date_time: "{{ as_timestamp(now()) | timestamp_custom(\"%Y-%m-%d\") }}"
target:
  entity_id: calendar.recollect_waste
response_variable: Type

service: todo.add_item
metadata: {}
data:
  item: Take out the Garbage
  due_date: "{{ as_timestamp(now()) | timestamp_custom(\"%Y-%m-%d\") }}"
  description: "{{Type}}"
target:
  entity_id: todo.chores

The above works, mostly. Except I get a long string in the description:

{‘calendar.recollect_waste’: {‘events’: [{‘start’: ‘2024-01-17’, ‘end’: ‘2024-01-18’, ‘summary’: ‘ReCollect Waste Pickup’, ‘description’: ‘Pickup types: Garbage cart, Recycling’, ‘location’: ‘Edmonton’}, {‘start’: ‘2024-01-22’, ‘end’: ‘2024-01-23’, ‘summary’: ‘ReCollect Waste Pickup’, ‘description’: ‘Pickup types: Christmas tree collection begins’, ‘location’: ‘Edmonton’}, {‘start’: ‘2024-01-24’, ‘end’: ‘2024-01-25’, ‘summary’: ‘ReCollect Waste Pickup’, ‘description’: ‘Pickup types: Food scraps cart, Recycling’, ‘location’: ‘Edmonton’}, {‘start’: ‘2024-01-31’, ‘end’: ‘2024-02-01’, ‘summary’: ‘ReCollect Waste Pickup’, ‘description’: ‘Pickup types: Garbage cart, Recycling’, ‘location’: ‘Edmonton’}, {‘start’: ‘2024-02-07’, ‘end’: ‘2024-02-08’, ‘summary’: ‘ReCollect Waste Pickup’, ‘description’: ‘Pickup types: Food scraps cart, Recycling’, ‘location’: ‘Edmonton’}, {‘start’: ‘2024-02-14’, ‘end’: ‘2024-02-15’, ‘summary’: ‘ReCollect Waste Pickup’, ‘description’: ‘Pickup types: Garbage cart, Recycling’, ‘location’: ‘Edmonton’}]}}

So, I’m close, but I can’t figure out how to just have the description field taken out of the calendar. Also, I can’t figure out why I’m getting more events returning than I asked for?

Finally, I might have to tweak this, because I want to run this on the day before the event turns on.

trigger:
  - platform: calendar
    event: start
    offset: "-24:0:0"
    entity_id: calendar.recollect_waste
condition: []
action:
  - service: calendar.get_events
    metadata: {}
    data:
      start_date_time: "{{ today_at() + timedelta(days=1) }}"
      duration: 
        hours: 36
    target:
      entity_id: calendar.recollect_waste
    response_variable: Type
  - variables:
      tom_events: "{{ Type['calendar.recollect_waste']['events'] | selectattr('start', 'search', (now()+timedelta(days=1)).date() | string) }}"
      events_bool: "{{ tom_events | count > 0 }}"
  - condition: template
    value_template: "{{ events_bool }}"
  - service: todo.add_item
    metadata: {}
    data:
      item: Take out the Garbage
      due_date: "{{ today_at() }}"
      description: "{{ tom_events|map(attribute='description')|first }}"
    target:
      entity_id: todo.chores

EDIT:

  • Fixed typo in selectattr()
  • Added condition to prevent todo’s when no calendar events exist
1 Like

Thanks for the help. For some reason it’s not adding the description to the task properly. I’ll keep playing.

Sorry, I had a typo. The selectattr() should be using search as its test not eq. I have corrected it above.

Thank you so much. It still doesn’t add a description in my test, but the next event isn’t until next week, so I’ll see if it triggers properly Tuesday.