Creating a alarm clock

I did the following to get it to work (mines in win 10 tho’ so don’t use the %R basically just move the template into a condition and the trigger runs every min)

  • alias: Turn off Alarm
    trigger:
    • platform: time
      minutes: ‘/1’
      seconds: 0
      condition:
  • condition: template
    value_template: ‘{{ now().time().strftime("%R") == states.sensor.alarm_time_fin.state }}’
    action:
  • service: switch.turn_off
    data:
    entity_id:
    - switch.bedroom_desk_lamp
    - switch.bedroom_light_red_bottom
    - switch.bedroom_ceiling_light
    - switch.pc_monitor_and_sound
  • service: switch.turn_off
    data:
    entity_id:
    - switch.foobaralarm

you only have one entity_id in your template and it’s static so it will never trigger because the state of an input select only changes if you select something in the input select.

Use a time trigger that checks every minute and use a template condition

WORKS √

This is my config:

input_select:
  wecker:
    name: Wecker
    options:
      - '05:15'
      - '05:45'
      - '06:00'
      - '06:30'


automation:
  - alias: 'Sunrise'
    trigger:
      platform: time
      minutes: '/1'
      seconds: 0
    condition:
      platform: template
      value_template: '{{ now().strftime("%H:%M") == states.input_select.wecker.state }}'
    action:
      - [...]

Keith & Joyrider, many thanks for your hints!

I manage to fix this by add Time & Date sensor and then change the automation trigger to…

value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'

2 Likes

Working great now! Thank you so much for keeping this up!

Hi,
There is another aproach using the three template sensors:

Sensors:

sensor:       
  - platform: template
    sensors:
      alarm_hour:
        friendly_name: 'Hour'
        value_template: '{{ states.input_slider.alarmhour.state|round(0)|string }}'
      alarm_minutes:
        friendly_name: 'Minutes'
        value_template: '{{ states.input_slider.alarmminutes.state|round(0)|string }}'
      alarm_time:
        friendly_name: 'Alarm'
        value_template: '{% if states.input_slider.alarmhour.state|round(0)|string|length == 1 %}0{% endif %}{{ states.input_slider.alarmhour.state|round(0)|string }}:{% if states.input_slider.alarmminutes.state|round(0)|string|length == 1 %}0{% endif %}{{ states.input_slider.alarmminutes.state|round(0)|string }}'
      time:
        value_template: '{{ now().strftime("%H:%M")}}'        

Inputs:

input_slider: 
  alarmhour:
    name: Hour
    icon: mdi:timer
    initial: 7
    min: 0
    max: 23
    step: 1
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 0
    min: 0
    max: 55
    step: 5
    
input_boolean:    
  alarmstatus:
    name: Active
    initial: off
    icon: mdi:alarm-check

  alarmweekday:
    name: workdays
    initial: off
    icon: mdi:calendar    

Automation:

- alias: 'Alarm'
   
    trigger:
      platform: time
      minutes: '/1'
      seconds: 0    
    
    condition:
      condition: and
      conditions:
        - condition: template
          value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'

        - 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.miguel
        data:
          message: 'morning'
          title: 'ALARM'

that is one way to do it. but triggering the automation every minutes (even though stop by the condition) is something I’m not comfortable with. I feel it is putting unnecessary stress on my little Pi.

Masterkenobi,

I get your point. May you share your config?

i’m not sure what would be the worst, the trick you used using the time sensor actually checks the value_template to see if it’s being triggered every minute as well because the state of the time sensor changes every minute triggering a state_change which in turn triggers a check on the trigger template’s value_template because the state of your time sensor changed and it’s used in value_template

So not sure if using the time sensor with trigger template or a trigger that fires every minute makes a lot of diffrence … If you check the code you can see that template sensors only check the value_template if one of the states of the entities used in the value_template change.

My method doesn’t trigger the automation every minutes. Yes, it checks for the state change every minute but it didn’t actually trigger the automation and have to be stop by an additional condition.

There is no right or wrong. I guess it just a matter of preference.

Is it possible to repost the full code just like list time MasterKenobi? It is super helpful and I use that every iteration! Thanks!

I have put it up in Github for easier to manage.

4 Likes

Hi masterkenobi, thanks for your work. I really appreciate it. However, after implementing your files into my HA I can only see the alarmclock in my dashboard, all the other devices are gone. Which files do I need to modify to fix this? Thanks in advance.

Don’t copy exactly. The files are just for example. You can learn more about groups at https://home-assistant.io/components/group/

Thanks for the fast reply. Yeah, I figured that I can just edit the groups file and uncomment the default_view line. Now I see entities ony my dashboard that I actually don’t need to see but I guess that can be streamlined later.

I am not that good with all this yaml, so i decided to make a clock alarm in python instead. I’d post it on github: https://github.com/nvanggaard/clockalarm

At the moment it only works with philips hue, but i believe that it is fairly easy to modify and make it work with other stuff.

I might one day, make it generic such that it works with everything.

1 Like

If you understand HA, you can use my sample to create all kinds of automations using components that are already supported by HA, not just Hue.

I don’t know python but you know and you can create custom components. I’m sure it is easy for you to learn HA.

I know It can be done using YAML in HA but I think making a custom_component is a better way.

And yes, it can be made for all kinds of components, I just haven’t figured out which method is the best yet :slight_smile:

Good job masterkenobi (and thanks for the code!)! Works excellent with my system!

Implementing it gave me good reason for cleaning up my sensors/automations :slight_smile:

Hi,

I just want to go further the processus to create an alarm on a specific time. I want to get an alarm on weekend day at a specific time only if weather outside is OK to go running / biking or skiing :slight_smile:

Do I need to create another alarm or modify this to get a more complex automation script ?

Thanks :slight_smile: