Waze Travel Time Sensor, Next event from Calendars

Hello everyone;

I have been trying for some time to create a Waze Travel Time Sensor that sniffs events from my calendar and updates to the next event. Not for notifications, just to sit on the front end. I have gotten it to find the events, but I am having a difficult time getting it to handle the Waze integration afterward. Here is what I have so far:

template:
  - triggers:
      - platform: time_pattern
        minutes: /1
    actions:
      - service: calendar.get_events
        target:
          entity_id:
            - calendar.finley
            - calendar.family_calendar
            - calendar.work
        data:
          # Fetch events for the next 12 hours
          duration:
            hours: 12
        response_variable: next_events_data

    sensor:
      # sensor to combine events from multiple calendars
      - name: "Next Event Location Sensor"
        unique_id: next_event_location_sensor
        state: >
          {% set events = next_events_data.values() | map(attribute='events') | sum(start=[]) | sort(attribute='start') %}
          {% if events %}
            {{ events[0].summary }}
          {% else %}
            Home
          {% endif %}
        attributes:
          summary: >
            {% set events = next_events_data.values() | map(attribute='events') | sum(start=[]) | sort(attribute='start') %}
            {% if events %}
              {{ events[0].summary }}
            {% else %}
              Home
            {% endif %}
          location: >
            {% set events = next_events_data.values() | map(attribute='events') | sum(start=[]) | sort(attribute='start') %}
            {% if events %}
              {{ events[0].location }}
            {% else %}
              zone.home
            {% endif %}
          start_time: >
            {% set events = next_events_data.values() | map(attribute='events') | sum(start=[]) | sort(attribute='start') %}
            {% if events %}
              {{ events[0].start }}
            {% endif %}
          friendly_start_time: >
            {% set events = next_events_data.values() | map(attribute='events') | sum(start=[]) | sort(attribute='start') %}
            {% if events %}
            {{ as_datetime(events[0].start).strftime('%-I:%M %p') }}
            {% endif %}


### This part doesn't work: ###
  - triggers:
      - platform: time_pattern
        minutes: /1
    actions:
      - action: waze_travel_time.get_travel_times
        data:
          origin: person.stiofan
          destination: "{{ state_attr('sensor.next_event_location_sensor', 'location') }}"
          region: na
        response_variable: waze_eta_result
        continue_on_error: true
      - variables:
          wiggle_room: "{{ 5 }}"
          waze_duration: "{{ waze_eta_result['routes'][0].duration | round(0) }}"
          start_time: "{{ state_attr('sensor.next_event_location_sensor', 'friendly_start_time') }}"
          start_time_friendly: "{{ as_datetime(start_time).strftime('%-I:%M %p') }}"
          location: "{{ state_attr('sensor.next_event_location_sensor', 'location') }}"
          summary: "{{ state_attr('sensor.next_event_location_sensor', 'summary') }}"
          departure_time: >-
            {{ as_datetime(start_time) - timedelta(minutes=waze_duration) -
            timedelta(minutes=wiggle_room) }}
          departure_time_friendly: "{{ as_datetime(departure_time).strftime('%I:%M %p') }}"
          reminder_time: "{{ as_datetime(departure_time) - timedelta(minutes=30) }}"
          sensor_window: "{{ as_datetime(start_time) + timedelta(minutes=30) }}"
    sensor:
      - name: "Next Event Travel Time"
        unique_id: next_event_travel_time
        state: waze_duration
        #state: "{{ state_attr('sensor.waze_travel_time_next_event', 'duration') | round() }}"
        unit_of_measurement: "minutes"
        attributes:
          destination: "{{ state_attr('sensor.next_event_location_sensor', 'location') }}"
          last_update: "{{ state_attr('sensor.travel_time_to_work', 'last_update') }}"
          event_name: "{{ state_attr('sensor.next_event_location_sensor', 'summary') }}"
          start_time: "{{ state_attr('sensor.next_event_location_sensor', 'friendly_start_time') }}"

There are some remnants of attributes from some testing, as well as some other bits I am still working on - just in case anyone is wondering… like “sensor_window” - this will eventually be used to filter out events that are ongoing and to display the next one… Once of course I get the sensor working in the first place.

Any help is greatly appreciated.

Anybody???

I don’t know anything about the Waze integration, but you could simplify your calendar event sensor by using merge_reponse:

template:
  - triggers:
      - platform: time_pattern
        minutes: /1
    actions:
      - service: calendar.get_events
        target:
          entity_id:
            - calendar.finley
            - calendar.family_calendar
            - calendar.work
        data:
          # Fetch events for the next 12 hours
          duration:
            hours: 12
        response_variable: next_events_data
      - variables:
          next_event: |
           {{ merge_response(next_events_data) | sort(attribute='start') | first }}
    sensor:
      # sensor to combine events from multiple calendars
      - name: "Next Event Location Sensor"
        unique_id: next_event_location_sensor
        state: "{{ next_event.summary | default('Home', true) }}"
        attributes:
          summary: "{{ next_event.summary | default('Home', true) }}"
          location: "{{ next_event.location | default('zone.home', true) }}"
          start_time: "{{ next_event.start }}"
          friendly_start_time: "{{ as_datetime(next_event.start).strftime('%-I:%M %p') }}"

You have the state set to a static string, “waze_duration”… but you have defined a unit_of_measurement which requires that the sensor returns a numeric output.

You’ve set your action to continue on error, but the variables that are set in the next block need that data. At a minimum, you need to provide defaults for all those variables if you are going to use continue on error in the waze_travel_time.get_travel_times action.