Help with new alarm clock projetc

Hello guys! Happy new year to y’all!

I wonder if someone could help me out with this new code: I need to create an alarm clock set manually on HA dashboard, using input numbers and booleans, since google home alarms does not work anymore.

I have this package as for now:

input_number:

  wake_clock_hour:
    name: "Hour"
    icon: mdi:timer
    min: 0
    max: 23
    step: 1
    unit_of_measurement: "Hours"

  wake_clock_minute:
    name: "Minute"
    icon: mdi:timer
    min: 0
    max: 50
    step: 10
    unit_of_measurement: "Minutes"

input_boolean:

  wake_clock_status:
    name: "Alarm"
    icon: mdi:alarm-bell

sensor:

  - platform: template
    sensors:

      wake_clock:
        friendly_name: "Alarm"
        icon_template: mdi:alarm-check
        value_template: "{{ '%0.02d:%0.02d' | format(states('input_number.wake_clock_hour') | int, states('input_number.wake_clock_minute') | int) }}"

      wake_time_left:
        friendly_name: "Time left until alarm"
        icon_template: mdi:timer
        value_template: >-
          {% set t = states('sensor.time').split(':') %}
          {% set t = t[0]|int*60 + t[1]|int %}
          {% set a = states('sensor.wake_clock').split(':') %} 
          {% set a = a[0]|int*60 + a[1]|int %}
          {% if a < t %}
            {% set a = a + 24*60 %}
          {% endif %}
          {{ ((a - t)*60)|timestamp_custom('%H:%M', false) }}        
        entity_id: sensor.time

automation:

  - alias: First routine
    id: wake_1_shour_before
    initial_state: true
    trigger:
      - platform: template
        value_template: "{{ (states('sensor.time') == (as_timestamp(states.sensor.wake_clock.state) - 3600) | timestamp_custom('%H:%M')) }}"
    action:
      - service_template: >-
          {% if is_state('input_boolean.wake_aquecer_toalha', 'on') %}
            switch.turn_on
          {% else %}
            script.nada
          {% endif %}
        data_template:
          entity_id: >-
              {% if is_state('input_boolean.wake_aquecer_toalha', 'on') %}
                switch.toalheiro
              {% endif %}
      - delay: '00:40:00'

I get this:

image

Looks really good :slight_smile:

The problem is that this template does not work:

value_template: "{{ (states('sensor.time') == (as_timestamp(states.sensor.wake_clock.state) - 3600) | timestamp_custom('%H:%M')) }}"

It is used on the automation trigger to start one hour before the alarm set. I think the tamestamp are not working for the sensor.wake_clock.

Maybe @pnbruckner or @petro could save my skin yet again? haha

Thanks!

Have you tried the template editor (under developper tools)? What does your expression returns there?

Thanks for your reply!

I was reading the forums and trying many scrips for hours now, using the template editor all the time.

I get this:

Thanks!

I’m not an expert but I always try to break it up in parts:

{{ states.sensor.time.state}}

That should return the current time
Try what this returns:

