Phillips Hue Wakeup Light with fade-in

I’ve made a wakeup light that uses the smooth transition on a Phillips Hue group (Room).

You can set the transition duration in minutes so that the maximum brightness is reached on “Alarm time”.

configuration.yaml

input_slider:
  wakeup_hour:
    name: Hour
    icon: mdi:clock-in
    initial: 6
    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: 10
    min: 5
    max: 60
    step: 5

input_boolean:
  wakeup:
    name: 'Enable wakeup light'
    icon: mdi:power
    initial: off

  wakeup_weekends:
    name: 'Wakeup weekends'
    icon: mdi:power
    initial: off

sensor:
  - platform: template
    sensors:
      wakeup_alarm_time:
        friendly_name: 'Alarm time'
        value_template: '{% if states.input_slider.wakeup_hour.state|length == 3 %}0{% endif %}{{ states.input_slider.wakeup_hour.state | int }}:{% if states.input_slider.wakeup_minutes.state|length == 3 %}0{% endif %}{{ states.input_slider.wakeup_minutes.state | int }}'
  - platform: template
    sensors:
      wakeup_start_time_lights:
        friendly_name: 'Fade-in start time'
        value_template: >
          {% if states.wakeup_alarm_time and states.input_slider.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_slider.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 %}

  - platform: template
    sensors:
      wakeup_fadein_duration:
        friendly_name: 'Fade-in duration'
        value_template: '{{ states.input_slider.wakeup_duration.state | int }}'
        unit_of_measurement: 'min'

group:
  wakeuplight:
    name: Wakeup light
    control: hidden
    entities:
      - sensor.wakeup_alarm_time
      - sensor.wakeup_start_time_lights
      - sensor.wakeup_fadein_duration
      - input_slider.wakeup_hour
      - input_slider.wakeup_minutes
      - input_slider.wakeup_duration
      - input_boolean.wakeup
      - input_boolean.wakeup_weekends

automation old:
  - alias: 'WakeUp Light Bedroom'
    hide_entity: True
    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_bedroom

script:
  wakeup_bedroom:
    alias: 'Wakeup lighting, smooth transition'
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: light.bedroom
          brightness: 1
          xy_color: [0.3698, 0.3723]
      - delay:
          seconds: 1
      - service: light.turn_on
        data_template:
          entity_id: light.bedroom
          brightness: 255
          transition: '{{ states.input_slider.wakeup_duration.state | multiply(60) | int }}'

# EDIT: Everything below is not longer necessary as Hass now supports Hue natively!

script:
  wakeup_bedroom:
    alias: 'Wakeup lighting, smooth transition'
    sequence:
      - service: shell_command.wakeup_init
      - service: shell_command.wakeup_transition

shell_command:
  wakeup_init: "/home/homeassistant/.homeassistant/bash_scripts/phue_control.sh bedroom '{\"on\":true,\"bri\":1,\"xy\":[0.3698,0.3723],\"transitiontime\":50}'"
  wakeup_transition: "/home/homeassistant/.homeassistant/bash_scripts/phue_control.sh bedroom '{\"bri\":254,\"transitiontime\":{{ states.input_slider.wakeup_duration.state | multiply(600) | int }}}'"

EDIT-1: I created this “app” a long time ago and I just found out that hass now supports Phillips Hue natively which means that the bash script (phue_control.sh) below is no longer needed!

EDIT-2: Added input booleans


The two shell commands uses a bash script (phue_control.sh) that I wrote to be able to use the groups (Rooms) and simultaneous transition function. I used the instructions given here to create an API key and to find the ID’s for my hue groups and lights. The wakeup_init sets the color and minimum brightness and the wakeup_transition sets the transition time and target brightness (max=254).

phue_control.sh

#!/bin/bash

