Creating a alarm clock

I could still use the slider input and the time trigger, I swear I read somewhere time has both an after and before?
If so I could take the after and just use the “alarm” time, I could use the slider variables to take alarm time and add the duration time to give me the before. Assuming I can use before and after in that manner.

The question is: would it be possible to use the values input by the user through the input slider to feed the time trigger?

should be able to, I mean you are using those to input the time for the alarm.
Here is my snippet for the timer function, im currently testing it. I have absolutely NO idea if the syntax for the hour and minutes in the automation are correct!

##POOL TIMER ##
input_slider: 
  alarmhour:
    name: Hour
    icon: mdi:timer
    initial: 9
    min: 0
    max: 23
    step: 1
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 0
    min: 0
    max: 55
    step: 5
  alarmdurationhour:
    name: Duration Hour
    icon: mdi:watch-export
    initial: 9
    min: 0
    max: 23
    step: 1
  alarmdurationminutes:
    name: Duration Minutes
    icon: mdi:watch-export
    initial: 0
    min: 0
    max: 55
    step: 5


input_boolean: 
  alarmstatus:
    name: Wake Me Up
    initial: off
    icon: mdi:alarm-check
  alarmweekday:
    name: Weekdays Only
    initial: off
    icon: mdi:calendar

sensor: 
  platform: template
  sensors:
    alarm_time:
      friendly_name: 'Time'
      value_template: '{{ states.input_slider.alarmhour.state }}:{% if states.input_slider.alarmminutes.state|length == 1 %}0{% endif %}{{ states.input_slider.alarmminutes.state }}'
    duration_time:
      friendly_name: 'Duration'
      value_template: '{{ states.input_slider.alarmdurationhour.state }}:{% if states.input_slider.alarmdurationminutes.state|length == 1 %}0{% endif %}{{ states.input_slider.alarmdurationminutes.state }}'


group:
  default_view:
    entities:
      - group.alarmclock
      - group.equipment
  alarmclock:
    name: Pool Pump Schedule
    entities: 
      - sensor.alarm_time
      - input_slider.alarmhour
      - input_slider.alarmminutes
      - sensor.duration_time
      - input_slider.alarmdurationhour
      - input_slider.alarmdurationminutes
      - input_boolean.alarmstatus
      - input_boolean.alarmweekday

  equipment:
    name: Equipment
    entities: 
      - switch.bedroom_switch
      - switch.pool_lights
      - switch.pool_pump
      - switch.waterfall

automation:
  - alias: 'Pool Pump On'
    trigger:
      platform: time
      after: '{{ now.time().strftime("%-H") == states.input_slider.alarmhour.state and now.time().strftime("%-M") == states.input_slider.alarmminutes.state }}'
      hour: '{{ now.time().strftime("%-H") == states.input_slider.alarmdurationhour.state }}'
      minutes: '{{ now.time().strftime("%-M") == states.input_slider.alarmdurationminutes.state }}'
    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:
                - mon
                - tue
                - wed
                - thu
                - fri
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'off'
    action:
      service: switch.turn_on
      entity_id: switch.pool_pump

According to the time trigger documentation

You cannot use after together with hour, minute or second.

I dont believe this is possible… I thought it might be… I really hope someone can prove me wrong though!

So I am not sure why, I cannot get this to work anymore. I KNOW that the sliders are correct, they are showing the correct time for the entity alarm_time. I know that the notify function is working, if I change it to the time it currently is (rendering it true) then the notify fires off. I cannot get it to trigger at a preset time though!

Just in case anyone is still reading this the problem for hours before noon was a padded zero. I added it with this:

‘{% if states.input_slider.alarmhour.state|length == 1 %}0{% endif %}{{ states.input_slider.alarmhour.state }}:{% if states.input_slider.alarmminutes.state|length == 1 %}0{% endif %}{{ states.input_slider.alarmminutes.state }}’

Hope that helps someone!

Edit:

Worked perfectly!

For anyone that is using the Windows version of Python 3.5 this code will work for the trigger…

    value_template: '{{ now.time().strftime("%R") == states.sensor.alarm_time.state }}'        

The original code give an error as %-H and %-M are not valid :stuck_out_tongue:

how can i make
trigger:
platform: time
after: “put here the template from input slider”?
i want the user to choose the time to activate my light

I looked at this and decided that this was close to what I wanted, but not at the automation level I wanted. I ended using an Android alarm that can do Tasker/ifttt events so I can change my alarms on the fly on my phone and not have to change anything in HASS.

I later added my device location so it doesn’t trigger when I’m not there, but this seemed much more intuitive and fluid than this solution.

Here is what I am using to compensate for “Input slider: Support for float value” addition in 0.25

        {%- if states('input_slider.time_hour') | length == 3 -%}
        0{%- endif %}{{ states('input_slider.time_hour') | int }}:
        {%- if states('input_slider.time_minute') | length == 3 -%}
        0{%- endif %}{{ states('input_slider.time_minute') | int }}

Hi all.
I managed to show the correct value in the tab is the following code:

value_template: '{% if states.input_slider.alarmhour.state| length < 4 %}0{{ states.input_slider.alarmhour.state | int }}{% endif %}{% if states.input_slider.alarmhour.state| length > 3 %}{{ states.input_slider.alarmhour.state | int }}{% endif %}:{% if states.input_slider.alarmminutes.state | length < 4 %}0{{ states.input_slider.alarmminutes.state | int }}{% endif %}{% if states.input_slider.alarmminutes.state | length > 3 %}{{ states.input_slider.alarmminutes.state | int }}{% endif %}'

