Use sensor time in Automation

Hi Guys,

I currently have my phone set up to send the time of my next alarm to a sensor in home assistant, it looks like this:

Entity: sensor.alarmtime
State:  Thu 6:30 am

Now what I was hoping to do was take the day and time from this sensor and use it in an automation which would trigger at the alarm time, so every morning my light would come on when my alarm goes off, and the automation would automatically be updated as the sensor is updated.

    platform: time
    hour: 6 <-- (taken from sensor state, updated daily)
    minutes: 30 <-- (taken from sensor state, updated daily)
    seconds: 00
    condition: time
      - Thu <-- (taken from sensor state, updated daily)

Is this even possible? I am getting a bit lost at this stage so if anyone could point me in the right direction it would be much appreciated!

If anyone was interested this is how I got my alarm time from tasker:
[How To] Get time of next alarm using Java Functions


You can make a sensor with the current day and time.

  - platform: template
        value_template: '{{ now().strftime("%a %-I:%M %p") }}'

which returns: Wed 9:07 AM
Python’s strftime directives

and trigger with a template

  - alias: Time template trigger
      platform: template
      value_template: '{{ states.sensor.current_day_time.state == states.sensor.alarmtime.state }}'

The problem may be the uppercase of AM/PM, but if it should trigger only in the morning, you can remove it.


That is awesome, it works!

You were right, I had to remove the AM / PM for it to match but after that no problems at all.

Thanks again, I would have never thought of doing it this way =)

One thought, without AM/PM this will trigger also on 6:30 pm. :frowning:
You may use a condition to prevent this.

I was a bit cheeky and just threw “am” in the time code, probably not best practice but I only wanted it for the mornings so it will do for now =)

value_template: '{{ now().strftime("%a %-I:%M am") }}'
1 Like

For completion the following gives lowercase am/pm.

value_template: '{{ now().strftime("%a %-I:%M %P") }}'