hopkins35
(Steve Hopkins)
March 11, 2021, 9:36am
1
Hi all, would someone be able to help with outputting the index number of a search expression on a string, currently I’m getting true or false as a result but want the index number to be able to populate a sensor.
This gives me a true or false result:
{{state_attr(‘sensor.next_train_to_afk’,‘next_trains’)|regex_search(‘09:37’, ignorecase=True) }}
This is the string:
[{‘origin_name’: ‘London St Pancras International’, ‘destination_name’: ‘Dover Priory’, ‘status’: ‘STARTS HERE’, ‘scheduled’: ‘09:37’, ‘estimated’: ‘09:37’, ‘platform’: ‘11’, ‘operator_name’: ‘Southeastern’}, {‘origin_name’: ‘London St Pancras International’, ‘destination_name’: ‘Margate’, ‘status’: ‘STARTS HERE’, ‘scheduled’: ‘10:12’, ‘estimated’: ‘10:12’, ‘platform’: ‘12’, ‘operator_name’: ‘Southeastern’}, {‘origin_name’: ‘London St Pancras International’, ‘destination_name’: ‘Dover Priory’, ‘status’: ‘STARTS HERE’, ‘scheduled’: ‘10:37’, ‘estimated’: ‘10:37’, ‘platform’: ‘12’, ‘operator_name’: ‘Southeastern’}, {‘origin_name’: ‘London St Pancras International’, ‘destination_name’: ‘Margate’, ‘status’: ‘STARTS HERE’, ‘scheduled’: ‘11:12’, ‘estimated’: ‘11:12’, ‘platform’: ‘12’, ‘operator_name’: ‘Southeastern’}]
Once I have the index number I can then use it here for my sensor:
{{state_attr(‘sensor.next_train_to_afk’, ‘next_trains’)[0].estimated}}
Many thanks
Hellis81
(Hellis81)
March 11, 2021, 10:25am
2
How many subarrays are there generally? Your example has four, is that “typical”?
Hellis81
(Hellis81)
March 11, 2021, 10:46am
4
You could do a very simple template test. Not pretty but it will get the job done.
Or more specific:
Or even better:
{% set json = [{"origin_name": "London St Pancras International", "destination_name": "Dover Priory", "status": "STARTS HERE", "scheduled": "09:37", "estimated": "09:37", "platform": "11", "operator_name": "Southeastern"}, {"origin_name": "London St Pancras International", "destination_name": "Margate", "status": "STARTS HERE", "scheduled": "10:12", "estimated": "10:12", "platform": "12", "operator_name": "Southeastern"}, {"origin_name": "London St Pancras International", "destination_name": "Dover Priory", "status": "STARTS HERE", "scheduled": "10:37", "estimated": "10:37", "platform": "12", "operator_name": "Southeastern"}, {"origin_name": "London St Pancras International", "destination_name": "Margate", "status": "STARTS HERE", "scheduled": "11:12", "estimated": "11:12", "platform": "12", "operator_name": "Southeastern"}] %}
{% if json.0 | regex_search("09:37") %}0
{% elif json.1 | regex_search("09:37") %}1
{% elif json.2 | regex_search("09:37") %}2
{% elif json.3 | regex_search("09:37") %}3{%- endif %}
Or loop the array and output what I believe is what you want (?):
{% for j in json -%}
{%- if j.scheduled == "09:37" %}
{{ j.estimated }}
{{ j.platform }}
{{ j.operator_name }}
{% endif -%}
{%- endfor %}
1 Like
hopkins35
(Steve Hopkins)
March 11, 2021, 11:39am
5
Thanks for your help @Hellis81 , the array isn’t static though so I would need to reference:
{{state_attr(‘sensor.next_train_to_afk’,‘next_trains’) }}
But I can’t get the correct syntax for the set command
Hellis81
(Hellis81)
March 11, 2021, 11:49am
6
Yes… I but since I didn’t have the sensor, I had to hard code the array.
I believe you can just replace
{% for j in json -%}
With
{% for j in state_attr(‘sensor.next_train_to_afk’,‘next_trains’) -%}
1 Like
hopkins35
(Steve Hopkins)
March 11, 2021, 11:56am
7
Perfect, that works, thanks very much.
For completeness this is the code that I used:
{% for j in state_attr('sensor.next_train_to_afk','next_trains') -%}
{%- if j.scheduled == "12:12" %}
{{ j.estimated }}
{{ j.platform }}
{{ j.operator_name }}
{% endif -%}
{%- endfor %}
123
(Taras)
March 11, 2021, 1:30pm
8
The attribute value you posted appears to be a list. If that’s true, this should work:
{% set y = state_attr('sensor.next_train_to_afk','next_trains')
| selectattr('scheduled', 'eq', '09:37') | list %}
{{ y[0].estimated }}
{{ y[0].platform }}
{{ y[0].operator_name }}
1 Like