/
dimmer_control_4_magic_cube.yaml
192 lines (176 loc) 路 6.15 KB
/
dimmer_control_4_magic_cube.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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.
To 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.
This 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.
***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.
Sample call / use of this script for ZHA:
```yaml
- service: script.cube_dimmer_control_bp
data:
angle: '{{ trigger.event.data.args.relative_degrees | default(0.1) | float(0.2) }}'
light: light.grp_studio
```
Sample call / use of this script for Z2M:
```yaml
- service: script.cube_dimmer_control_bp
data:
angle: '{{ trigger.payload_json.action_angle | default(0.1) | float(0.2) }}'
light: light.grp_kitch
```
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
max: 200
step: 1
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
max: 90
step: 1
mode: slider
transition_i:
name: Transition Input
description: Seconds to take to change light one level to another
default: []
selector:
number:
min: 0
max: 1800
step: 0.5
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:
# Get the sensitivity input
sensi_var: !input sensi
# Convert raw percentage to decimal for calculations. Default to 0.4.
sens_d: "{{ sensi_var / 100 | default( 0.4 ) }}"
# Get the minimum brightness input
min_b_var: !input min_b
# Set new Brightness
bright_p: >-
{% set step_size = angle * sens_d %}
{# Get brightness as a percent. #}
{% set cb = (state_attr( light, 'brightness') | float(10) / 255.0) * 100.0 %}
{% set new_brightness = cb | int(10) + step_size %}
{% if new_brightness < 5 %}
{# If it gets really low set to 0. Adjust if needed for your lights. #}
0
{% elif new_brightness <= min_b_var %}
{# If it's not quite 0, set to a minimum working brightness. #}
{{ min_b_var }}
{% elif 91 <= new_brightness < (90 + step_size) %}
{# If it's almost full brightness, set to 100%. #}
100
{% else %}
{# Send actual calculated value. #}
{{ new_brightness }}
{% endif %}
# Get the transition input if there is one
trans_i: !input transition_i
# Set the transition value if the operator requests it
transition_var: >-
{% if is_number(transition | default('none')) %}
{{ transition }}
{% elif is_number(trans_i | default('none')) %}
{{ trans_i }}
{% else %}
none
{% endif %}
sequence:
- if:
- alias: turn the light & set brightness
condition: template
value_template: "{{ is_number(transition_var) }}"
then:
- alias: "Transition number IS available"
service: light.turn_on
data:
entity_id: '{{ light }}'
brightness_pct: '{{ bright_p }}'
transition: '{{ transition_var }}'
else:
- alias: "NO transition number available"
service: light.turn_on
data:
entity_id: '{{ light }}'
brightness_pct: '{{ bright_p }}'
icon: mdi:lightbulb-on-50
mode: queued
max: 10