Creating a alarm clock

This should work if you want your automation to run 5 minutes before alarmtime:

'{{ ((now().strftime("%s") | int + 300) | timestamp_custom("%H:%M")) == states.sensor.alarmtime.state  }}'

Remember you can allways test your syntaxes in the DEV tool

@Bob_NL, yeah I tried that and it didn’t work. I think I’m having the same problem as @Vantskruv. When I type {{ ((now().strftime("%s") | int) | timestamp_custom("%H:%M")) }} into dev tools I get back 00:12. The time here is currently 23:12. Had a look at my timezone and it is configured correctly as Europe/Stockhom.

EDIT: My pi’s time zone was incorrect. Went into raspi-config and corrected it. Home Assistant is now displaying the right time data.

1 Like

Amazing with work here… could this get rolled into a module??

Everything works excellent but i can’t understand why it shows the .0 after the hour and minutes? how i can display only the integers?

Everything works excellent but i can’t understand why it shows the .0 after the hour and minutes? how i can display only the integers?

Could you post your sensor syntax? The int() filter can get you to integers. Here’s what I use for my template sensor:

  - platform: template
    sensors:
      alarm_clock_hour:
        value_template: '{{ states.input_slider.alarm_clock_hour.state | int }}'
      alarm_clock_minute:
        value_template: '{{ states.input_slider.alarm_clock_minute.state | int }}'

I use the following sensor to display the current set alarm time:

  - platform: template
      alarmtime:
        friendly_name: Wakeup Time
        entity_id:
         - input_slider.alarm_hour
         - input_slider.alarm_minutes
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_slider.alarm_hour") | int, states("input_slider.alarm_minutes") | int) }}'

And the following sensor to calculate the time until my next alarm:

  - platform: template
      nextalarm:
        friendly_name: Seconds untill next alarm
        entity_id:
         - input_slider.alarm_hour
         - input_slider.alarm_minutes
        value_template: >
          {% set relative_time =  (states.input_slider.alarm_hour.state|float|multiply(60) + states.input_slider.alarm_minutes.state|float) - (now().hour|float|multiply(60) + now().minute) %}
          {%- if relative_time < 0 -%}
                   {{24*60+relative_time}}
          {%- else -%}
                   {{ relative_time-60}}
          {%- endif %}
2 Likes

Exactly what I was looking for, adding " | int " solved the issue…

@ronmar on vídeo you shared… your mobile phone rings alarm 17:25, but how you connect it to HA?
other words, how your mobile phone sends information to HA? i saw all your code, and didn´t see nothing about that.

For some reason, my automation doesn’t work automatically on time, i does work when i set the time with slider on current time, but not if i set to the future time. Here’s my automation code, what’s wrong?

  - alias: Wake Me Up
    trigger:
      platform: template
      value_template: '{{ ((now().strftime("%-H") | int) == (states.input_slider.alarmhour.state | int)) and ((now().strftime("%-M") | int) == (states.input_slider.alarmminutes.state | int)) }}'
    condition:
      condition: or
      conditions:
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'on'
            - condition: time
              weekday:
                - sun
                - mon
                - tue
                - wed
                - thu
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'off'
    action:
      - service: script.turn_on
        data:
          entity_id: script.alarmclock

@radinsky Try this:

- alias: 'Wakeup Light with Radio'
  trigger:
    platform: time
    minutes: '/5'
    seconds: '0'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ ((now().strftime("%-H") | int) == (states.input_slider.alarmhour.state | int)) and ((now().strftime("%-M") | int) == (states.input_slider.alarmminutes.state | int)) }}'

This checks if your condition is met every 5 minutes and will fire the automation once it does. I think this is neccessary to refresh the data_template values.

Thanks a lot for the idea, but I figured much easier solution by using time sensor:

  - alias: Wake Me Up
    trigger:
      platform: template
      value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'
    condition: ......
3 Likes

Thank you. Finally i managed to set the alarm. After so many posts, I had tried several methods and I was always failing something. I got the explanations you put on your website and video and it works. :sweat_smile:

I actually didn’t connect my phone alarm to HA. I set my phone alarm and the Home Assistant alarm separately to the same time.

That being said, there’s probably a way to connect the two. Bob_NL posted above about how to change the alarm through MQTT, sent through Tasker. There might be a way to set the phone alarm, and have Tasker send the data to Home Assistant. I haven’t had much time to experiment with Tasker in my config yet.

Sorry for taking a while to respond!

Hi @hokagegano.
Can I suggest to put in the first post a valid configuration for people who see this for the first time?
I think this because it was difficult for me to create this project. Since the first post placed on April 16, the application had several updates and copying the data from the post, it did not work. I had to read every post to find a way to do it. Thank you.

2 Likes

HI @Paulo106

Good idea, i’ll do it this afternoon.

I can’t modify my root post :frowning: So i’ll create a new post with a correct basic config.

Ok. Thanks for your attention. I thought you could edit the first post because I test and edit some of my posts.

What does your Tasker task look like to trigger the input updates? Are you using HTTP Post or is there some add-on for MQTT?

UPDATE: I didn’t realize that you could trigger an MQTT automation through a POST request. I’m now having a hard time getting Tasker to send the request when I set an alarm on my phone. I can have it send a request when I turn off my alarm, but I want to have it send as soon as I set an alarm. Has anyone been able to do this?

@astone I use a combination of MQTT and HTTP post commands in my task. The MQTT I use to change the slider values, after that I use a HTTP post command to trigger the automation.

I don’t think this can be done.
I have a wireless charger next to my bed, I made a Tasker task which reads my alarmtime and sends this to Hass per MQTT and triggers the automation as soon as I put my phone on that charger. So when i’m ready for bed I check if my alarmtime is set correctly on my phone and then simply put it on the charger :).
You can read out the alarmtime with the AutoAlarm app.

I just finished implementing this morning, so I’ll post in case anyone else stumbles upon this in the future. On Tasker, I was able to trigger a task with a profile based on the android.app.action.NEXT_ALARM_CLOCK_CHANGED intent that is intercepted by Tasker every time an alarm is set on my Android phone.

On Tasker, you can set up this profile through Event > System > Intent Received and then in the Action field, fill in the intent that you want to intercept (in this case android.app.action.NEXT_ALARM_CLOCK_CHANGED).

Then, in my task, I use AutoAlarm to get the hour and minute of my next alarm, and then I send two HTTP Post requests to my HA server, one to update the hour and one to update the minute of my alarm. I update these with the MQTT automation that @Bob_NL posted above.

So, every time I set an alarm or use my Google Assistant to set an alarm on my phone (I have a Google Pixel phone), my HA alarm will be updated to that time within a couple of seconds.

The only thing that I’d like to do in the future is have some way to switch the alarm boolean off when I don’t have an alarm set, but this setup is fine for now. Thanks for the help @Bob_NL!

3 Likes