Tracking door dash In home assistant?

ChatGPT setup an integration in Home Assistant to track the status of my most recent DoorDash order using the DoorDash API. This configuration includes token management, error handling, and dynamic order tracking. Below are the details of how to set it up.

Features

  1. Dynamically tracks the most recent DoorDash order.
  2. Displays the current order status as a sensor.
  3. Automatically refreshes the API token every hour or on failure.
  4. Sends notifications when the order status changes (e.g., out_for_delivery or complete).

Configuration

1. Store API Token

Use input_text to store the current API token:

input_text:
  doordash_token:
    name: DoorDash API Token
    initial: "YOUR_INITIAL_ACCESS_TOKEN"  # Replace with your initial token

2. REST Commands

Define REST commands to fetch orders, check order status, and refresh the API token:

rest_command:
  refresh_doordash_token:
    url: "https://api.doordash.com/v1/auth/token"
    method: POST
    headers:
      Content-Type: "application/json"
    payload: >
      {
        "client_id": "YOUR_CLIENT_ID",
        "client_secret": "YOUR_CLIENT_SECRET",
        "grant_type": "client_credentials"
      }

  fetch_latest_order:
    url: "https://api.doordash.com/v1/drive/orders"
    method: GET
    headers:
      Authorization: "Bearer {{ states('input_text.doordash_token') }}"
      Content-Type: "application/json"

  fetch_order_status:
    url: "https://api.doordash.com/v1/drive/fulfillment/{{ order_id }}"
    method: GET
    headers:
      Authorization: "Bearer {{ states('input_text.doordash_token') }}"
      Content-Type: "application/json"

3. Sensors

Latest Order ID: Dynamically fetch the ID of the most recent order:

template:
  - sensor:
      - name: "Latest DoorDash Order ID"
        state: >
          {% if state_attr('sensor.doordash_orders', 'orders') %}
            {{ state_attr('sensor.doordash_orders', 'orders') | sort(attribute='created_at') | last | default('') }}
          {% else %}
            None
          {% endif %}

Order Status: Fetch and display the status of the most recent order:

sensor:
  - platform: rest
    name: "Current DoorDash Order Status"
    resource_template: "https://api.doordash.com/v1/drive/fulfillment/{{ states('sensor.latest_doordash_order_id') }}"
    headers:
      Authorization: "Bearer {{ states('input_text.doordash_token') }}"
      Content-Type: "application/json"
    value_template: >
      {% if value_json is not none and 'status' in value_json %}
        {{ value_json.status }}
      {% else %}
        error
      {% endif %}
    scan_interval: 300  # Check every 5 minutes

4. Automations

Token Refresh: Automatically refresh the token every hour:

automation:
  - alias: Refresh DoorDash API Token Periodically
    trigger:
      - platform: time_pattern
        hours: "/1"  # Refresh every 1 hour
    action:
      - service: rest_command.refresh_doordash_token
      - wait_template: "{{ trigger.event.data['status'] == '200' }}"
      - service: input_text.set_value
        target:
          entity_id: input_text.doordash_token
        data:
          value: "{{ trigger.event.data['json']['access_token'] }}"

Handle API Errors: Refresh the token if the API call fails:

automation:
  - alias: Refresh DoorDash Token on API Error
    trigger:
      - platform: state
        entity_id: sensor.current_doordash_order_status
        to: "error"
    action:
      - service: automation.trigger
        target:
          entity_id: automation.refresh_doordash_api_token_periodically

Notify on Status Update: Send a notification when the order status changes:

automation:
  - alias: Notify on DoorDash Order Status
    trigger:
      - platform: state
        entity_id: sensor.current_doordash_order_status
    condition:
      - condition: template
        value_template: "{{ trigger.to_state.state in ['out_for_delivery', 'complete'] }}"
    action:
      - service: notify.dcord_chatha
        data:
          message: >
            Your DoorDash order is now {{ trigger.to_state.state }}!

Usage

  1. Add your client_id, client_secret, and a valid initial token in secrets.yaml or directly in the configuration.
  2. The system will refresh the token automatically and handle errors gracefully.
  3. The Current DoorDash Order Status sensor will display the status of the most recent order.
  4. Notifications are sent when the status changes.

Let me know if you try this or have any suggestions for improvement! :blush:. I have not tested this yet so it probably needs some refinement, and hopefully someone smarter than me or chatgpt can get it the rest of the way there so we don’t have to use the app notification approach.