it works!! one last thing Petro … is there a code to extract the numeric value inside ‘package […]’?
I don’t know what you mean. Can you elaborate?
I come from the javascript code for http pages … I would like to know if it is possible to extract the value inside the brackets [] in order to then perhaps create a for loop to read the sensor values of the 17track integration. in practice I would like to know how many my packages are in transit by the value packages [n]
Oh you want to know the length of the array…
"{{ state_attr('sensor.example', 'package') | length }}"
Oh oh… was so simple?! :-)) Thank so much Petro!!!
I swear I tried but I didn’t find a tutorial rag anywhere. What I would like to do is obtain with a loop {{for}} a ‘value template’ with the list of my parcels delivered (example: value_template: "my delivered packages are {{package1}},{{package2},{{package3}} ) , obtained from the 17track sensor sttributes, which is as follows:
{
"attribution": "Data provided by 17track.net",
"packages": [
{
"friendly_name": "package1",
"info_text": "delivered,
"status": "Delivered",
"tracking_number": "f2343112533"
},
{
"friendly_name": "package2",
"info_text": "delivered",
"status": "Delivered",
"tracking_number": "f2343112512"
},
{
"friendly_name": "package3",
"info_text": "delivered",
"status": "Delivered",
"tracking_number": "f2343112524"
}
],
"unit_of_measurement": "packages",
"friendly_name": "Seventeentrack Packages Delivered",
"icon": "mdi:package"
}
Easiest way to do that woudl be:
{{ state_attr('sensor.example', 'packages') | map(attribute='friendly_name') | list | join(', ') }}
great! instead is there a way to get only the first package from the list?
I got there alone!
{{ state_attr('sensor.example', 'packages') | map(attribute='info_text') | list | first }}
to avoid disturbing you again … do you know where I can find an online manual where I can study the codes of the home assistant templates well?
Home Assistant’s documentation provides an introduction to Jinja2 templating:
and refers the reader to the Jinja2 documentation:
https://jinja.palletsprojects.com/en/master/templates/
is there a way to concatenate the friendly name with the tracking number in the list?
example:
package1, 5736478, package2, 7747848, package3, 5433
This this in the Template Editor:
{% set ns = namespace(items = '') %}
{% for item in state_attr('sensor.example', 'packages') %}
{% set ns.items = ns.items ~ '{}: {}, '.format(item.friendly_name, item.tracking_number) %}
{% endfor %}
{{ns.items}}
I wonder if f'{item.friendly_name}: {item.tracking_number}'
works in jinja
Thank you Taras!!!
hopefully that makes it into jinja some day. A much easier way to write out strings
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?