# INPUT: hue IP & api key
HOST=<YOUR HUE BRIDGE IP>
APIKEY=<YOUR KEY>
# group/light friendly name
FNAME=$1
# hue command
COMMAND=$2
# INPUT: specify hue type ("groups" or "lights") and ID
case $FNAME in
	bedroom)
		TYPE="groups"
		ID=2
		;;
	nattbord_lorentz)
		TYPE="lights"
		ID=1
		;;
	nattbord_karen)
		TYPE="lights"
		ID=2
		;;
	sofaspotter)
		TYPE="groups"
		ID=1
		;;
	spisebord)
		TYPE="lights"
		ID=3
		;;
	*)
esac

if [ "$TYPE" == "groups" ] ; then
	AS="action"
else
	AS="state"
fi

curl -X PUT -H "Content-Type: application/json" -d $COMMAND http://$HOST/api/$APIKEY/$TYPE/$ID/$AS
20 Likes

Very cool! And quite the savings on buying the Philips Alarm Clock.

I’m going to try this; I only have one lightstrip in the bedroom but I’m real sensitive to lights when I am sleeping so it may work in my case; thanks!

1 Like

I created this “app” a long time ago and I just found out that hass now supports Phillips Hue natively which means that the bash script is no longer needed!

Replace the script with this:

script:
  wakeup_bedroom:
    alias: 'Wakeup lighting, smooth transition'
    sequence:
      - service: light.turn_on
        data_template:
          entity_id: light.bedroom
          brightness: 1
          xy_color: [0.3698, 0.3723]
      - delay:
          seconds: 1
      - service: light.turn_on
        data_template:
          entity_id: light.bedroom
          brightness: 255
          transition: '{{ states.input_slider.wakeup_duration.state | multiply(60) | int }}'
2 Likes

Very cool! I’m gonna have to try to implement this!

Actually I’m wondering what that xy color represents?

1 Like

I have a similar setup here: https://github.com/Danielhiversen/home-assistant_config/blob/master/packages/alarm_clock.yaml

2 Likes

@Lorentz this is awsome, thanks for sharing!

However it seems your config is missing the setup for these 3 entities thats showed in your screenshot and also in the group settings:

Alarm time
Enable Wakeup light
Wakeup Weekends

Could you maybe update your example code?

Thanks!

Thanks! xy color is just one way of setting the color, you can use rgb if you want. Read more about it here: https://www.developers.meethue.com/documentation/core-concepts

Thank you:) Alarm time is one of the sensors defined. “Enable wakeup” and “wakeup weekends” are just regular input booleans. I will update the code later…

oh I know, I suppose I can just try some sample JSON to figure it out (not home right now) I was wondering what color that equated to.

Ok:) It’s a high kelvin white, to resamble daylight.

Updated code with input_boolean’s.

I don’t use the script to call the lights service, I just do it all in the automation like so.

- alias: 'wake up bedroom'
  hide_entity: True
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.wakeup_start_time_lights.state }}'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.lex_iphone
        state: home
      - 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: light.turn_on
    data:
      entity_id: light.hue_go_1, light.hue_go_2
      profile: relax
    data_template:
      transition: '{{ states.input_slider.wakeup_duration.state | multiply(60) | float }}'
1 Like

Hey guys,

Trying to get this to work but somehow my “wakeup_start_time_lights” sensor isn’t being set, resulting in it not working :confused:

Any idea as to why this is?

I had this working perfectly but my installs just started showing the same behaviour…

I think I found the error in the first line of the value template for the “wakeup_start_time_lights” sensor:

Change:
{% if states.wakeup_alarm_time and states.input_slider.wakeup_duration %}
to:
{% if states.sensor.wakeup_alarm_time and states.input_slider.wakeup_duration %}

Somehow this worked in 0.51 (it shouldn’t), but not when I updated to 0.54.

@Kantraz @jamieb

2 Likes

Works like a charm thanks @Lorentz

1 Like

That did the trick! The whole thing now looks like this:

- platform: template
  sensors:
    wakeup_start_time_lights:
      friendly_name: 'Fade-in start time'
      value_template: >
        {% if states.sensor.wakeup_alarm_time and states.input_slider.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_slider.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 %}

I think you pasted the old version…

Thanks! Fixed it!

I get an error:
ERROR (MainThread) [homeassistant.loader] Unable to find component input_slider

Any idea how to fix it?