'nested' templates

'I make an example…

sensor template 1 = {{states.sensor.example.attributes.tracking_number}} = ftcdubvu
sensor template 2 = {{ states.sensor.example2_ftcdubvu.attributes.location }}

how can i nest the two templates?
'{{ states.sensor.example2_ {{states.sensor.example.attributes.tracking_number}} .attributes.location }}

I hope I was clear…’

{% set name = state_attr('sensor.example', 'tracking_number') %}
{% set name2 = 'sensor.example2_' ~ name %}
{{ state_attr(name2, 'location') }}

or in 2 separate templates

template1, entity: sensor.template1

value_template: "{{ state_attr('sensor.example', 'tracking_number') }}"

template2

value_template: "{{ state_attr('sensor.example2_' ~ state('sensor.template1'), 'location') }}"
1 Like

little problem… my sensor template 1 is
{{states.sensor.example.attributes.package[0].tracking_number}}

how is the correct code for this?
{% set name = state_attr(‘sensor.example’, ‘package[0].tracking_number’) %}
ERROR!!!
{% set name = state_attr(‘sensor.example.package[0]’, ‘tracking_number’) %}
ERROR!!!

"{{ state_attr('sensor.example', 'package')[0].tracking_number }}"
1 Like

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 }}"
1 Like

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(', ') }}
1 Like

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}}
1 Like

I wonder if f'{item.friendly_name}: {item.tracking_number}' works in jinja

Just tried it (simplified version) and no joy:

Thank you Taras!!!

hopefully that makes it into jinja some day. A much easier way to write out strings