Can I set an input_datetime to a time/day pulled from the text of a last_notification sensor?

That is what I believe also.
There is no reason why it would fail, unless not all parts of the attributes is correct.

I completely forgot about the am/pm thing. When you never use it, it just doesn’t occur to one.
Nice catch.

Actually… I can’t get this to get the date correct.

It only captures the first character of “31”, and when I test it on regex101 it produces the same result.
But set for (\d+)/(\d+) is more than sufficient to get an automated date.
I also noticed the | as_local puts timezone data at the end of the string. Does that work with input_datetime?

I think the most complete way to do this will be with

{% set text = state_attr('sensor.phone_last_notification', 'android.text') %}
{% set order_date = text| regex_findall(find='set for (\d+)\/(\d+)', ignorecase=True) %}
{% set order_time = text| regex_findall(find='(0?[1-9]|1[0-2]):([0-5][0-9])', ignorecase=True) %}
{% set am_pm = text| regex_findall(find='(am|pm)', ignorecase=True) %}
{% set order_hour = order_time[0][0] | int %}

{% if am_pm|join() == 'pm'%}
  {% if order_hour < 12 %}
      {% set order_hour = order_hour + 12 %} 
  {% endif %}
{% endif %}

{% set order_hour = "0"~order_hour %} # simplified, we just add a 0 before the time then at output we only read the two last digits with [-2:].

{% if order_date[0][0] | int == 1 and now().month == 12 %}
   {% set year = now().year +1 %}
{%else%}
   {% set year = now().year %}
{% endif %}

{% set order = ( year~"-"~order_date[0][0]~"-"~order_date[0][1]~" "~order_hour[-2:]~":"~order_time[0][1]~":00") %}
{{ order|as_datetime|as_local }}  # not sure about the as_local though

This takes the date correctly and adds a year if the date in the sms is in January but current month is December.

1 Like

Y’all are wonderful. I did try this last one and I set the time to 11:00pm, but the datetime helper stayed am with this last one. The one from didgeridrew (love that name btw) did handle am/pm. As for year, I don’t know if edge cases where I order one year and pick up the next is worth mulling over any more than you already have.

It works here…

Perhaps the automation didn’t react because the state wasn’t changed?

Ok, I ran it in the template on my end too and I see what the issue was. The commented out portions of the code were causing the output to be just that, the comments. So, removed those sections and now am/pm works. Thank you so much.

1 Like

At this point, it’s probably overkill… but what about just using < instead of using the and in the year variable? :

{% if order_date[0][0] | int < now().month %}
  {% set year = now().year +1 %}
{%else%}
   {% set year = now().year %}
{% endif %}

1 Like

True. But that means the sms would have to be sent in November with a delivery in January.
Highly u likely :slight_smile: but you are definitely correct!