As I’m not good with templating I’ve exhausted all my abilities with many hours trying various things in the template dev tool and reading previous posts etc.
I have a sensor from the Android companion app which provides my next alarm and I’d like to use that along with an offset (which I’d like to grab from an input datetime) to use as a trigger for an automation. I want to trigger an automation to gradually turn a light on based on subtracting the offset from the alarm time.
Sensor to use from the companion app: sensor.dave_s_s22_next_alarm currently has this state: 2024-07-31T21:05:00+00:00
I’ve created input_datetime.daves_next_alarm_offset which is currently set at 00:15:00
If someone can please help me create the template to subtract the input datetime offset from the alarm time and use that as a trigger, that would be great.
A totally different approach for a similar outcome is with the Sleep as Android app. I’ve got it sending events via MQTT and it has events for “before alarm” which is 60 minutes early I think, as well as delays for the sound alarm.
My bed heater kicks on way early to get me out of deep sleep, then the lights fade in and a bit later the alarm sounds.
Using the template is definitely more flexible, but a similar outcome.
I do actually use this app which is where the alarm time is coming from, via the companion app. I’ll look into this ‘before alarm’ entity. It currently doesn’t exist in my HA but I’m guessing it must need to be configured in the app first.
Awesome, thanks for this. I’ll give it a test run and report back.
On further investigation, this is enabled in the app so I guess I just need to listen for the MQTT event create a trigger based on the sensor changing to this state. It’s a shame the docs are almost non-existent.
I think I’ll still with the template option given I will have more flexibility.
Yeah, I ended up figuring out how to make the MQTT events work by following some thread about a totally different Automation platform. I tinkered and it finally worked. Then I later found there’s a HACS integration with the events pre-configured. I haven’t messed with that as I’d already got things working by that point. Here’s the trigger I use based on that thread I’d found linked from the SAA docs.
Before going to sleep last night I checked that the ‘next alarm’ entity had the correct state for my morning alarm and all looked good, but this morning nothing happened and the last automation trace is still back from days ago.
When I loo in the history for that sensor, there is nothing because I’m guessing I don’t have it included in the history settings. I didn’t think that would cause this issue…?
It’s a fairly straightforward template (compare two time strings) so I am puzzled that it occasionally fails to work.
The template updates every minute due to its use of now().
Grasping at straws here; is there a pattern to the trigger failures (like every other day)?
When do you change the value of sensor.dave_s_s22_next_alarm? The day/evening before the next morning? How often do you change it (like every day, or every other day)?
It’s important that you set the Template Sensor’s device_class to timestamp.
Change the automation’s trigger from a Template Trigger to a Time Trigger.
alias: example
trigger:
- platform: time
at: sensor.dave_wakeup
condition: []
action:
... your actions ...
Now that the wakeup time is calculated by a Template Sensor, it makes it easy to see the resulting time in advance.
NOTE
Pay attention to what is shown on the bottom of the form where it say “This template listens for the following state changed events”.
In my case, it only listed the sensor and not the input_datetime. Perhaps it’s because my system doesn’t contain either of the two entities.
If it says the same thing on your system then that may present a problem. If it’s not monitoring the input_datetime then the template will not be evaluated whenever the input_datetime’s value changes.