'nested' templates

once again I have to give up …
How do I go about obtaining the locations from the individual sensor packages (sensor.seventeentrack_package_xxxxxxxx) and then inserting them in the payload?

state_attr (‘sensor.seventeentrack_package_’ ~ state_attr (‘sensor.seventeentrack_packages_in_transit’, ‘packages’) [0,1,2,3,4 …???]. tracking_number, ‘location’)

      payload_template: >
        {% set ns = namespace(items = 'update on the packages you are waiting for: ') %}
        {% for item in state_attr('sensor.seventeentrack_packages_in_transit', 'packages') %}
        {% set ns.items = ns.items ~ 'the package with {}: is  in {} at ######{LOCATION}#######, '.format(item.friendly_name, item.info_text, ???????) %}
        {% endfor %}
        {{ns.items}}		

I know next to nothing about the seventeentrack integration and my answer was based solely on the sample data you provided in your post. You’ll have to help me understand where the location data exists because I don’t see it in the sample data.


sensor.seventeentrack_package_e109e19w7c4

{
  "attribution": "Data provided by 17track.net",
  "destination_country": "England",
  "info_text": "in transit",
  "location": "berlin",
  "origin_country": "spain",
  "package_type": "Registered Parcel",
  "tracking_info_language": "Unknown",
  "tracking_number": "e109e19w7c4",
  "friendly_name": "my package",
  "icon": "mdi:package"
}

I have another question … you can enter later
all in the value_template of a new sensor template? I tried but it doesn’t work …

- platform: template
  sensors:
    intransit:
      entity_id: sensor.seventeentrack_packages_in_transit
      friendly_name: 'in transit'
      value_template: >-
        {% set ns = namespace(items = 'packages: ') %}
        {% for item in state_attr('sensor.seventeentrack_packages_in_transit', 'packages') %}
        {% set ns.items = ns.items ~ '{}: is in  {}. '.format(item.friendly_name, item.info_text) %}
        {% endfor %}
        {{ns.items}}

Sorry, I’m having difficulty understanding how your sensors and data are organized. You’ve shown the data for a sensor called:

sensor.seventeentrack_package_e109e19w7c4

but then in the Template Sensor sensor.intransit it refers to a packages attribute in a different sensor:

sensor.seventeentrack_packages_in_transit

What’s that sensor’s data look like?

The 17track sensors that I would like to edit are:
the main one, sensor.seventeentrack_packages_in_transit, which contains the attribute packages, with the attributes of the various packages in transit inside

{
  "attribution": "Data provided by 17track.net",
  "packages": [
    {
      "friendly_name": "package1",
      "info_text": "info1",
      "status": "In Transit",
      "tracking_number": "xxxxxxxx"
    },
    {
      "friendly_name": "package2",
      "info_text": "info2",
      "status": "In Transit",
      "tracking_number": "yyyyyyyy"
    },
    {
      "friendly_name": "info3",
      "info_text": "info3",
      "status": "In Transit",
      "tracking_number": "kkkkkkkk"
    }
   ],
  "unit_of_measurement": "packages",
  "friendly_name": "Seventeentrack Packages In Transit",
  "icon": "mdi:package"
}

and the sensors of the individual packages,(sensor.seventeentrack_package_xxxxxxxx) from which I should derive the attibuto location that is missing from the main sensor

{
  "attribution": "Data provided by 17track.net",
  "destination_country": "Unknown",
  "info_text": "info1",
  "location": "berlin",
  "origin_country": "england",
  "package_type": "Registered Parcel",
  "tracking_info_language": "Unknown",
  "tracking_number": "xxxxxxxx",
  "friendly_name": "name package1",
  "icon": "mdi:package"
}

Try this in the Template Editor (I don’t have the means to test it with your data):

{% set ns = namespace(items = '') %}
{% for item in state_attr('sensor.seventeentrack_packages_in_transit', 'packages') %}
  {% set sensor = 'sensor.seventeentrack_package_' ~ item.tracking_number %}
  {% set ns.items = ns.items ~ 'Name: {} Number: {} Location: {}, '.format(item.friendly_name, item.tracking_number, state_attr(sensor, 'location')) %}
{% endfor %}
{{ns.items}}

