Lost in a loop. (Call-service not working: connection lost)

Hi everyboy,

First, let me explain what i am trying to do. I m triying to automatically add products to my shopping list, by using this great Activity Manager Card.

It should work like this:

  1. Connect my shopping list (done)
  2. Install activiy manager and the lovelace card (done)
  3. Create a lovelace card and introduce some products with their frecuency, which is supposed to be the period to buy the product (done).
  4. Create a script to automatically add to my shopping list all those products which are due, and restart the period. Here is where i am lost.

Since Activity manager card creates an entity to each product like “activity_manager.CATEGORY_PRODUCT”, mi idea was to get all entities beginning with “activity_manager.CATEGORY_”, compare the due date with the actual date, and restart all the products where it matches. However, sometimes i get no results (i check it should be working with developer tools), and sometimes i get “Call-service not working: connection lost”. I presume this last error may be caused because of the loop.

I am lost here anyway. Can someone help? Find attached a version of the script.

alias: Actualizar Lista de Compra (sin resultados)
sequence:
  - service: script.turn_off
    entity_id: script.actualizar_lista_de_compra
  - variables:
      fecha_actual: "{{ now().date().isoformat() }}"
  - repeat:
      while:
        - condition: template
          value_template: "{{ entities | length > 0 }}"
      sequence:
        - variables:
            entities: >-
              {{ states | selectattr('entity_id', 'startswith',
              'activity_manager.lista_de_la_compra_') | list }}
            producto: "{{ entities[0].name }}"
            fecha: "{{ entities[0].state[:10] }}"
        - choose:
            - conditions:
                - "{{ fecha == fecha_actual }}"
              sequence:
                - service: shopping_list.add_item
                  data:
                    name: "{{ producto }}"
                - service: activity_manager.update_activity
                  data:
                    name: "{{ producto }}"
                    category: Lista de la compra
        - delay: "00:00:02"
        - variables:
            entities: "{{ entities[1:] }}"
  - service: script.turn_on
    entity_id: script.actualizar_lista_de_compra
mode: single

You can’t adjust the variable entities like that because of scope: you end up with an infinite loop because your while is looking at the original list each time. Better to use foreach on your list:

Thanks for your answer Troon.

I modified the script, and i get no error, but no result. Not sure if i aplied foreach correctly.

alias: Actualizar lista de la compra
sequence:
  - variables:
      fecha_actual: "{{ now().date().isoformat() }}"
  - repeat:
      for_each: >-
        {{ states | selectattr('entity_id', 'match',
        '^activity_manager.lista_de_la_compra_') | list }}
      sequence:
        - variables:
            producto: "{{ entities[0].name }}"
            fecha: "{{ entities[0].state[:10] }}"
        - choose:
            - conditions:
                - "{{ fecha == fecha_actual }}"
              sequence:
                - service: shopping_list.add_item
                  data:
                    name: "{{ producto }}"
                - service: activity_manager.update_activity
                  data:
                    name: "{{ producto }}"
                    category: Lista de la compra
        - delay: "00:00:02"
mode: single
icon: mdi:shopping

Replace the two entities[0] with repeat.item.

I believe you can reduce your script to this:

alias: Actualizar lista de la compra
sequence:
  - repeat:
      for_each: >-
        {{ states.activity_manager
          | selectattr('object_id', 'match', 'lista_de_la_compra_')
          | selectattr('state', 'contains', now().date().isoformat())
          | list }}
      sequence:
        - service: shopping_list.add_item
          data:
            name: "{{ repeat.item.name }}"
        - service: activity_manager.update_activity
          data:
            name: "{{ repeat.item.name }}"
            category: Lista de la compra
        - delay: "00:00:02"
mode: single
icon: mdi:shopping

Thank you so much! it really help me!

1 Like