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.

  trigger:
    platform: time
    hour: 6 <-- (taken from sensor state, updated daily)
    minutes: 30 <-- (taken from sensor state, updated daily)
    seconds: 00
  condition:
    condition: time
    weekday:
      - 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

Thanks!

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

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

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

and trigger with a template

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

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

2 Likes

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") }}'