I receive a text from my grocer when my curbside order is almost ready. It is an automated text that tells me the date and time for my pickup. For example. "Your order will be ready on 12/30 at 7:00 AM.
I have last_notification setup and working. My goal is to set setup an automation that runs a reminder of some kind (light flash, phone alert, whatever) when that time from the message rolls around.
Is there a way to set a datetime helper with the date and time pulled from the attribute of my last_notification sensor so that the execution of the automation can be set for a future time/date based on the specific time/date received in that text notification?
It will be hard to get it correct without the proper details.
I suggest you create an automation that can write the sensor information, states and attributes to a text file.
When you get it working then just disable the automation and wait until the next order day and enable it then.
That will give us all the details because we donât want this to trigger if you get a different message from someone else.
Again, I really appreciate it. The HA community is truly amazing. Not being a developer myself but still being able to utilize HA thanks to the support of people like you as well as just how far the interface and everything else has come over the last two years or so is astounding.
alias: New Automation
description: ''
mode: single
trigger:
- platform: template
value_template: >-
{{ state_attr('sensor.last_notification', 'package') ==
'com.samsung.android.messaging' and state_attr('sensor.last_notification',
'android.title') == 'Grocery Store' and
state_attr('sensor.last_notification', 'android.text')[0:21] == 'Your
order is set for' }}
condition: []
action:
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.YOUR_DATETIME
data:
datetime: "{{ now().year}}-{{ state_attr('sensor.last_notification', 'android.text') | regex_findall_index('(\d+\/\d+)') | replace('/', '-') }} {{ state_attr('sensor.last_notification', 'android.text') | regex_findall_index('at (\d+:\d+)') }}"
There is one big limitation, since there is no year mentioned in the sms I have to use what I believe and that is that itâs this year (current year, not 2021).
If you make an order that will be delivered the next year from when the sms arrives then this wonât work.
Itâs possible to template that also in to the mix, but for now lets keep it simple and test if it works.
You need to replace the last notification entity and the datetime entitiy.
As you can see it triggers on sms package, from grocery store and the first 21 characters. This could possibly false trigger if the store sends more similar sms messages.
I think you actually can test this with the set states in the developer tools.
If you paste the attributes there and set the state then the automation should kick in, I think.
There is not even a device in the automation.
Your sure you didnât accidentally do some other changes to the automation because that automation I posted saved fine on my instance with my entities.
Is the datetime both date and time or only time entity?
Itâs a date and time named input_datetime.grocery_pickup.
Clearly I am doing something wrong though. I re-copied your automation, pasted into notepad and changed the entity_id to my datetime helper, and alias to a more fitting name, and now I get
Mineâs a bit uglier than Hellisâs but here it is as a script:
alias: Set order ready
sequence:
- service: input_datetime.set_datetime
data:
datetime: '{{ order_ready }}'
target:
entity_id: input_datetime.your_datetime
mode: single
variables:
order_ready: >
{% set text = state_attr('sensor.last_notification', 'android.text') %} {% set order_date = text|
regex_findall(find='(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])',
ignorecase=True) %} {% set order_time = text|
regex_findall(find='([1-9]|1[0-2])\:(30)', ignorecase=True) %} {% set am_pm
= text| regex_findall(find='(am|pm)', ignorecase=True) %} {% if am_pm ==
"pm" %} {% set order_time = (order_time|int + 12) %} {% endif %} {% set
order = ( now().year~"-"~order_date[0][0]~"-"~order_date[0][1]~"
"~order_time[0][0]~":"~order_time[0][1]~":00") %} {{
order|as_datetime|as_local }}
Since they included âamâ in the time I took that to mean that it could also be âpmâ and would need correcting from 12-hour to 24-hour format⌠if thatâs not the case, we can get rid 3 lines.
It works for me when I replace state_attr('sensor.last_notification', 'android.text') with "Your order is set for 12/31 at 7:00-7:30am. Log in for more info.".
Is that still the value of {{state_attr('sensor.last_notification', 'android.text')}} ? If the value has changed to something without a date and time, the template will fail.
Ok, so I am not sure why, but I recreated the script (for the third? time) and I did replace
{{state_attr('sensor.last_notification', 'android.text')}} with {{state_attr('sensor.phone_last_notification', 'android.text')}} since that is the full entity_id of the sensor in question. And now it works! (mostly)
It input the time as 7:30.
Is it possible to use the 7:00? I wouldnât be late by 7:30, but the start time of the window would be preferable.
I really appreciate all you help (and everyone elseâs).