renemarc/home-assistant-config

View on GitHub
automations/devices/aurora_schedule.yaml

Summary

Maintainability
Test Coverage
#
# Schedule the Nanoleaf Aurora based on its loaded-in effects
#
# 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.aurora_effect
#
# @publish variable.aurora_brightness
# @publish variable.aurora_effect
#
# @see /automations/devices/aurora_effect.yaml
#
# @link https://forum.nanoleaf.me/docs/openapi
#
- id: aurora_schedule
  alias: "Aurora SCHEDULE"

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

    # When an item which was off or physically disconnected comes back online.
    - platform: state
      entity_id:
        - light.aurora
      from: 'off'

    # 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.aurora_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.aurora_schedule.attributes.last_triggered) | float %}

        {{ current - last_triggered > 1}}

    # If the Aurora is active.
    - condition: state
      entity_id: light.aurora
      state: 'on'

    # If the selected effect is the default.
    - condition: state
      entity_id: input_select.aurora_effect
      state: 'Auto'

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

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

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

  action:
    # Update the Aurora effect variable with the desired effect name.
    - service: variable.set_variable
      data:
        variable: aurora_effect
        value_template: >-
          {% set hour = states('sensor.time').split(':')[0] %}
          {% set schedule = {
            '00': 'Be Productive',
            '01': 'Canada Day',
            '02': 'Color Burst',
            '03': 'Fifty shades',
            '04': 'Flames',
            '05': 'Forest',

            '06': 'Inner Peace',
            '07': 'Movie Night',
            '08': 'Nemo',
            '09': 'Northern Lights',
            '10': 'Rain',
            '11': 'Retro Future',

            '12': 'Romantic',
            '13': 'Serve warm and chilled',
            '14': 'Snowfall',
            '15': 'Stranger Things',
            '16': 'Synthwave',
            '17': 'Tron Legacy',

            '18': 'Tron2',
            '19': 'Vibrant Sunrise',
            '20': 'Inner Peace',
            '21': 'Nemo',
            '22': 'Flames',
            '23': 'Retro Future',
          } %}

          {{ schedule[hour] }}

    # Update the Aurora brightness variable with the desired level.
    - service: variable.set_variable
      data:
        variable: aurora_brightness
        value_template: >-
          {% set hour = states('sensor.time').split(':')[0] %}
          {% set schedule = {
            '00': '35',
            '01': '30',
            '02': '25',
            '03': '20',
            '04': '20',
            '05': '25',

            '06': '45',
            '07': '60',
            '08': '75',
            '09': '80',
            '10': '85',
            '11': '95',

            '12': '100',
            '13': '100',
            '14': '95',
            '15': '90',
            '16': '85',
            '17': '80',

            '18': '75',
            '19': '70',
            '20': '65',
            '21': '60',
            '22': '55',
            '23': '50',
          } %}

          {{ schedule[hour] }}