I tried the code you used but the time I get is 8:0:30.0 any ideas?

I guess I’m having a hard time figuring out what to update on sensor and automation section. Thank you guys!

To fix this, I have created 2 new sensors; sensor.alarm_hour and sensor.alarm_minutes. These will take the value from the sliders and Round it to 0.

Then update the sensor.alarm_time and the automation to use the 2 new sensors.

Here is the complete code.

input_slider: 
  alarmhour:
    name: Hour
    icon: mdi:timer
    initial: 9
    min: 0
    max: 23
    step: 1
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 0
    min: 0
    max: 55
    step: 5

input_boolean: 
  alarmstatus:
    name: Wake Me Up
    initial: off
    icon: mdi:alarm-check
  alarmweekday:
    name: Weekdays Only
    initial: off
    icon: mdi:calendar

sensor: 
  platform: template
  sensors:
    alarm_hour:
      friendly_name: 'Hour'
      value_template: '{{ states("input_slider.alarmhour") | round(0) }}'
    alarm_minutes:
      friendly_name: 'Minutes'
      value_template: '{{ states("input_slider.alarmminutes") | round(0) }}'
    alarm_time:
      friendly_name: 'Time'
      value_template: '{{ states("sensor.alarm_hour") }}:{% if states("sensor.alarm_minutes")|length == 1 %}0{% endif %}{{ states("sensor.alarm_minutes") }}'

group:
  default_view:
    view: yes
    entities:
      - group.alarmclock
  alarmclock:
    name: Wake Me Up
    entities: 
      - sensor.alarm_time
      - input_slider.alarmhour
      - input_slider.alarmminutes
      - input_boolean.alarmstatus
      - input_boolean.alarmweekday

automation
  - alias: 'Wake Me Up'
    trigger:
      platform: template
      value_template: '{{ now.time().strftime("%-H") == states.sensor.alarm_hour.state and now.time().strftime("%-M") == states.sensor.alarm_minutes.state }}'
    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:
                - mon
                - tue
                - wed
                - thu
                - fri
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'off'
    action:
      service: notify.notify
      data:
        message: 'Good morning. Time to Wake Up!'
        title: ""
3 Likes

I’ve fixed the “.0” problem by using round():

value_template: ‘{{ states.input_slider.alarmhour.state | round(0) }}:{% if states.input_slider.alarmminutes.state|length == 1 %}0{% endif %}{{ states.input_slider.alarmminutes.state | round(0) }}’

1 Like

I think this is a great example that deserves more publicity.
So it would be great if someone would add it here:

1 Like

Hi, I just found this discussion. I have a problem with time triggered automation

Now I’ve edit new configuration, just for testing input_slider and time trigger :

homeassistant:
  # Name of the location where Home Assistant is running
  name: E.R.S.A
  latitude: -6.1744
  longitude: 106.8294
  elevation: 2
  # C for Celcius, F for Fahrenheit
  unit_system: imperial
  
  time_zone: Asia/Jakarta

# Show links to resources in log and frontend
# introduction:

# View all events in a logbook
logbook:

# Track the sun
#sun:

# Enables support for tracking state changes over time.
history:

# Checks for available updates
updater:

# Allows you to issue voice commands from the frontend
conversation:

# Enables the frontend
frontend:

# Discover some devices automatically
# discovery:

http:
  api_password: admin

mqtt:
  broker: 127.0.0.1
  port: 1883
  client_id: home-assistant-1
  keepalive: 60

sensor: 
  platform: template
  sensors:
    alarm_time:
      friendly_name: 'Time'
      value_template: '{% if states.input_slider.alarmhour.state| length < 4 %}0{{ states.input_slider.alarmhour.state | int }}{% endif %}{% if states.input_slider.alarmhour.state| length > 3 %}{{ states.input_slider.alarmhour.state | int }}{% endif %}:{% if states.input_slider.alarmminutes.state | length < 4 %}0{{ states.input_slider.alarmminutes.state | int }}{% endif %}{% if states.input_slider.alarmminutes.state | length > 3 %}{{ states.input_slider.alarmminutes.state | int }}{% endif %}:00'
      
input_slider: 
  alarmhour:
    name: Hour
    icon: mdi:timer
    initial: 9
    min: 0
    max: 23
    step: 1
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 1
    min: 0
    max: 59
    step: 1

group:
  Test:
    - sensor.alarm_time
    - input_slider.alarmhour
    - input_slider.alarmminutes  

automation:
    alias: 'Wake Me Up'
    trigger:
      platform: time
      after: '{{ states.sensor.alarm_time.state }}'
    action:
        service: persistent_notification.create
        data:
            message: "{{ states.sensor.alarm_time.state }}"
            title: "message"

Bad news, till now that code didnt work well for me.
But if I changed parameter trigger value to “minutes: ‘/1’”, notification appeared perfectly.

Please help me.

1 Like

Found a better way to format the slider values:

value_template: ‘{{ “%0.02d:%0.02d” | format(states(“input_slider.peter_hour”) | int, states(“input_slider.peter_minutes”) | int) }}’

1 Like

After upgrade to 29.4 my alarm clock don’t work.
I am use this string as triger:
value_template: ‘{{ now().time().strftime("%-H") == states.sensor.alarm_hour.state and now().time().strftime("%-M") == states.sensor.alarm_minutes.state }}’