Instantly share code, notes, and snippets.
Created
April 14, 2024 00:32
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save x-magic/2cd6623424bac6a2805828e6f5c46008 to your computer and use it in GitHub Desktop.
Originally from: https://github.com/bbcontrol/ha-snippets/blob/main/blueprints/single_button_dim_switch.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Originally from: https://github.com/bbcontrol/ha-snippets/blob/main/blueprints/single_button_dim_switch.yaml | |
blueprint: | |
name: Single Button Dim Switch | |
description: >- | |
Switch a light on/off by pressing a button. | |
Dim it up/down (depending on the current state) by holding the same button. | |
domain: automation | |
input: | |
button: | |
name: Push button | |
description: | |
Entity representing a physical push button (which restores its position | |
when released). It must emit a square signal on press. | |
selector: | |
entity: | |
domain: binary_sensor | |
light: | |
name: Light | |
selector: | |
entity: | |
domain: light | |
hold_threshold: | |
name: Hold threshold | |
description: Time to consider button is being held, instead of just pushed. | |
default: 0.8 | |
selector: | |
number: | |
min: 0.05 | |
max: 10 | |
step: 0.05 | |
unit_of_measurement: s | |
brightness_threshold: | |
name: Brightness threshold | |
description: If the light brightness is above this value, | |
the light is going to dim down when holding the button. | |
default: 130 | |
selector: | |
number: | |
min: 0 | |
max: 255 | |
brightness_step_pct: | |
name: Brightness step | |
description: Percentage brightness increase/decrease in each step. | |
default: 20 | |
selector: | |
number: | |
min: 1 | |
max: 100 | |
unit_of_measurement: "%" | |
transition_step_length: | |
name: Transition length | |
description: >- | |
Lenght of brightness transition in each step. | |
WARNING: may not be supported by all lights. | |
default: 0 | |
selector: | |
number: | |
min: 0 | |
max: 5 | |
step: 0.01 | |
unit_of_measurement: s | |
delay_step_length: | |
name: Delay length | |
description: >- | |
Length of pause after each step. | |
MUST be larger than Transition length. | |
default: 0.2 | |
selector: | |
number: | |
min: 0.1 | |
max: 5 | |
step: 0.01 | |
unit_of_measurement: s | |
mode: single | |
trigger: | |
- platform: state | |
entity_id: !input button | |
to: "on" | |
action: | |
- variables: | |
brightness_step_pct_positive: !input brightness_step_pct | |
- variables: | |
brightness_step_pct_negative: "{{ brightness_step_pct_positive|int * -1 }}" | |
light: !input light | |
- wait_for_trigger: | |
- platform: state | |
entity_id: !input button | |
to: "off" | |
from: "on" | |
timeout: !input hold_threshold | |
continue_on_timeout: true | |
- choose: | |
- conditions: | |
# when no trigger came (button has not been switched back to | |
# off state during timeout) = button is being held | |
- condition: template | |
value_template: "{{ wait.trigger == none }}" | |
sequence: | |
- choose: | |
- conditions: | |
# when brightness level is high | |
- condition: numeric_state | |
entity_id: !input light | |
attribute: brightness | |
above: !input brightness_threshold | |
sequence: | |
# dim down | |
- repeat: | |
while: | |
- condition: and | |
conditions: | |
- condition: state | |
entity_id: !input button | |
state: "on" | |
- condition: template | |
value_template: "{{ (state_attr(light, 'brightness') or 0) > 0 }}" | |
sequence: | |
- if: | |
- alias: If next step would not turn off the light | |
condition: template | |
value_template: "{{ (state_attr(light, 'brightness') or 0) > brightness_step_pct_positive }}" | |
then: | |
- service: light.turn_on | |
entity_id: !input light | |
data: | |
transition: !input transition_step_length | |
brightness_step_pct: "{{ brightness_step_pct_negative }}" | |
else: | |
- service: light.turn_off | |
entity_id: !input light | |
data: | |
transition: !input transition_step_length | |
- stop: Dimmed to zero | |
- delay: !input delay_step_length | |
default: | |
# otherwise, dim up | |
- repeat: | |
while: | |
- condition: and | |
conditions: | |
- condition: state | |
entity_id: !input button | |
state: "on" | |
- condition: template | |
value_template: "{{ (state_attr(light, 'brightness') or 0) < 255 }}" | |
sequence: | |
- if: | |
- alias: If next step would not turn the light fully on | |
condition: template | |
value_template: "{{ (state_attr(light, 'brightness') or 0) + brightness_step_pct_positive < 255 }}" | |
then: | |
- service: light.turn_on | |
entity_id: !input light | |
data: | |
transition: !input transition_step_length | |
brightness_step_pct: "{{ brightness_step_pct_positive }}" | |
else: | |
- service: light.turn_on | |
entity_id: !input light | |
data: | |
transition: !input transition_step_length | |
- stop: Dimmed to max | |
- delay: !input delay_step_length | |
# when trigger came (button has been released within timeout) | |
default: | |
- service: light.toggle | |
entity_id: !input light | |
data: | |
transition: !input transition_step_length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment