Hi!
I’ve been trying to put a sensor on for hours.
I’m pretty new to this.
The goal is to display the last alarm clock that is set on two diffrent devices.
If I check it in the template tool, it works. But the sensor always shows unavailable or unknown.
If only one alarm is set on one of the phones, it returns unavailable, if both are set, it returns unknown.
What am I doing wrong?
I think you should handle the different case when sensor.galaxy_francisca_next_alarm or sensor.samuel_s_phone_next_alarm are not set.
In my opinion having this template without checking first that the sensor are not “unknown” leads to issue when trying to convert “unknown” value to timestamp.
There are a couple things that need to be addressed:
Use either quotes around the template or a block scalar indicator like >, not both.
To use the timestampdevice class the template must return a Datetime object or timestamp string like: 2024-11-05T23:00:00.000Z. Both your defined format and unknown or unavailable will cause an error.
If the purpose is for display, leave off the device class and either add an availability or modify the template so it returns a default value:
I would also suggest an availability template… There are many forms that this can take, I have for instance a custom_template in GitHub that can be loaded thru HACS that could help with this. That help could be by using it as is or copying parts of the code to do as you like with it.
Hi
Thank you very much. It works. But i think, i need the device class.
I need the result as a timestamp i think. I will use it to calculate an wake-up-light.
The sensor for the wake-up-light looks like this. (I’ts working)
That way you can easily create a trigger that will fit your need.
value_template: >
{% set t1 = as_timestamp(now()) | int %}
{% set t2 = as_timestamp(strptime(states('sensor.last_alarm_mobile'),'%Y-%m-%d %H:%M')) | int - ** your delay prior to turn on the light before the alarm in seconds ** %}
{{t1 == t2}}
trigger: template
Another idea which is closer to yours would be to compare as_timestamp(now()) | int + ** your delay in seconds** with your alarm sensor :
trigger : template
value_template: : {{ ( as_timestamp(now()) | int + ** your delay in seconds**) == (as_timestamp(states('sensor.last_alarm_mobile') | int) }}
note : make sure {{ as_timestamp(states('sensor.last_alarm_mobile') | int }} is rendering well in your dev tools “template”.
Otherwise you might try as_timestamp(strptime(states('sensor.last_alarm_mobile'),'%Y-%m-%d %H:%M') which will format your sensor state so "as_timestamp"can convert it
I’m not sure this is the best way to achieve what you’re looking for, but it should work
Pretty sure @Didgeridrew would have a more elegant way to write this down though
The reason I stated “If the purpose is for display…” in my earlier post was as an allusion to the fact that the timestamp device class designation is necessary for use in a Time trigger. Do you need to use last_alarm_mobile in a trigger? If it does, it cannot be formatted the way you showed in your original post.
This can be used with the start_wakeup you posted earlier.
If it doesn’t, there is no need for it to have that device class in order for start_wakeup to work. You just have to use the correct time functions based on your input and desired output.
Start Wakeup for non-timestamp Last Alarm Mobile
The function strptime() takes a time string and format string and returns a datetime object which is one of the acceptable types for a sensor with device class timestamp.
@nok Thank you. But i’m right, a template trigger isn’t necessary in this case, isn’t it? If one state in the sensor is changing, the sensor well be triggeret. Right?
But maybe I misunderstand you
@Didgeridrew
Perfect, the combination of these templates is working, thank you so much:
Do you need to use last_alarm_mobile in a trigger?
No, not directly. i’ll use start_wakeup as a time trigger.
Another question for both of you. Just for understanding:
The sensor start_wakeup for example, does not need an trigger, right? Because as i searched the forum (and felt like the whole internet), for a soultion, at the end, my code looked like that:
But am i right, the first part (trigger) isn’t necessary. Or is there a case where it is? Because now, during this discussion with you, i erased that part. Nothing changed…
Sorry for all this questions, but im realy new in that an try to understand.
No your sensor does not need a trigger. Without a trigger, the value of the template will be rendered every time any of the states that are being “watched” in the template change. So, the state-based example at the top of your last post will re-check it’s template whenever either sensor.last_alarm_mobile or input_number.delay_wackeuplight have a change to their states as well as at restart or reload.
In the trigger-based example you have posted at the bottom of the post, the template will be re-checked at midnight, when HA restarts, and when input_number.delay_wackeup changes state. That is probably fine… but if you change the alarm on your phone after midnight, the corresponding update to sensor.samuel_s_phone_next_alarmwill not update the trigger-based sensor because there is no trigger for sensor.samuel_s_phone_next_alarm.
For this use case there is no practical benefit to using a trigger… unless you have a habit of changing your alarm after midnight when you really shouldn’t…
No, the trigger template isn’t necessary with @Didgeridrew solution.
What I described was an idea to use the value of the sensor (last_alarm_mobile) in order to compare it to “now() - delay”. As a template-trigger this would return true exactly at the time you want to start your automation. So it could have been a way to start something some times (delay) before your alarm.
Still, I think @Didgeridrew’s solution is way more cleaner than using a template trigger so just forget it
I testet everythin, and i got a new problem
As i’ve allredy mentioned, i need all this for a wakeup light. But the light should not start if e.g. my wife is still in bed when i go up. Or vice versa.
As i tested it, there is a problem what i don’t relalized befor:
When i go up earlier, i cofirm my alarm. But then, the next alarm, who should trigger the wackeup light for my wife, isn’t anymore the alarm in sensor.start_wakeup. The next alarm is then the next alarm on my phone at the next day.
My idea was to create another sensor to just extract the timestamp, without the date.
Like this:
Well, that works. But now, the sensor start_wakeup won’t work. Probably because of the diffrent timeformat.
Is there a way to implement sensor.last_alarm_mobile_time in start_wakeup? I tried almost everything…
One solution could be, that the state of the sensor start_wakeup won’t be updatet between 01:00 AM and 09:00 AM… but then it have to, because the alarms on the phones won’t be updatet every day. Anyway, is there a way to “freeze” a state over a specific time?
Hope you unterstand my english… it’s not my native language
I’m sorry I don’t really understand the expected behavior.
At the end, you want to turn on a light based on the latest phone alarm ?? (Latest meaning the one which is scheduled later in the morning)
If so I would simply have 2 sensors to register both mine and my wife alarm’s time
Then I would create a state-based template sensor which will return the later alarm between mine or my wife alarm’s sensor.
→
state : >
{% set ent_list = ['sensor.samuel_s_phone_next_alarm',
'sensor.galaxy_francisca_next_alarm'] | select('has_value') | list %}
{{ (ent_list | map('states') | map('as_datetime') | map('as_local') | max) }}
Finally I would use this state-based template sensor as a “trigger” to start an automation in the morning at the latest alarm time.
But that in generil is not my problem. I have a template sensor wich returns the latest alarm in the morning. Thats is working after this thread.
I’ll try to explain:
Example:
My alarm is set to 05:00 on Monday and Tuesday
The alarm of my wife is set from Monday to Wednesday at 07:00
Example for Monday:
The sensor start_wakeup is set to 07:00 the next day.
I’ll go up at 05:00. Then i turn my alarm off.
Later, at 07:00, the wakeuplight for my wife should start. But the wakeuplight won’t start, because the latest alarm in start_wakeup isn’t now the one of my wife. Its mine in the next morning, Tuesday at 05:00…
Wow, thank you so much. It works perfectly.
I just had to chance the time in reject to 08:00. Becuase my wife using the phonealarm during the day for many tasks…