Creating a alarm clock

I erased the “view” line and now it looks fine in the frontend :slight_smile:
However I would like this look on its own tab in the top of the frontend,

So create a group that is a view, and put the group you just created in it :wink:

Works :wink:
I guess it is not possible to remove it from the home tab and only have it shown on its own tab?

Only if you create your own default view.

I thought so.
Thanks for helping :-):+1:

2 Likes

Hi,

Based on this post Phillips Hue Wakeup Light with fade-in I’ve made several adjustments to work with my lights (as only Hue and Lifx platforms accept transition).

I use MiLight but served by a NodeMCU running this project https://github.com/sidoh/esp8266_milight_hub, not the ibox. Basically you can have as many groups as you want and loose the ibox. The performance is much better and on the gateway can be created as many lights and groups as you want (instead of buying an ibox for each group of four lights) thus limiting costs to a single NodeMCU and a NRF24L01.

The scripts for fade-in should work on any light platform that can be dimmed.

Apart from transition I’ve made an input select (not limiting thus to a single light). There is a light named light.all_lights which is a group, however not in HA but a group made in MiLight gateway (as turning a group of lights in HA would set them individually). Also, there are input numbers for maximum brightness and transition time in seconds (this is only intermediary as, depending on the the total time for the fade-in, this would be adjusted to hurry-up or slow-down the transitions due to the fact that brightness can be served to bulbs only as integer).

When using with MiLight (probably also with other platforms) an automation should set the brightness to 1 before the alarm is set during the evening (after: sunset) / night (before: sunrise) as otherwise the light would start at the last known brightness level. Setting the brightness to 1 when the light turn all time has the disadvantage of flickering but using the input boolean “Enable wakeup light” as condition this can be limited only to the last hour/hours before sleep (this is not covered here).

My configuration is split into individual folders for each component so indentation needs to be amended if using a single configuration file (although I strongly advise splitting it).

#1. Automation
- alias: 'Wakeup Light'
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.wakeup_start_time_lights.state }}'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.wakeup
        state: 'on'
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.wakeup_weekends
            state: 'on'
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
  action:
    - service: script.wakeup_room

#2. Sensors
- platform: template
  sensors:
    wakeup_alarm_time:
      friendly_name: 'Alarm time'
      value_template: '{% if states.input_number.wakeup_hour.state|length == 3 %}0{% endif %}{{ states.input_number.wakeup_hour.state | int }}:{% if states.input_number.wakeup_minutes.state|length == 3 %}0{% endif %}{{ states.input_number.wakeup_minutes.state | int }}'
- platform: template
  sensors:
    wakeup_fadein_duration:
      friendly_name: 'Fade-in duration'
      value_template: '{{ states.input_number.wakeup_duration.state | int }}'
      unit_of_measurement: 'min'
- platform: template
  sensors:
    wakeup_start_time_lights:
      friendly_name: 'Fade-in start time'
      value_template: >
        {% if states.sensor.wakeup_alarm_time and states.input_number.wakeup_duration %}
        {% set alarmtime = states.sensor.wakeup_alarm_time.state %}
        {% set alarm_hour = alarmtime.split(":")[0] | int %}
        {% set alarm_min = alarmtime.split(":")[1] | int %}
        {% set light_dur = states.input_number.wakeup_duration.state | int %}
        {% set alarm_min_light = alarm_min - light_dur %}
        {% if alarm_min_light  < 0 %}
        {% set alarm_min_light = alarm_min_light + 60 %}
        {% set alarm_hour_light = alarm_hour - 1 %}
        {% if alarm_hour_light < 0 %}{% set alarm_hour_light = 23 %}{% endif %}
        {% if alarm_hour_light < 10 %}0{% endif %}{{ alarm_hour_light}}:{% if alarm_min_light < 10 %}0{% endif %}{{ alarm_min_light }}
        {% else %}
        {% if alarm_hour < 10 %}0{% endif %}{{ alarm_hour }}:{% if alarm_min_light < 10 %}0{% endif %}{{ alarm_min_light }}
        {% endif %}
        {% endif %}
