'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…’
petro
(Petro)
January 27, 2020, 5:56pm
2
{% 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!!!
petro
(Petro)
January 28, 2020, 3:56am
4
"{{ state_attr('sensor.example', 'package')[0].tracking_number }}"
1 Like
squayor78
(marco)
January 28, 2020, 10:11am
5
it works!! one last thing Petro … is there a code to extract the numeric value inside ‘package […]’?
petro
(Petro)
January 28, 2020, 3:45pm
6
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]
petro
(Petro)
January 28, 2020, 4:23pm
8
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!!!
squayor78
(marco)
January 29, 2020, 12:36pm
10
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"
}
petro
(Petro)
January 29, 2020, 3:00pm
11
Easiest way to do that woudl be:
{{ state_attr('sensor.example', 'packages') | map(attribute='friendly_name') | list | join(', ') }}
1 Like
squayor78
(marco)
January 29, 2020, 3:45pm
12
great! instead is there a way to get only the first package from the list?
squayor78
(marco)
January 29, 2020, 3:59pm
13
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?
123
(Taras)
January 29, 2020, 4:06pm
14
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/
squayor78
(marco)
January 29, 2020, 4:36pm
15
is there a way to concatenate the friendly name with the tracking number in the list?
example:
package1, 5736478, package2, 7747848, package3, 5433
123
(Taras)
January 29, 2020, 6:01pm
16
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
petro
(Petro)
January 29, 2020, 6:12pm
17
I wonder if f'{item.friendly_name}: {item.tracking_number}'
works in jinja
123
(Taras)
January 29, 2020, 6:30pm
18
Just tried it (simplified version) and no joy:
petro
(Petro)
January 29, 2020, 7:06pm
20
hopefully that makes it into jinja some day. A much easier way to write out strings