blueprint:
  name: dimmer_control_4_magic_cube_and_other_things - 2023-12-07
  author: SirGoodenough
  domain: script
  homeassistant:
    min_version: 2023.8.0
  source_url: https://github.com/SirGoodenough/HA_Blueprints/blob/master/Scripts/dimmer_control_4_magic_cube.yaml
  description: "This script was invented to be used with my Magic Cube Blueprints.
    I released it as a standalone Script Blueprint because I saw others struggling
    with how so convert cube rotation to light dimming in other places.\n\nTo use
    this blueprint, install it from the source in the normal way. After that add your
    unique name for the Script, and add an icon and change the entity_id if desired.
    You should only need to run the blueprint once, as you will be calling the unique
    name/entity you added above to use the code. This script has 2 inputs in the Blueprint
    screen to set the sensitivity of the rotation action and the minimum bbrightness
    level for your lights. If the rotation is too sensitive the lights will change
    from dim to bright in too short of a rotation so make it less sensitive. If the
    lights flicker at low brightness, raise the minimum brightness level to stop it.
    The data is sent to the generated script to control the action live at each use.
    You only need to ececute this blueprint generator script if you wish to change
    in input values or create another dimmer script.\n\nThis requires you call this
    script wth 2 data values. One is a positive or negative number between 360 and
    -360 that in the original, represents the input angle from the cube movement.
    The other data point is the entity of the light or light group that you are trying
    to control.\n***NOTE:*** the trigger variable below will be different for Z2M
    and ZHA and others. Look at my documentation for that cube integration help if
    you need it.\n\n## \U0001F5FFNotice\U0001F46E\U0001F3FF‍♂️:\n* Copies of the original
    Blueprint that were converted via the 'Take Control' feature or other means are
    officially not supported by me.\n\n* I may or may not be able to support you when
    you have a problem after you make changes to my code, as some of the code is no
    longer mine.\n\n* I & my license also require attribution as a link back to the
    original should you use this code in your own creation.\n\n* [Here is a link to
    my license & the original github post](https://github.com/SirGoodenough/HA_Blueprints?tab=License-1-ov-file)
    expected to be followed & referenced as attribution should you use this code elsewhere.\n\n
    \ Sample call / use of this script for ZHA:\n    ```yaml\n    - action: script.cube_dimmer_control_bp\n
    \     data:\n        angle: '{{ trigger.event.data.args.relative_degrees | default(0.1)
    | float(0.2) }}'\n        light: light.grp_studio\n    ```\n\n  Sample call /
    use of this script for Z2M:\n    ```yaml\n    - action: script.cube_dimmer_control_bp\n
    \     data:\n        angle: '{{ trigger.payload_json.action_angle | default(0.1)
    | float(0.2) }}'\n        light: light.grp_kitch\n    ```\n"
  input:
    sensi:
      name: Sensitivity of the cube rotational motion.
      description: 'This sets how ''fast'' the changes happen compared to how far
        you move the cube. Higher percentage is faster, lower is slower.

        I have found that 40% is a good number, but you can adjust as desired.

        '
      default: 40
      selector:
        number:
          min: 1.0
          max: 200.0
          step: 1.0
          mode: box
    min_b:
      name: Minimum Brighness
      description: 'This sets the lowest brightness it will set before it turns off.
        Some bulbs react poorly below a certain percent, you can adjust that here.

        I have found that 10% is a good number, but you can adjust as desired.

        '
      default: 10
      selector:
        number:
          min: 5.0
          max: 90.0
          step: 1.0
          mode: slider
    transition_i:
      name: Transition Input
      description: Seconds to take to change light one level to another
      default: []
      selector:
        number:
          min: 0.0
          max: 1800.0
          step: 0.5
          mode: slider
fields:
  angle:
    name: Angle to represent new brightness
    description: ' This is the new angle to be used in the calculation for Brightness'
    required: true
    example: '-120.7'
    default: -120.7
    selector:
      number:
        min: -360
        max: 360
        step: 0.1
        mode: box
  light:
    name: Light entity to control
    description: Add the light entity you want to control here
    required: true
    example: light.grp_studio
    default: light.grp_studio
    selector:
      entity:
        multiple: false
        filter:
          domain: light
  transition:
    name: Transition Field
    description: Seconds to take to change light one level to another
    required: false
    example: '4'
    selector:
      number:
        min: 0
        max: 1800
        step: 0.5
variables:
  sensi_var: !input sensi
  sens_d: '{{ sensi_var / 100 | default( 0.4 ) }}'
  min_b_var: !input min_b
  bright_p: "{% set step_size = angle * sens_d %}\n  {# Get brightness as a percent.
    #}\n{% set cb = (state_attr( light, 'brightness') | float(10) / 255.0) * 100.0
    %} {% set new_brightness = cb | int(10) + step_size %} {% if new_brightness <
    5 %}\n  {# If it gets really low set to 0. Adjust if needed for your lights. #}\n
    \ 0\n{% elif new_brightness <= min_b_var %}\n  {# If it's not quite 0, set to
    a minimum working brightness. #}\n  {{ min_b_var }}\n{% elif 91 <= new_brightness
    < (90 + step_size) %}\n  {# If it's almost full brightness, set to 100%. #}\n
    \ 100\n{% else %}\n  {# Send actual calculated value. #}\n{{ new_brightness }}
    {% endif %}"
  trans_i: !input transition_i
  transition_var: "{% if is_number(transition | default('none')) %}\n  {{ transition
    }}\n{% elif is_number(trans_i | default('none')) %}\n  {{ trans_i }}\n{% else
    %}\n  none\n{% endif %}"
sequence:
- if:
  - alias: turn the light & set brightness
    condition: template
    value_template: '{{ is_number(transition_var) }}'
  then:
  - alias: Transition number IS available
    action: light.turn_on
    data:
      entity_id: '{{ light }}'
      brightness_pct: '{{ bright_p }}'
      transition: '{{ transition_var }}'
  else:
  - alias: NO transition number available
    action: light.turn_on
    data:
      entity_id: '{{ light }}'
      brightness_pct: '{{ bright_p }}'
icon: mdi:lightbulb-on-50
mode: queued
max: 10