#replace with own light ids
- platform: template
  sensors:
    wakeup_light_current_brightness:
      friendly_name: 'Light brightness'
      value_template: >
        {% if is_state('input_select.wakeup_light_id', 'light.light1') %}
          {{states.light.light1.attributes.brightness | int}}
        {% elif is_state('input_select.wakeup_light_id', 'light.light2') %}
          {{states.light.light2.attributes.brightness | int}}
        {% elif is_state('input_select.wakeup_light_id', 'light.light3') %}
          {{states.light.light3.attributes.brightness | int}}
#add the other lights
        {% else %}
          0
        {% endif %}
- platform: template
  sensors:
    wakeup_adjusted_transition:
      friendly_name: 'Adjusted transition'
      value_template: >
        {% if states.sensor.wakeup_light_current_brightness.state|int < ((((states.input_number.wakeup_max_brightness.state|int/(states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/(states.input_number.wakeup_duration.state|int*60))|round)|round)*(states.input_number.transition.state | int + 1)) -states.input_number.wakeup_duration.state|int*60)*(((states.input_number.wakeup_max_brightness.state|int)*(states.input_number.transition.state | int))/(states.input_number.wakeup_duration.state | int * 60))|round %}
          {{states.input_number.transition.state|int-([states.input_number.transition.state|int-(states.input_number.wakeup_duration.state|int*60/(states.input_number.wakeup_max_brightness.state|int/((states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/states.input_number.transition.state|int)|round)|round)|round),0]|max)}}
        {% else %}
          {{states.input_number.transition.state|int-([states.input_number.transition.state|int-(states.input_number.wakeup_duration.state|int*60/(states.input_number.wakeup_max_brightness.state|int/((states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/states.input_number.transition.state|int)|round)|round)|round),0]|max) + 1}}
        {% endif %}

#3. input boolean
wakeup:
  name: 'Enable wakeup light'
  icon: mdi:power
  initial: off
wakeup_weekends:
  name: 'Wakeup weekends'
  icon: mdi:power
  initial: off

#4. input number
wakeup_hour:
  name: Hour
  icon: mdi:clock-in
  initial: 7
  min: 0
  max: 23
  step: 1
wakeup_minutes:
  name: Minutes
  icon: mdi:clock-in
  initial: 30
  min: 0
  max: 55
  step: 5
wakeup_duration:
  name: Light fade-in duration
  icon: mdi:clock-in
  initial: 20
  min: 5
  max: 60
  step: 5
wakeup_max_brightness:
  name: Maximmum brightness
  icon: mdi:white-balance-sunny
  initial: 225
  min: 15
  max: 255
  step: 17
transition:
  name: Transition in seconds
  icon: mdi:clock-in
  initial: 10
  min: 1
  max: 60
  step: 1

#5. input select
  wakeup_light_id:
    name: Light id
    options:
     - light.all_lights
     - light.light1
     - light.light2
     - light.light3
#add the other lights
    initial: light.light1
    icon: mdi:weather-sunset-up

#6. scripts
wakeup_room_initial:
  alias: 'Wakeup initial'
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: '{{ states.input_select.wakeup_light_id.state}}'
        brightness: 0
        rgb_color: [1,1,1]
wakeup_room_turnon:
  alias: 'Wakeup turnon'
  sequence:
    - service: script.turn_off
      entity_id: script.wakeup_room_iterate
    - service: light.turn_on
      data_template:
        entity_id: '{{ states.input_select.wakeup_light_id.state}}'
        brightness: '{{ states.sensor.wakeup_light_current_brightness.state|int + (((states.input_number.wakeup_max_brightness.state|int)*(states.input_number.transition.state | int))/(states.input_number.wakeup_duration.state | int * 60))|round }}'
        rgb_color: [1,1,1]
    - delay: '00:00:{{states.sensor.wakeup_adjusted_transition.state|int}}'
    - service: script.turn_on
      entity_id: script.wakeup_room_iterate
wakeup_room_iterate:
  alias: 'Wakeup iterate'
  sequence:
    - service: script.turn_off
      entity_id: script.wakeup_room_turnon
    - service_template: >
        {% if states.sensor.wakeup_light_current_brightness.state|int < states.input_number.wakeup_max_brightness.state|int %}
          script.turn_on
        {% else %}
          script.turn_off
        {% endif %}
      data:
        entity_id: script.wakeup_room_turnon
