Sonoff Wakeup Light

Turning Sonoff B1 R2 led bulb into a wakeup light.

This setup requires the bulb to be flashed with Sonoff-Tasmota 6.3.0 by Theo Arends. All of the automation is being handled by Home Assistant 0.86.4.

Parts:

Here are the pictures showing the bulb with attached PIN headers. Detailed information can be found at https://github.com/arendst/Sonoff-Tasmota/wiki/Sonoff-B1-and-B1-R2

Home Assistant - lights.yaml

- platform: mqtt
  name: "Wakeup Lamp"
  command_topic: "cmnd/sonoffb11/POWER"
  state_topic: "stat/sonoffb11/POWER"
  payload_on: "ON"
  payload_off: "OFF"
  brightness_command_topic: "cmnd/sonoffb11/DIMMER"
  brightness_scale: 100
  brightness_state_topic: "stat/sonoffb11/RESULT"
  brightness_value_template: "{% if value_json.Dimmer is defined %}{{ value_json.Dimmer }}{% else %}{{ states.light.test_b1.attributes.brightness / 255 * 100 }}{% endif %}"
  rgb_command_topic: "cmnd/sonoffb11/COLOR"
  rgb_command_template: "{{ '%02X%02X%02X0000' | format(red, green, blue) }}"
  rgb_state_topic: "stat/sonoffb11/RESULT"
  rgb_value_template: "{% if value_json.Color is defined %}{{ value_json.Color[0:2]|int(base=16) }},{{ value_json.Color[2:4]|int(base=16) }},{{ value_json.Color[4:6]|int(base=16) }}{% else %}{{ states.light.test_b1.attributes.rgb_color|join(',') }}{% endif %}"
  color_temp_command_topic: "cmnd/sonoffb11/CT"
  color_temp_state_topic: "stat/sonoffb11/RESULT"
  color_temp_value_template: "{% if value_json.CT is defined %}{{ value_json.CT }}{% else %}{{ states.light.test_b1.attributes.color_temp }}{% endif %}"
  effect_command_topic: "cmnd/sonoffb11/SCHEME"
  effect_state_topic: "stat/sonoffb11/RESULT"
  effect_value_template: "{% if value_json.Scheme is defined %}{{ value_json.Scheme }}{% else %}{{ states.light.test_b1.attributes.effect }}{% endif %}"
  effect_list:
    - 0
    - 1
    - 2
    - 3
    - 4
  availability_topic: "tele/sonoffb11/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"

Home Assistant - input_boolean.yaml

wakeup:
  name: Enable wakeup light
  icon: mdi:power
  initial: 'off'
wakeup_weekends:
  name: Wakeup weekends
  icon: mdi:power
  initial: 'off'

Home Assistant - input_number.yaml

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: 0
  min: 0
  max: 55
  step: 5
wakeup_duration:
  name: Light fade-in duration
  icon: mdi:clock-in
  initial: 15
  min: 5
  max: 60
  step: 5

Home Assistant - script.yaml

wakeup_bedroom:
  alias: 'Wakeup lighting, smooth transition'
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.wakeup_lamp
        brightness: 1
        color_temp: 500
    - delay:
        seconds: 1
    - service: light.turn_on
      data_template:
        entity_id: light.wakeup_lamp
        brightness: 64
        transition: '{{ (( states.input_number.wakeup_duration.state | float * 60 / 4 )| round ) | int }}'
    - delay: '00:00:{{ (( states.input_number.wakeup_duration.state | float * 60 / 4 )| round ) | int }}'
    - service: light.turn_on
      data_template:
        entity_id: light.wakeup_lamp
        brightness: 128
        transition: '{{ (( states.input_number.wakeup_duration.state | float * 60 / 4 )| round ) | int }}'
    - delay: '00:00:{{ (( states.input_number.wakeup_duration.state | float * 60 / 4 )| round ) | int }}'
    - service: light.turn_on
      data_template:
        entity_id: light.wakeup_lamp
        brightness: 191
        transition: '{{ (( states.input_number.wakeup_duration.state | float * 60 / 4 )| round ) | int }}'
    - delay: '00:00:{{ (( states.input_number.wakeup_duration.state | float * 60 / 4 )| round ) | int }}'
    - service: light.turn_on
      data_template:
        entity_id: light.wakeup_lamp
        brightness: 255
        transition: '{{ (( states.input_number.wakeup_duration.state | float * 60 / 4 )| round ) | int }}'

Home Assistant - sensors.yaml

- platform: time_date
  display_options:
    - 'time'
    - 'date'
    - 'date_time'
    - 'time_date'
    - 'time_utc'
    - 'beat'
- 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_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 %}
- platform: template
  sensors:
    wakeup_fadein_duration:
      friendly_name: 'Fade-in duration'
      value_template: '{{ states.input_number.wakeup_duration.state | int }}'
      unit_of_measurement: 'min'

Home Assistant - groups.yaml

wakeuplight:
  name: Wakeup Light
  control: hidden
  entities:
    - sensor.wakeup_alarm_time
    - sensor.wakeup_start_time_lights
    - sensor.wakeup_fadein_duration
    - input_number.wakeup_hour
    - input_number.wakeup_minutes
    - input_number.wakeup_duration
    - input_boolean.wakeup
    - input_boolean.wakeup_weekends

Home Assistant - automations.yaml

- id: wakeup_light_bedroom
  alias: Wakeup light bedroom
  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

Sources: