renemarc/home-assistant-config

View on GitHub
automations/areas/ceiling_schedule.yaml

Summary

Maintainability
Test Coverage
#
# Schedule ceiling wash lights based on LIFX Cloud scenes
#
# Unless an effect is manually selected, this scheduler will do its thing.
#
# @subscribe input_boolean.nap_mode
# @subscribe input_boolean.night_mode
# @subscribe input_boolean.scene_mode
# @subscribe input_select.ceiling_effect
#
# @publish variable.ceiling_effect
#
# @see /automations/areas/ceiling_effect.yaml
#
# @link https://api.developer.lifx.com/docs/activate-scene
#
- id: ceiling_schedule
  alias: "Ceiling SCHEDULE"

  trigger:
    # Every hour on the hour.
    - platform: time_pattern
      minutes: 00
      seconds: 00

    # When Home Assistant starts.
    - platform: homeassistant
      event: start

    # When an item which was off comes back online.
    - platform: state
      entity_id:
        - light.bedroom
        - light.lounge
        - light.middle
      to: 'on'

    # When scenes are disabled.
    - platform: state
      entity_id: input_boolean.scene_mode
      to: 'off'

    # When the selected effect is switched to default.
    - platform: state
      entity_id: input_select.ceiling_effect
      to: "Auto"

  condition:
    # If automation was not trigered lately (debounce).
    - condition: template
      value_template: >-
        {% set current = as_timestamp(utcnow()) %}
        {% set last_triggered = as_timestamp(states.automation.ceiling_schedule.attributes.last_triggered) | float %}

        {{ current - last_triggered > 1}}

    # If ceiling wash lights are on.
    - condition: state
      entity_id: group.ceiling_lights
      state: 'on'

    # If the selected effect is the default.
    - condition: state
      entity_id: input_select.ceiling_effect
      state: "Auto"

    # If no scenes are enabled.
    - condition: state
      entity_id: input_boolean.scene_mode
      state: 'off'

    # It it's not during the night mode.
    - condition: state
      entity_id: input_boolean.night_mode
      state: 'off'

    # It it's not during the nap mode.
    - condition: state
      entity_id: input_boolean.nap_mode
      state: 'off'

  action:
    # Update the ceiling effect variable.
    - service: variable.set_variable
      data:
        variable: ceiling_effect
        value_template: >-
          {% set hour = states('sensor.time').split(':')[0] %}
          {% set schedule = {
            '00': 'Warming',
            '01': 'Warming',
            '02': 'Northern Lights',
            '03': 'Northern Lights',
            '04': 'Northern Lights',
            '05': 'Relaxing',

            '06': 'Serene',
            '07': 'Soothing',
            '08': 'Relaxing',
            '09': 'Focusing',
            '10': 'Mellow',
            '11': 'Serene',

            '12': 'Focusing',
            '13': 'Soothing',
            '14': 'Relaxing',
            '15': 'Mellow',
            '16': 'Powerful',
            '17': 'Celebrations',

            '18': 'Blissful',
            '19': 'Tranquil',
            '20': 'Intense',
            '21': 'Cheerful',
            '22': 'Soothing',
            '23': 'Tranquil',
          } %}

          {{ schedule[hour] }}