wakeup_room:
  alias: 'Wakeup'
  sequence:
    - service: script.turn_on
      entity_id: script.wakeup_room_initial
    - delay: '00:00:{{states.input_number.transition.state|int-([states.input_number.transition.state|int-(states.input_number.wakeup_duration.state|int*60/(states.input_number.wakeup_max_brightness.state|int/((states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/states.input_number.transition.state|int)|round)|round)|round),0]|max)}}'
    - service: script.turn_on
      entity_id: script.wakeup_room_turnon
3 Likes

I finally got all my lights connected (combination zwave & wink/zigbee) and want a similar automation. I copied your example and modified for a few of my lights for testing but I do not understand all the configurables & scripts.

Would you mind explaining each?

1 Like

Hi @Corey_Johnson,

As long as it works and it doesn’t hide the remote or leaves dirty dishes, does it really matter? :slight_smile:

So here it is: in addition to the components set in the initial topic (based on Hue) I added the maximum brightness level as input number/slider (which could be lower than default of 255), then a sensor for current brightness of the light (used to compare against the target), input number/slider for transition (MiLight component doesn’t support transition however by setting the script to issue the light.turn_on command at a set time interval the same effect can be reached) and adjusted transition as a sensor (this is due to the fact that, in order to reach the target brightness in a specified amount of time, the transition interval should be in fraction of seconds, however the platform only supports rounded numbers; at a certain moment there is the need for hurry-up or slow-down the scripts as the default transition would not be sufficient).

I’ve started with the following script which would increase the brightness from 1 to 255 (actually I haven’t written 255 components but only one and then used Microsoft Excel to substitute the values from prior). Obviously there are a lot of problems with this (as I haven’t written each and every script component it wasn’t such a big issue to start with) because the file is large, the transition interval is fixed at 4 seconds and the end brightness is not adjustable. And for each room I should have created a different set of components (script, input number, sensor, etc).

wakeup_bedroom:
  alias: 'wakeup'
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom1
        brightness: 1
        rgb_color: [1, 1, 1]
    - delay:
        seconds: 4
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom1
        brightness: 2
        rgb_color: [1, 1, 1]
    - delay:
        seconds: 4
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom1
        brightness: 4
        rgb_color: [1, 1, 1]
    - delay:
        seconds: 4
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom1
        brightness: 5
        rgb_color: [1, 1, 1]
... => there are a lot of intermediary items :slight_smile: 
    - delay:
        seconds: 4
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom1
        brightness: 254
        rgb_color: [1, 1, 1]
    - delay:
        seconds: 4
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom1
        brightness: 255
        rgb_color: [1, 1, 1]

The scripts can be definitely improved.

You can hide the last three scripts in the group.

2 Likes

How do I split this configuration into multiple files ? Thanks !

Do you want to split the whole thing in to one file, or split the various parts in to files?

Check the docs for packages and includes.

I want to easily be able to cut and paste this alarm clock project and change entities to suit my lights. I am lost as some of the automation have - and some such as scripts, input number don’t in the above example. I am using Notepad ++ and get frequently locked with invalid config for even simple changes. This is going to be a weekend project learning indents with yaml and such !!

Create a folder in your .homeassistant directory called packages.

In your configuration.yaml, under the homeassistant: bit (where you have your longitude and latitude set) , put packages: !include_dir_named packages/ (indented 2 spaces so it lines up with long, lat, timezone etc).

Then inside the packages folder create a new file called alarm_clock.yaml , and then copy and paste the whole thing into it. Go through and change the entity_id’s, save the file, restart HA, profit :+1:

(edit) - I don’t know why but the OP has added # marks before all the component declarations, so you’ll have to remove them and then check all the indentations too.

2 Likes

If it helps, I’ve got prepackaged alarm clocks in my config. The Bedroom one is the most intricate.

Triggers at alarm time (if we’re home), plays Free Radio (local radio) over the chromecast and (if it’s dark) fades up the lights. Also has a repeat boolean so you can set it for 5am tomorrow and it will go off at 5 every morning, or it will just go off tomorrow as a one off.

link removed

(note that the condition I use for the lights is based on a virtual ‘dark outside’ sensor in another package, but you could just use sunrise / sunset)

4 Likes

Hi @Orange_Waters,

Have a look at:

Create the folders for each component and add, for each folder, a specific reference in configuration.yaml, for example (create the folders only for the components you have):

automation: !include_dir_merge_list automation/
binary_sensor: !include_dir_merge_list binary_sensor/
camera: !include_dir_merge_list camera/
climate: !include_dir_merge_list climate/
device_tracker: !include_dir_merge_list device_tracker/
group: !include_dir_merge_named group/
input_boolean: !include_dir_merge_named input_boolean/
input_number: !include_dir_merge_named input_number/
input_select: !include_dir_merge_named input_select/
light: !include_dir_merge_list light/
media_player: !include_dir_merge_list media_player/
panel_custom: !include panel_custom.yaml
panel_iframe: !include_dir_merge_named panel_iframe/
sensor: !include_dir_merge_list sensor/
scene: !include_dir_merge_list scene/
switch: !include_dir_merge_list switch/
script: !include_dir_merge_named script/
shell_command: !include_dir_merge_named shell_command/
zone: !include_dir_merge_list zone/

In each folder you create files (with extension yaml). The name of each individual file is not important.

You need to ensure that in configuration.yaml you do not have duplicates (if you use automation folder by adding “automation: !include_dir_merge_list automation/” then move all automations to this folder).

In the files in the automation folder you do need to modify indentation (remove two spaces from the automations in configuration.yaml so it will start on the left margin).

Thanks to @Petrica and @anon43302295 I got the folders and config files figured out. I see the tabs etc but I am unable to get it working. When I setup the time and chose “Enable wakeup light”, nothing happens except my milight becomes red and very low brightness. Any thoughts ?

Wow, I didn’t know this option existed.
This is excellent!!!

Constantly complaining that I have little bits of code for each DIY spread all over every other file…

Thank you!

1 Like

Hi @Orange_Waters,

The color is normal as my settings were based on the rgb_color [1,1,1] which is red (you can change it to whatever setting you like); it starts dimmed, at brightness 1 and then increases its brightness to 255 after each 10 seconds. I use FUT103 bulbs which have a total of 8 leds (2 for color, 3 for warm white and 3 for cool white) and the increase in the brightness results in a reddish sunrise effect.

However, if you used the scripts I posted the “enable wakeup light” switch should not trigger it on the spot but only when the time for wakeup arrives.

The light can be called “on demand” with the script wakeup (the other three are hidden) but not from the switch enable wakeup light.

I am not able to call script on demand or use the enable wakeup light to get the alarm clock working. The light does start with red color, low brightness but does not transition at all and stays the same. I am using RGB milight bulbs with esp8266. The config file check says valid. All the folders are created as suggested. Any help would be greatly appreciated.

Hi,

What do you mean about “milight bulbs with esp8266”? Does esp8266 control the relay for the light or you’re using Chris’ esp8266 milight hub (with the NRF2401/LT8900 module)?

Could you post your HA components?

My components are hassio, Chris’ esp8266 milight hub (NRF2401), milight RGB bulbs - 3 groups of them with 2 bulbs in each group, mosquitto broker (from hassio addons), lights controlled using mqtt_json platform based on this example config from this thread “MiLight Status Feedback with NodeMCU!”. That’s how I found your posts there and tracked back to this alarm clock thread.

  • name: “Office Lamp”
    command_topic: milight/0x0001/rgb_cct/1
    state_topic: milight/updates/0x0001/rgb_cct/1
    <<: &MILIGHT_PARAMS
    platform: mqtt_json
    color_temp: true
    rgb: true
    brightness: true
  • name: “Livingroom Lamp”
    command_topic: milight/0x0001/rgb_cct/2
    state_topic: milight/updates/0x0001/rgb_cct/2
    <<: *MILIGHT_PARAMS
  • name: “Bedroom Lamp”
    command_topic: milight/0x0001/rgb_cct/3
    state_topic: milight/updates/0x0001/rgb_cct/3
    <<: *MILIGHT_PARAMS
  • name: “Nightstand Lamps”
    command_topic: milight/0x0002/rgbw/0
    state_topic: milight/updates/0x0002/rgbw/+
    <<: *MILIGHT_PARAMS