blueprint:
  name: Smart Light Dimmer V3.0
  description: 'Version 3.0  ! BETA !

    Switch or dim lights based on the value of a light sensor.

    Light color and temperature can be specified (fixed value).

    Settings for brightness, temperature and color above and below min and max values
    of the sensor can be set.

    '
  source_url: https://gist.github.com/Twanne/56791e1917c751de7a72b16ee5e067cd
  domain: automation
  input:
    schedule_start:
      name: Schedule start time
      description: Automation only runs after this time.
      selector:
        time: {}
    schedule_stop:
      name: Schedule stop time
      description: Automation does not run after this time.
      selector:
        time: {}
    schedule_days:
      name: Run on these days
      description: 'Days on which the automation will run.

        Write days in short form, seperated by punctuation marks and/or spaces.

        (i.e.: mon, tue, wed,...)

        '
      selector:
        text: {}
    light_sensor_entity:
      name: Light Sensor
      selector:
        entity:
          domain: sensor
          device_class: illuminance
          multiple: false
    max_brightness_value:
      name: Maximum ambient light value
      description: Maximum ambient light value measured.
      default: 500
      selector:
        number:
          min: 0.0
          max: 1000.0
          step: 10.0
          unit_of_measurement: lx
          mode: slider
    min_brightness_value:
      name: Minimum ambient light value
      description: Minimum ambient light value measured.
      default: 0
      selector:
        number:
          min: 0.0
          max: 1000.0
          step: 10.0
          unit_of_measurement: lx
          mode: slider
    light_value_1:
      name: Dimming value 1
      description: Brightness of the light at maximum ambient light.
      default: 0
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
          unit_of_measurement: '%'
    light_value_2:
      name: Dimming value 2
      description: Brightness of the light at minimum ambient light.
      default: 100
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
          unit_of_measurement: '%'
    light_brightness_over_max:
      name: Brightness when over max ambient.
      description: Brightness of the light when the sensor measures over the maximum
        ambient light value.
      default: 0
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
          unit_of_measurement: '%'
    light_temp_over_max:
      name: Light temperature when over max ambient.
      description: Temperature of the light when over maximum ambient light value.
      default: 2000
      selector:
        color_temp: {}
    light_color_over_max:
      name: Light color when over max ambient.
      description: Color of the light when over maximum ambient light value.
      selector:
        color_rgb: {}
    light_brightness_under_min:
      name: Brightness when under min ambient.
      description: Brightness of the light when the sensor measures under the minimum
        ambient light value.
      default: 100
      selector:
        number:
          min: 0.0
          max: 100.0
          step: 1.0
          mode: slider
          unit_of_measurement: '%'
    light_temp_under_min:
      name: Light temperature when under min ambient.
      description: Temperature of the light when under minimum ambient light value.
      default: 2000
      selector:
        color_temp: {}
    light_color_under_min:
      name: Light color when under min ambient.
      description: Color of the light when over under minimum light value.
      selector:
        color_rgb: {}
    light_temp:
      name: Light temperature
      description: Temperature of the light when between minimum and maximum measured
        light values.
      default: 2000
      selector:
        color_temp: {}
    light_color:
      name: Light color
      description: Color of the light when between minimum and maximum measured light
        values.
      selector:
        color_rgb: {}
    target_light:
      name: Target lights
      description: which lights do you want to control?
      selector:
        target:
          entity:
            domain: light
mode: single
variables:
  light_sensor: !input light_sensor_entity
  maxB: !input max_brightness_value
  minB: !input min_brightness_value
  light1: !input light_value_1
  light2: !input light_value_2
  slope: '{{ ( light1 - light2 ) / ( maxB - minB ) }}'
  constant: '{{ light1 - ( slope * maxB ) }}'
  days: !input schedule_days
trigger:
  platform: state
  entity_id: !input light_sensor_entity
condition:
- condition: numeric_state
  entity_id: !input light_sensor_entity
  above: !input min_brightness_value
- condition: time
  after: !input schedule_start
  before: !input schedule_stop
- condition: template
  value_template: '{{ now().strftime(''%a'') | lower in days }}'
action:
- choose:
  - conditions:
    - condition: numeric_state
      entity_id: !input light_sensor_entity
      above: maxB
    sequence:
    - service: light.turn_on
      data:
        brightness_pct: !input light_brightness_over_max
        color_temp: !input light_temp_over_max
        color_rgb: !input light_color_over_max
      target: !input target_light
  - conditions:
    - condition: numeric_state
      entity_id: !input light_sensor_entity
      below: minB
    sequence:
    - service: light.turn_on
      data:
        brightness_pct: !input light_brightness_under_min
        color_temp: !input light_temp_under_min
        color_rgb: !input light_color_under_min
      target: !input target_light
  - conditions:
    - condition: numeric_state
      entity_id: !input light_sensor_entity
      below: maxB
      above: minB
    sequence:
    - service: light.turn_on
      data:
        brightness_pct: '{{ (( slope_brightness * states(light_sensor)|int ) + constant_brightness)|round
          }}'
        color_temp: '{{ (( slope_temperature * states(light_sensor)|int ) + constant_temperature)|round
          }}'
        color_rgb: !input light_color
      target: !input target_light
