I want to trigger an automation 15 min before the above input_datetime.
Example:
If input_datetime is 2019-11-02 15:45:00 I want the automation to trigger 2019-11-02 15:30:00.
If input_datetime is 2019-11-02 00:05:00 I want the automation to trigger 2019-11-01 23:50:00.
By the way, checking the template I see the formatting of sensor.date_time and my input_datetime is not the same: {{ states.sensor.date_time.state }} show as 2019-10-30, 13:30 {{ states.input_datetime.flemming_phone_next_alarm_date_and_time.state }} show as 2019-10-30 13:30:00
Tried to google, and I find examples on time and date separate, but not with them together, and with trigger in “past-time”
And the code that generates this error looks like?
Hint: this error means, you are comparing to different types of values, one being without a type (none) and one being an integer (int). But that’s general, that can be the reason, but without the code…
@paddy0174 Sorry for being unclear, I just copied the code above from @tom_l from oct ‘19 in the template editor.
It looks like the alarm_time definition is not compatible (anymore? It worked oct ‘19).
I know that sounds picky, but please post “your” exact code. Copy it and paste it here. The reason is rather un-exciting, the copied code may have some indentation errors or other funny things like that.
Just from the top of my head, I’d say there has something changed in the definiton of one of your times or HA is now checking the type “more exactly”. Maybe one of your sensors has changed in type or state.
I’d try to check your code in the template editor, one by one. Taking the code from @tom_l from the Oct '19 and check the outcome.
Just like flemingss did, so you could see the outcome:
Thanks for the detailed response. The problem is, you are trying to compare different things. Why this worked until now I can’t say. In your case time_now and alarm_time are timestamps, where the “900” is an integer. You can’t calculate with that.
Would you mind trying this in the template editor and see if it works for you:
{% set alarm_time = (state_attr('input_datetime.wakeup_time', 'timestamp') | int) - 900 %}
{% set time_now = as_timestamp(states('sensor.date_time').replace(',', '')) | int %}
{{ time_now >= alarm_time }}
@123 Sorry for notifying you, but could you please take a look here and see if I’m totally wrong? It seems a little off to filter and typecast all these. Thank you very much!
now().ctime() produces a string like this: Wed Aug 11 13:17:15 2021
We only want the time as hours and minutes so we use [11:16] to slice it out of the string.
On the right hand side:
We get the input_datetime’s value as a timestamp then subtract 900 seconds from it.
The result of the subtraction is converted to a time string with timestamp_custom.
You would think that it would be more elegant to compare the two times as datetime objects. The current time is easily expressed as a datetime object by now(). The challenge is to express the time-only input_datetime as a datetime object. It’s complicated by the fact it has no date and stores its time as local time and not with respect to UTC (as does an input_datetime with date and time).
The following works but is longer than comparing times as strings:
@123@paddy0174 Thanks for the detailed response, learned a lot in the process. Will try when I get at home tonight and post the result!
Is there a way/site to get more knowledge about this kind of manipulating different types of varables? The official docs are not always beginner frendly in this respect. Also the strict rules about indentations, comma’s, brackets etc is not immediately clear to a non-programmer like me.