Need help in Repeat For Each with list

Hi,

I’m using the 17Track integration to tracks my parcels number, and wanted to have a notification every time it’s updated.

in the in transit sensor, there is an attribute packages which is an array of package object

packages:
  - tracking_number: ABCDE
    location: Germany
    status: In Transit
    timestamp: "2024-07-02T20:19:00+00:00"
    info_text: >-
      The international shipment has been processed in the parcel center of
      origin
    friendly_name: null

I have made this automation:

alias: Alert on Parcel
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.17track_in_transit
    attribute: packages
condition: []
action:
  - variables:
      parcels: >-
        {% set parcel = state_attr("sensor.17track_in_transit", "packages") %}
        {{parcel | list}}
  - repeat:
      for_each: "{{ parcels }}"
      sequence:
        - service: notify.mobile_app_***
          metadata: {}
          data:
            message: "{{ repeat.item.info_text }}"
            title: "{{ repeat.item.tracking_number }}"
mode: single

But when I run it, in the trace for the repeat section, I have this error : Error: Repeat 'for_each' must be a list of items.
If I go to the Changed Variables section, the parcels variable seems to be set correctly:

context:
  id: ***
  parent_id: ***
  user_id: null
parcels: >-
  [{'tracking_number': 'ABCDE', 'location': 'Germany', 'status': 'In
  Transit', 'timestamp': datetime.datetime(2024, 7, 2, 20, 19, tzinfo=<UTC>),
  'info_text': 'The international shipment has been processed in the parcel
  center of origin', 'friendly_name': None}]

In the for_each, I’ve tried multiple things:

  • {{ state_attr("sensor.17track_in_transit", "packages") | list }}
  • {% set parcel = state_attr("sensor.17track_in_transit", "packages") %} {{parcel | list}} (without the variable section)
  • {{ (states.sensor | selectattr('entity_id', 'match', 'sensor.17track_in_transit') | map(attribute='attributes.packages') | list)[0] }}

If anyone have any idea to solve it, thanks in advance.

It’s the timestamp… for_each can’t handle complex objects like datetime objects.

...
action:
  - repeat:
      for_each: |
        {% set ns = namespace(items=[]) %}
        {% for p in state_attr('sensor.17track_in_transit', 'packages') %}
            {% set ns.items = ns.items + [{'text': p.info_text, 'number': p.tracking_number}] %}
        {% endfor %}
        {{ ns.items }}
      sequence:
        - service: notify.mobile_app_***
          metadata: {}
          data:
            message: "{{ repeat.item.text }}"
            title: "{{ repeat.item.number }}"
1 Like

edit: Only works when I modify it from the Developer tools page, but not from the real update.

Thanks, what I’ve done today, is using the trigger.to_state.attributes.packages variable. In the Trace, I can see there is everything I want, and already parsed as yaml, so I think HA will be happy with that. :

this:
  entity_id: automation.alert_on_parcel
  state: 'on'
  attributes:
    id: '***'
    last_triggered: '2024-07-03T23:15:08.550747+00:00'
    mode: single
    current: 0
    friendly_name: Alert on Parcel
  last_changed: '2024-07-03T23:15:06.770981+00:00'
  last_reported: '2024-07-03T23:15:08.551847+00:00'
  last_updated: '2024-07-03T23:15:08.551847+00:00'
  context:
    id: ***
    parent_id: ***
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: sensor.17track_in_transit
  from_state:
    entity_id: sensor.17track_in_transit
    state: '1'
    attributes:
      packages:
        - tracking_number: ABCDE
          location: Germany
          status: In Transit
          timestamp: '2024-07-02T20:19:00+00:00'
          info_text: >-
            The international shipment has been processed in the parcel center
            of origin
          friendly_name: null
      unit_of_measurement: packages
      attribution: Data provided by 17track.net
      friendly_name: 17Track In transit
    last_changed: '2024-07-03T20:29:11.512917+00:00'
    last_reported: '2024-07-04T01:59:44.498071+00:00'
    last_updated: '2024-07-03T20:29:11.512917+00:00'
    context:
      id: ***
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.17track_in_transit
    state: '1'
    attributes:
      packages:
        - tracking_number: ABCDE
          location: DE
          status: In Transit
          timestamp: '2024-07-04T03:31:00+00:00'
          info_text: >-
            Votre colis est prêt à quitter le territoire étranger. Il va être
            remis au transporteur pour son acheminement vers la France.
          friendly_name: null
      unit_of_measurement: packages
      attribution: Data provided by 17track.net
      friendly_name: 17Track In transit
    last_changed: '2024-07-03T20:29:11.512917+00:00'
    last_reported: '2024-07-04T10:40:30.479225+00:00'
    last_updated: '2024-07-04T02:09:45.620499+00:00'
    context:
      id: ***
      parent_id: null
      user_id: null
  for: null
  attribute: packages
  description: state of sensor.17track_in_transit