Recently the PostNL sensor has been changed so it gives (much) more information on deliveries. Now I’d like to extract a piece of the message and use it as text for my notification. In my case I’m talking specifically about time. (I am not used to coding, and still learning so forgive me if this automation looks weird)
I have the following attribute: "short": "{Date:2019-02-07T00:00:00+01:00} {time:2019-02-07T15:00:00+01:00} - {time:2019-02-07T17:30:00+01:00} uur"
In my automation I have put this line in: {{ trigger.to_state.attributes.shipments[0]['status']['formatted']['short'] }}
Well however this works well, the notification will contain the entire message. My current automation works well but not well enough
What I would like to do is to extract the date value as well as the time values, but seperately. So I could make an automation like below:
action:
- service: notify.ios_iphone_jimmy
data_template:
message: " There is {{ trigger.to_state.state }} package on its way for Jimmy from {{ trigger.to_state.attributes.shipments[0]['title'] }} on (DATE HERE) between (TIME1) and (TIME2)."
title: 'PostNL Jimmy'
(I translated the notification to English so it’s easier to follow)
I only need to extract parts of the time (so not the entire line) for example, I only need 2019-02-01 as a date (without the year it would even be better as it would just be 02-01) and something like 18:30 as a time (or 18:30:00 at worst).
I hope someone can help me out on this. Thanks in advance
action:
- service: notify.ios_iphone_jimmy
data_template:
message: >-
{% set s= trigger.to_state.attributes.shipments[0]['status']['formatted']['short'] %}
{% set t1 = (s|regex_findall_index('time:[^T]*T[^+-]*',0)).replace('time:','') %}
{% set d = t1.split('T')[0] %}
{% set t1 = t1.split('T')[1].rsplit(':',1)[0] %}
{% set t2 = (s|regex_findall_index('time:[^T]*T[^+-]*',1)).replace('time:','').split('T')[1].rsplit(':',1)[0] %}
There is {{ trigger.to_state.state }} package on its way for Jimmy from {{ trigger.to_state.attributes.shipments[0]['title'] }} on {{ d }} between {{ t1 }} and {{ t2 }}.
title: 'PostNL Jimmy'
Woah, that looks a whole lot different than what I expected it would look like I will try this out as soon as I get home. But yeah, looking at this makes me think that this might work indeed. I am relatively new with templating and so I am still figuring out what is possible. But man the options are endless . I will try it and report my results as soon as I have them. Thanks a lot though, even if it doesn’t work I might be able to use this elsewhere. Thank you very much.