[Mealie] - Sync Shopping List as HA-ToDo List

Integration: Mealie

Sync Shopping List to Home Assistants ToDo List.

TIA

Already possible, see here:

EDIT:
here’s the code, for others looking:

rest:
  - resource_template: !secret mealie_shopping_items_api_endpoint
    headers:
      Authorization: !secret mealie_api_token
    params:
      perPage: 1
      queryFilter: checked=false
      orderBy: food.name
      orderDirection: asc
    sensor:
      - name: Mealie Shopping List Items
        availability: >
          {{ value_json['total'] }}
        #Sensor to view items which are on any Mealie shopping list
        #Extract the ingredient data in the following format: "food - quantity unit" eg. "apples - 1.5 kilogram", or "Empty List" if no items are left in the list
        value_template: >
          {# If no items in list, return "Empty List" #}
          {% if value_json['total'] == 0 %}
            Empty List

          {# If distinct food is not defined use display name instead #}
          {% elif value_json['items'][0]['food'] == none %}
            {{ value_json['items'][0]['display'] }}

          {# If quantity is available, include it in the output #}
          {% elif value_json['items'][0]['quantity'] > 0 %}
            {{ value_json['items'][0]['food']['name'] }} - {{ (value_json['items'][0]['quantity'] | round(3) | string).rstrip('0').rstrip('.') }} {{ value_json['items'][0]['unit']['name'] if value_json['items'][0]['unit'] is not none }}

          {# Otherwise, print just the food name #}
          {% else %}
            {{ value_json['items'][0]['food']['name'] }}
            
          {% endif %}
      - name: Mealie Shopping List Item To Delete
        availability: >
          {{ value_json['total'] }}
        #Extract the ingredient's ID. Required to delete through the API later on.
        value_template: >
          {{ value_json['items'][0]['id'] }}

rest_command:
  #Command to delete a Mealing shopping list item
  mealie_delete_from_shopping_list:
    url: >
      {{ url }}/{{ states('sensor.mealie_shopping_list_item_to_delete') }}
    method: delete
    headers:
      Authorization:  !secret mealie_api_token

automation:
  #Automation to regularly refresh the "Mealie Shopping List Items" sensor 
  - id: mealie_update_rest_sensors
    alias: "Mealie - Update Rest Sensors"
    initial_state: "on"
    trigger:
      - platform: time_pattern
        seconds: "/10" #How often do you want to check for updates in Mealie?
    action:
      - service: homeassistant.update_entity
        data:
          entity_id:
            - sensor.mealie_shopping_list_items
            - sensor.mealie_shopping_list_item_to_delete

  #Automation to transfer items from the Mealie shopping list to another list of your choice
  - id: mealie_transfer_shopping_list
    alias: "Mealie - Transfer Shopping List"
    description: "When shopping list items are added to the Mealie shopping list, transfer them to an alternative list."
    mode: queued
    max: 10
    initial_state: "on"
    variables:
      target_shopping_list: todo.groceries_list #CHANGEME: entity ID of your shopping list
      notify_service: notify.mobile_app_michael_s_galaxy_s10 #CHANGEME: where to be notified if items fail to delete from Mealie shopping list
    trigger:
      - id: "regular_trigger"
        platform: state
        entity_id: sensor.mealie_shopping_list_items
      - id: "fallback_trigger"
        platform: time_pattern
        minutes: "/10"
    condition:
      - condition: not
        conditions:
          - condition: state
            entity_id: sensor.mealie_shopping_list_items
            state: "Empty List"
    action:
      - alias: "Ensure shopping list item isn't now 'Empty List'"
        condition: not
        conditions:
          - condition: state
            entity_id: sensor.mealie_shopping_list_items
            state: "Empty List"

      - alias: "Add shopping list item to target list"
        service: todo.add_item
        target:
          entity_id: "{{ target_shopping_list }}"
        data:
          item: "{{ states('sensor.mealie_shopping_list_items') }}"
          description: Imported From Mealie

      - alias: "Delete shopping list item from Mealie"
        service: rest_command.mealie_delete_from_shopping_list
        data:
          url: !secret mealie_shopping_items_api_endpoint
        response_variable: mealie_delete_response

      - alias: "Notify if item failed to delete from Mealie"
        if: "{{ mealie_delete_response['status'] != 200 }}"
        then:
          - service: "{{ notify_service }}"
            data:
              title: "Failed to delete item from Mealie list"
              message: "{{ mealie_delete_response['content'] }}"
              data:
                notification_icon: "mdi:food-off"
                
      - alias: "Refresh Mealie shopping list items sensor, in case more items need processing"
        service: homeassistant.update_entity
        data:
          entity_id:
            - sensor.mealie_shopping_list_items
3 Likes

Please consider marking paddy0174’s post with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

2 Likes

For everyone who also had trouble with the configuration:

mealie_shopping_items_api_endpoint: “https://my-mealie-domain.com/api/groups/shopping/items

mealie_api_token: “Bearer MealieAPI

A small tip for anyone looking to sync the Mealie list with the Bring! list:
delete in the automation “mealie_transfer_shopping_list” “description” under

data: 
   item: "{{ states('sensor.mealie_shopping_list_items') }}"
1 Like

Great! I was not having connection until I updated “mealie_shopping_items_api_endpoint” to Your suggestion. Many thanks!