Hi there,
I’m expanding my alarm and having some trouble comparing two timestamps. Perhaps the long post is a little bit of an overkill but I figured maybe someone would like to see the code for their own setup.
Current setup: wakeup light with radio alarm which is adjustable by a slider. Also I implemented an On/Off switch and a selector for various radio streams. Currently the whole sequence takes 20 minutes which is perfect (easy to make a slider for that as well but I have no need for that).
Frontend view:
Configuration for switch, sliders and selector:
input_boolean:
alarm_clock:
name: On/Off
initial: on
icon: mdi:alarm
input_slider:
alarm_hour:
name: Hour
icon: mdi:timer
initial: 6
min: 0
max: 23
step: 1
alarm_minutes:
name: Minutes
icon: mdi:timer
initial: 35
min: 0
max: 55
step: 5
input_select:
radio_station:
name: Radio Station
options:
- Radio 538
- Q-Music
- 3FM
- 100% NL
- Veronica
- Sky Radio
# - Arrow Classic Rock
initial: Radio 538
icon: mdi:radio
Automation (perhaps not so fancy but it works):
- alias: 'Rule 9 - Wakeup Light with Radio'
trigger:
platform: time
minutes: '/5'
seconds: '0'
condition:
condition: and
conditions:
- condition: template
value_template: '{{ ((now().strftime("%s") | int + 1200) | timestamp_custom("%H:%M")) == states.sensor.alarmtime.state }}'
- condition: state
entity_id: input_boolean.alarm_clock
state: 'on'
action:
- service: light.turn_on
data:
entity_id: light.bed_room
brightness: 255
rgb_color: [255,255,255]
transition: 1200
- service: media_player.play_media
data_template:
entity_id: media_player.bed_room
media_content_id: >
{% if is_state("input_select.radio_station", "Radio 538") %} http://vip-icecast.538.lw.triple-it.nl:80/RADIO538_MP3
{% elif is_state("input_select.radio_station", "Q-Music") %} http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3
{% elif is_state("input_select.radio_station", "3FM") %} http://icecast.omroep.nl/3fm-bb-mp3
{% elif is_state("input_select.radio_station", "100% NL") %} http://stream.100p.nl/100pctnl.mp3
{% elif is_state("input_select.radio_station", "Veronica") %} http://8543.live.streamtheworld.com/VERONICACMP3
{% elif is_state("input_select.radio_station", "Sky Radio") %} http://8623.live.streamtheworld.com:80/SKYRADIOAAC_SC
{% endif %}
media_content_type: 'audio/mp4'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.01'
- delay: '00:02:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.05'
- delay: '00:02:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.10'
- delay: '00:01:30'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.15'
- delay: '00:01:30'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.20'
- delay: '00:01:30'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.25'
- delay: '00:01:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.30'
- delay: '00:01:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.35'
- delay: '00:01:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.40'
- delay: '00:01:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.45'
- delay: '00:01:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.50'
- delay: '00:01:00'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.55'
- delay: '00:00:45'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.60'
- delay: '00:00:45'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.65'
- delay: '00:00:45'
- service: media_player.volume_set
data:
entity_id: media_player.bed_room
volume_level: '0.70'
- delay: '00:02:00'
- service: light.turn_on
data:
entity_id: light.hall_way
brightness: 125
- service: light.turn_on
data:
entity_id: light.diner_table
brightness: 125
- service: light.turn_on
data:
entity_id: light.living_room
brightness: 140
Now I want to add 2 things:
- TTS notification on my Chromecast that tells me how much time I have left untill my next alarm (by sending a http post command to trigger a script using Tasker using my wireless charger next to my bed as trigger);
- Calculate the time I have slept and make that part of my “Morning briefing”.
I can’t figure out how to calculate the time untill my next alarm.
Values that must be compared are:
Current time:
(now().strftime("%s") | int | timestamp_custom(" %H:%M"))
Wakeup time:
(states.sensor.alarmtime.state) | timestamp_custom(" %H:%M")
If I want to calculate, I first have to figure out a way to convert both “timestamps” (which are currently not seen as timestamps by HA) to a complete timestamp (year, month, day, hour, minute, second). I don’t know how to do that (rather new to HA, just installed it 3 weeks ago).
Help is much appreciated!