Struggling with state_attr as list

I’m using an the 17track integration to track packages (for some automation I’m planning).
I’m seeing some really strange behavior and I can’t seem to resolve, the simplest way to demonstrate my issue is with this one automation step that defines some variables:

variables:
  packages: "{{ state_attr('sensor.seventeentrack_in_transit','packages') }}"
  p_test1: "{{(packages)[0]}}"
  p_test2: "{{ state_attr('sensor.seventeentrack_in_transit','packages')[0] }}"

In the “trace” for the automation, this is what I’m seeing:

packages: >-
  [{'tracking_number': 'PH8000218XXX', 'location': '', 'status': 'In Transit',
  'timestamp': datetime.datetime(2025, 2, 8, 19, 59, tzinfo=<UTC>), 'info_text':
  'Arrived at linehaul office, Carrier note: Arrived at linehual office',
  'friendly_name': "LED Project + RJ45"}, {'tracking_number':
  'PH800023XXX', 'location': '', 'status': 'In Transit', 'timestamp':
  datetime.datetime(2025, 2, 8, 7, 21, tzinfo=<UTC>), 'info_text': 'Arrived at
  departure transport hub, Carrier note: Arrived at departure transport hub',
  'friendly_name': 'Adapter for RJ45'}, {'tracking_number': 'PH80002XXXX',
  'location': '', 'status': 'In Transit', 'timestamp': datetime.datetime(2025,
  2, 8, 2, 35, tzinfo=<UTC>), 'info_text': 'Arrived at departure transport hub,
  Carrier note: Arrived at departure transport hub', 'friendly_name': 'dddd'}]
p_test1: '['
p_test2: >-
  {'tracking_number': 'PH800021XXXX', 'location': '', 'status': 'In Transit',
  'timestamp': datetime.datetime(2025, 2, 8, 19, 59, tzinfo=<UTC>), 'info_text':
  'Arrived at linehaul office, Carrier note: Arrived at linehual office',
  'friendly_name': "LED Project + RJ45"}

My real issue here is that somewhere later in this automation I want to do a “repeat/for_each” on “packages” and it won’t let me, saying packages is not an array. So I don’t understand how to turn it into one, and if it’s not, how come I’m able to use indices to get individual members here, which seems to work directly in the line where the variable is defined, but not later.

Something doesn’t make sense… Any help will be greatly appreciated.

Thanks.

Your problem is that packages contain values that won’t automatically convert to a string, namely the datetime objects filed under the timestamp key for each of your packages. Because of this everything gets converted into a single string.

Do you need the timestamp? If not, the easiest solution would be to simply discard it. If you do need it, you’d need to single out only the timestamp, convert it into a string or number, then smush 'em back together. Doable but a bit messy as I think it would require a loop rather than just filters.

Something like this should be enough to discard the timestamps:

packages: >-
  {{ dict(state_attr('sensor.seventeentrack_in_transit',
  'packages') | items | rejectattr(0, '==', 'timestamp') | list) }}

(Completely untested, caveat emptor, etc.)

Something like this should work for keeping the timestamp, which gets converted to a string. If instead you want a real timestamp (number), replace string with as_timestamp.

packages: >-
  {% set repeat = namespace(packages=[]) %}
  {% for package in state_attr('sensor.seventeentrack_in_transit', 'packages') %}
    {% set repeat.packages = repeat.packages + dict(package | items
    + [('timestamp', package.timestamp | string)]) %}
  {% endfor %}
  {{ repeat.packages }}

Both options did not work for me, I ended up doing this to remove the elements I didn’t need:

  {% set ns = namespace(new_packages=[]) %}
  {% for pkg in state_attr('sensor.seventeentrack_in_transit','packages') %}
    {% set filtered_pkg = {
      "tracking_number": pkg.tracking_number,
      "status": pkg.status,
      "info_text": pkg.info_text,
      "friendly_name": pkg.friendly_name
    } %}
    {% set ns.new_packages = ns.new_packages + [filtered_pkg] %}
  {% endfor %}
  {{ ns.new_packages }}

I’m sure there was a simpler way, but this works…