Find the furthest address from a list of calendar events

I am trying to use home assistant to pull a list of events for the next day in my calendar, then for each calendar event pull the location and calculate driving distance using Waze Travel Time, then return the one with the longest distance to determine my charge limit for my electric vehicle.

I seem to be in a egg first or chicken first dilemma… The only way I know to handle a list of calendar locations is using template within a service, like in this example

service: notify.nina
data:
  title: Daily agenda for {{ now().date() }}
  message: >-
    Your school calendar for today:
    {% for event in agenda["calendar.school_calendar"]["events"] %}
    {{ event.start}}: {{ event.summary }}<br>
    {% endfor %}
    Your work calendar for today:
    {% for event in agenda["calendar.work_calendar"]["events"] %}
    {{ event.start}}: {{ event.summary }}<br>
    {% endfor %}

But in order to pull the address out for Waze Travel time I have to do a service call to save it into an input_text or sensor, then somehow record the distance attribute from Waze Travel Time before moving on to the next event int he foreach loop. I simply do not know how to make all these service calls which is within a value template of another service call?

This is my pseudo-code:

- get calendar events into response variable *agenda*
- set input_number.distance to 0
- for each event in *agenda*
    - use input_text.set_value to save location from *agenda*
    - if Waze Travel time is greater than input_number.distance, set input_number.distance to current distance from Waze Travel Time
- when greatest input_number.distance is determined, set charge limit with a value_template of if/elseif loop to the desired value

Can someone provide some insights? Thanks!

1 Like

Hi, did you ever get this working?

I’m trying to create a slightly different automation using a similar idea of grabbing the distances for all the following days calendar events that contain a location.

This is what I ended up doing

  - service: input_number.set_value
    target:
      entity_id: input_number.destination_distance
    data:
      value: 0
  - service: calendar.get_events
    target:
      entity_id:
        - calendar.home
    data:
      duration:
        hours: 24
    response_variable: agenda
  - variables:
      event: "{{ agenda['calendar.home'].events }}"
  - repeat:
      for_each: "{{ event }}"
      sequence:
      - service: input_text.set_value
        target:
          entity_id: input_text.dest_address
        data:
          value: "{{ repeat.item.location }}"
      - if:
        - condition: template
          value_template: "{{ states('input_text.dest_address') != '' }}"
        - service: homeassistant.update_entity
          entity_id: sensor.calendar_destination
        - delay: "00:00:02"
        - if:
          - condition: template
            value_template: "{{ state_attr('sensor.calendar_destination', 'distance')|int > states('input_number.destination_distance')|int }}"
          then:
          - service: input_number.set_value
            target:
              entity_id: input_number.destination_distance
            data:
              value: "{{ state_attr('sensor.calendar_destination', 'distance')|int }}"
          - service: input_text.set_value
            target:
              entity_id: input_text.destination_event_name
            data:
              value: "{{ repeat.item.summary }}"

I’ve just come across this and I think I can almost decipher what you’re doing.

But could you please explain what the input_number and any other helpers you have setup are and what they’re doing/for?
Thanks!

Input number is to compare and store the greatest distance. For example

9:30: 15 minutes
13:00: 60 minutes
16:00: 30 minutes

It’ll first process the 9:30 appointment. Input number is first initialized to 0. Since 15>0, itll store that into input number

Then it’ll process 13:00. Input number is 15, and it’s smaller than 60, so now it’ll store 60 into input number

Lastly when it processes 16:00, input number is 60, and it’s greater than 30, so it won’t overwrite 60 with 30.

That way you get the longest distance of the day.

You’ll need to create a sensor in Waze travel time and use the input_text.dest_address as destination and car current location (or home) as origin.

Wow! This is great! I’ll reuse this with the google distance matrix API since I’m in south america and Waze Travel Time is not compatible with my region. Thank you!