Just to clarify, you have sensor.next_train_to_btn and it contains an attribute named next_trains which contains a JSON dict as shown in your post above?
If what I said is true, then if you paste this template into the Template Editor, it should report the time of the next train:
The information you wish to access is contained in a list which is zero-based (first item in the list is the zeroth item). To get the first item we use .next_trains[0]. To get the second one we use .next_trains[1].
So the post you’ve tagged as ‘Solution’ is not functionally different (nor somehow more correct) than what I offered you.
The sole reason I’m explaining this is because I’ve seen this happen many times before.
Someone asks how to add one and one.
I reply: 1+1=2
They reply: That didn’t work for me but 1 + 1 = 2 did so that’s the solution!
I noticed a difference in the screenshot of your sensor and mine. Yours has more attributes and next_trains is displayed as [object object],[object Object],etc. That’s because it wasn’t clear (at least to me) what the JSON data you displayed in your first actually represents. Based on your description, I interpreted it to be the value held within the next_trains attribute (because that’s the attribute you were attempting to parse).
In fact, that’s the payload your sensor receives (potentially via its json_attributes_topic) and converts into attributes. That definitely changes how one should extract data from the next_trains attribute!
I’ve adapted my test sensor accordingly, so now it appears more like yours does in the States page:
The first one uses the state_attrfunction to acquire the entity’s attribute value. If either the entity or the attribute don’t exist, the function return None.
The second one uses properties of the statesobject. As long as you have at least one sensor defined then states.sensor is valid. If you’ve defined a sensor.something then states.sensor.something is valid. Etc, etc. If you do not have a sensor.something then states.sensor.something.attributes.whatever will fail and result in an error (i.e. it doesn’t report None, it just fails).
So the preference is to use the states and state_attr functions because they are more tolerant of non-existent entities (and attributes) and will report None. I also find them easier to read than the other form.
HOWEVER, in this particular case it makes no difference which form we use! Why? Because we are appending [1].scheduled to the output of the state_attr function:
Hi. I have similar timetable sensor giving info about trams departures. I would like to process it and get a new sensor showing next tram with number 9 or 49 which departures not earlier than 15 minutes from now. I’ve managed to create sensor showing trams 9 or 49 but my code is cumbersome and doesn’t involve checking if tram will departure above 15 minutes from now. I would appreciate any help.
Hello everyone, I have a similar issue, only the problem is I cannot extract the value from "converter_factor_kwh/m3" which has a slash / in it.
Here is my problem: