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 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.
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 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 …
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
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.
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).
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.