Extract value from string

Hi all,

I am looking on how to extract a certain value from a state.attribute. I always need the time after dlt: returned to be used in a sensor template, in this case ‘19:47’

state.attribute = rt: status: null, dlm: ‘2’, dlt: ‘19:47’ , dld: ‘23.12.2017’

Many thanks for your help

are those commas always in the sensor output as a delimiter between the rt: dlm: dlt: and did: results?

Also is that time assumed to be on the current date?

This would probably be easier with regex but off the top of my head I don’t know the regex syntax in Jinja2, so this is a generic Python workaround. Substitute the value of str for your attribute value:

{% set str = "rt: status: null, dlm: ‘2’, dlt: ‘19:47’ , dld: ‘23.12.2017’" %}
{% set idx = str.find('dlt: ') - 2 %}
{{ str[-idx:-21] }}

Output: 19:47

Many thanks for your reply! I am really sorry, I was to unprecise I think.
Below the corrected state.attribute. value of “status:” can be different, could be as well ‘Ausfall’ or something else. So it’s not always the same amount of characters before dlt.
So I would need to search for “dlt: '” and then return the next 5 characters. I don’t need to do calculations with this value, so even just text format would be fine, as well the date does not matter.

status: null
dlm: ‘17’
dlt: ‘18:06’

You are making it hard for us to help by providing such limited information and providing it without formatting.

Go to the Developer Tools States tool, select you sensor entity, take a screen shot of the entity state information, and post it here…

Sorry for that, I will do better next time.

{{ state_attr('sensor.oebb_journey_1_2', 'rt')['dlt'] }}
1 Like

Works like a charm! Thanks for your help, much appreciated!