Filtering attribute data

Hey Everyone!

I am trying to create an automation that tells me if my morning train is on time when I leave my house. In order to do this I need to filter out information I get from one of the attributes of the sensor.

If I use this - ‘{{ state_attr(‘sensor.trains_trowbridge_to_bristol_temple_meads’, ‘service’)}}’

I get this:

{
“sta”: “20:49”,
“eta”: “On time”,
“std”: “20:50”,
“etd”: “On time”,
“platform”: “1”,
“operator”: “Great Western Railway”,
“operatorCode”: “GW”,
“serviceType”: “train”,
“serviceID”: “598447TRWBRDG_”,
“origin”: {
“location”: {
“locationName”: “Salisbury”,
“crs”: “SAL”
}
},
“destination”: {
“location”: {
“locationName”: “Bristol Temple Meads”,
“crs”: “BRI”
}
}
}

I want to extract the value for “eta” and “platform” so they can be sent to me via a push notification.

Is this possible?

First You should read the Doc, in regards to post Code "“code blocks” in pt. 11

Have you “Viewed” the sensor in the “States” section, there you should also see the “attributes” in it’s correct “list/dict” form

in your “template” it says that

eta=1
platform=4

When your done reading, and used 3-backticks, above and below your code, it will look alot nicer, and easier to read and interpret

Try this:

{{ state_attr(‘sensor.trains_trowbridge_to_bristol_temple_meads’, ‘service’)[1].eta}}
{{ state_attr(‘sensor.trains_trowbridge_to_bristol_temple_meads’, ‘service’)[4].platform}}

or just .eta .platform

Just FYI: You have curly quotes in those templates because the pasted data was unformatted. The template will fail with these.

ohh ok, i didn’t know those would “appear” because it was unformatted

Have you tried

with or without [1] ? , i think he should/need to do without [1] , in his case … it work in the Template-Section ( i actually think it was you who taught me my first “dict-traverse” )

… but as mention, i have never seen, or tried template from “unformmated” text , and i always use the States-Section to figure out howto address the specific value, from an attr.