{{ (as_timestamp(states.sensor.wake_clock.state) }}

Good luck!

Thanks!

Unfortunately I get “none” as a result.

I think since the sensor.wake_clock returns a text, I need to break it, identify the “hours” and “minutes” and set as a “timestamp” so I could calculate with the sensor.time entity.

No sure how to do it.

Ok, so this seems to work on my template editor:

    trigger:
      - platform: template
        value_template: >-
          {% set wake_hour = (states('sensor.wake_clock').split(':').0 | int) - 1  %}
          {% set wake_final_time = wake_hour ~ ":" ~ (states('input_number.wake_clock_minute') | int) %}
          {{ wake_final_time == states.sensor.time.state }}

Will try the next morning.

I advise you to not do it like this, even the docs say to avoid this, read the warning in this section.

1 Like

May I ask why you don’t use input_datetime for your alarm clock?

Thanks for the tip!

Thanks! will take a look at input_datetime.

This is how I use it:
I have input sliders to set time and minute like these:

  ah_mon:
    name: Ora
    icon: mdi:timer
    options:
     - '00'
     - '01'
     - '02'
     - '03'
     - '04'
     - '05'
     - '06'
     - '07'
     - '08'
     - '09'
     - '10'
     - '11'
     - '12'
     - '13'
     - '14'
     - '15'
     - '16'
     - '17'
     - '18'
     - '19'
     - '20'
     - '21'
     - '22'
     - '23'
    initial: '04'

  am_mon:
    name: Perc
    icon: mdi:timer
    options:
     - '00'
     - '10'
     - '20'
     - '30'
     - '40'
     - '50'
    initial: '40'

then template sensors liek these:

  - platform: template
    sensors:
      sen_at_mon:
        friendly_name: 'Monday'
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_select.ah_mon") | int, states("input_select.am_mon") | int) }}'

And then the wake up automation is:

# Wake MONDAY
  alias: 'WAKE - Monday'
  initial_state: true
  trigger:
    - platform: template
      value_template: '{{ states.sensor.time.state == states.sensor.sen_at_mon.state }}'


  condition:
    condition: and
    conditions:
      - condition: time
        weekday:
          - mon
      - condition: state
        entity_id: 'input_boolean.holiday'
        state: 'off'
      - condition: state
        entity_id: 'group.telefonok'
        state: 'on'
      - condition: template
        value_template: '{{ (states.input_select.ah_mon.state | int) > 0 }}'


  action:
    - service: homeassistant.turn_on
      entity_id: input_boolean.ebredes
    - service: shell_command.ebresztes

cheers
tom

This seems overkill for me, if I understand correctly and assuming you have an alarm clock for every day in the week, you have 14 input slider, 7 template sensors and 7 automations and a whole lot of code.

With clever naming you can reduce this to 7 input datetimes (one for each day) and 1 automation (assuming the actions to be executed on wakeup are the same every day). I don’t have this setup as I use AppDaemon for my automations, but I think one could simplify this massively like this:

Input_datetime:
Create 7 input_datetime:

  • input_datetime.alarm_monday
  • input_datetime.alarm_tuesday
  • etc.

Automation
Trigger:
Add a trigger for each input_datetime by using a template trigger

Condition:
last word of trigger entity_id == weekday of today

Action:
Whatever you want to do to wake you up

The only thing were I’m not really sure is getting the triggering entity_id from a template. (Condition part)
@Mutt
Hey mister template :stuck_out_tongue_winking_eye: can you get the triggering entity_id if the trigger is a template that compares the value of two entity_ids? I’ll test it myself when I’m back home but I thought you may know this.

Not at home myself at the mo, but I would suggest naming with ‘mon’ rather than ‘monday’ it’s not much difference but entities are shorter and it’s a constant 3 letter substitution (no difference for the template but neater in my OCD head)
Petro did a post on this sort of thing really recently, might be worth a search …

May I ask though why you are re-inventing the wheel ?
There are lots of threads on here about creating just this sort of thing …

Is this mean’t for me?

Sorry Burning,
No that was not meant for you, you are not building a new alarm clock.
I’m lazy and thought it would be obvious, I ended up having to write this, so curse my lazyness :roll_eyes:

I’ve managed to make it work like this:

input_number:

  wake_clock_hour:
    name: "Hour"
    icon: mdi:timer
    min: 0
    max: 23
    step: 1
    unit_of_measurement: "Hours"

  wake_clock_minute:
    name: "Minute"
    icon: mdi:timer
    min: 0
    max: 50
    step: 10
    unit_of_measurement: "Minutes"

input_boolean:
 
  wake_clock_status:
    name: "Alarm"
    icon: mdi:alarm-bell

sensor:

  - platform: template
    sensors:

      wake_clock:
        friendly_name: "Alarm"
        icon_template: mdi:alarm-check
        value_template: "{{ '%0.02d:%0.02d' | format(states('input_number.wake_clock_hour') | int, states('input_number.wake_clock_minute') | int) }}"

      wake_time_left:
        friendly_name: "Time left"
        icon_template: mdi:timer
        value_template: >-
          {% set t = states('sensor.time').split(':') %}
          {% set t = t[0]|int*60 + t[1]|int %}
          {% set a = states('sensor.wake_clock').split(':') %} 
          {% set a = a[0]|int*60 + a[1]|int %}
          {% if a < t %}
            {% set a = a + 24*60 %}
          {% endif %}
          {{ ((a - t)*60)|timestamp_custom('%H:%M', false) }}        

automation:

  - alias: Routine start 1 hour early
    id: wake_1_hour_before
    initial_state: true
    trigger:
      - platform: template
        value_template: >-
          {% set sleep_final_time = ('%0.02d' | format(states('sensor.wake_clock').split(':').0 | int - 1 )) ~ ":" ~ ('%0.02d' | format(states('input_number.wake_clock_minute') | int)) %}
          {{ wake_final_time == states.sensor.time.state }}
    action:
      - service_template: >-
          {% if is_state('input_boolean.wake_aquecer_toalha', 'on') %}
            switch.turn_on
          {% else %}
            script.nada
          {% endif %}
        data_template:
          entity_id: >-
              {% if is_state('input_boolean.wake_aquecer_toalha', 'on') %}
                switch.toalheiro
              {% endif %}
      - delay: '00:40:00'
      [...]

The problem is that I am still working with text, not timestamp so calculating time would work perfectly (ie 23:00 + 1 hour would be 24:00, not 00:00).

I am pretty sure there is a valid usable alarm clock template available somewhere but could find it.

I really don’t need to set the specific day for the alarm since I plan to use an automation that simply turns of the alarm itself on weekends and so on.

Also I need to be able to easily calculate automation triggers changing the start time using the clock alarm time, for example, 1 hour before the alarm, 2 hours after the alarm and so on.

A bonus would be to know how much time left until the next alarm, achieved on the above template sensor.

Again, this is not ideal since I am using text to calculate everything, instead of using timestamp.

Sorry, I did not get that. I don’t have that many entities created, just a single alarm time for everyday.

This was meant for @towme

Sorry, but there is no time value of 24:00, it goes from 23:59:59 to 00:00:00
I like the rest of your work so far (not read it all yet, will edit this post when I have).

Haha I assumed it was not mean’t for me, just wanted to be sure :stuck_out_tongue_winking_eye:

Hello!

Sorry that was exacaly my point. Using text to calculate the hour part only, as I am doing on this project, If I add 1 hour to 23, it would result on 24 (24:00), it does not exists. If timestamp was used, it would result 00:00, calculating the entire information as time, not separately (hour/minute) as texts.