If it works you can easily modify the template to display the result the way you prefer. Should you also require it to display package_type (or other attribute) you can append state_attr(sensor, 'package_type') to the template’s format function.

all right! … and to insert all the result in the value template of a new sensor? it can be done?

You could do that. The Template sensor’s value_template would be the template I posted above. HOWEVER, an entity’s state is limited to storing 255 characters maximum. Given enough tracked packages, it’s possible that the template’s result can exceed 255 characters.

FWIW, an entity’s attributes are not limited to storing only 255 characters.

as you may have guessed they are right at the beginning … so the code to insert that value template in an entity attribute of a sensor would be there? what I would like to do in practice is to transform the tts message into a sign with the position of the packages on it

I got there alone

- platform: template
  sensors:
    intransitpack:
      entity_id: sensor.seventeentrack_packages_in_transit
      friendly_name: 'in transit'
      value_template: ''
      attribute_templates:
        intransit: >-
          {% set ns = namespace(items = '') %}
          {% for item in state_attr('sensor.seventeentrack_packages_in_transit', 'packages') %}
          {% set sensor = 'sensor.seventeentrack_package_' ~ item.tracking_number %}
          {% set ns.items = ns.items ~ 'Name: {} Number: {} Location: {}, '.format(item.friendly_name, item.tracking_number, state_attr(sensor, 'location')) %}
          {% endfor %}
          {{ns.items}}

Hi! I was looking for such solution to list all my 17Track.net packages in one card. Mi question is how can I list all items in a card, each on a separate line? My template looks like this:

- platform: template
  sensors:
    intransitpack:
      entity_id: sensor.seventeentrack_packages_in_transit
      friendly_name: 'in transit'
      value_template: ''
      attribute_templates:
        intransit: >-
          {% set ns = namespace(items = '') %}
          {% for item in state_attr('sensor.seventeentrack_packages_in_transit', 'packages') %}
          {% set sensor = 'sensor.seventeentrack_package_' ~ item.tracking_number %}
          {% set ns.items = ns.items ~ 'Name: {} Info: {}, '.format(item.friendly_name, item.info_text) %}
          {% endfor %}
          {{ns.items}}

change your last line to

{{ ns.items | join('\n') }}

EDIT: Nevermind, you need to update teh whole template to this

          {% set ns = namespace(items = []) %}
          {% for item in state_attr('sensor.seventeentrack_packages_in_transit', 'packages') %}
          {% set sensor = 'sensor.seventeentrack_package_' ~ item.tracking_number %}
          {% set ns.items = ns.items + [ 'Name: {} Info: {}'.format(item.friendly_name, item.info_text) ] %}
          {% endfor %}
          {{ ns.items | join('\n') }}

Hi Petro! Thank you for your sggestion. i tried your code and I get the following error:

TypeError: can only concatenate list (not “str”) to list

What is causing this?

Yes, because you need to use the whole template I provided. Not the original post saying “change your last line to”.

You are right. It works now. Thank you.
Is there a way to add an incrementing number to the beginning of each line?

          {% set ns = namespace(items = [], count=0) %}
          {% for item in state_attr('sensor.seventeentrack_packages_in_transit', 'packages') %}
          {% set sensor = 'sensor.seventeentrack_package_' ~ item.tracking_number %}
          {% set ns.count = ns.count + 1 %}
          {% set ns.items = ns.items + [ '{} Name: {} Info: {}'.format(ns.count, item.friendly_name, item.info_text) ] %}
          {% endfor %}
          {{ ns.items | join('\n') }}

Thank you, it works. One last question how can I add this sensor to a Lovelace card which will display all results as seen in the template editor?

You’d need to use the markdown card

Thank you ver much your support.

1 Like

@petro Hi Petro! My markdown card looks really nice, but it shows only the “in transit” packages. Is there a way have listed also the “not found” packages? I would like to see all my packages from 17tracknet listed in the card, regardless of their “in transit” or “not found” status.