summaryrefslogtreecommitdiffstats
path: root/blueprints/automation/lights/light_button.yaml
blob: 7aeb0ab2c6f27b4b9780fdeffafa843dc53f5928 (plain)
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
blueprint:
  name: Light Button
  description: Button to control smart bulb and perform other actions when bulb turns on/off, plus additional actions on double-click and long-press (select *_action entity)
  domain: automation
  input:
    button:
      name: button
      description: controller
      selector:
        entity:
          domain: sensor
    light:
      name: light
      description: another switch, smart bulb, or power socket
      selector:
        entity:
          domain: light
    temperature:
      name: Temperature
      description: Color Temperature
      default: 6500
      selector:
        number:
          min: 2000
          max: 6500
    on_click:
      name: on_click
      description: Additional actions to perform while turning on master light
      default: []
      selector:
        action:
    off_click:
      name: off_click
      description: Additional actions to perform while turning off master light
      default: []
      selector:
        action:
    dblclick:
      name: dblclick
      description: Actions to perform on double click
      default: []
      selector:
        action:
    longclick:
      name: longclick
      description: Actions to perform on long-press (single action)
      default: []
      selector:
        action:
    hold:
      name: hold
      description: Actions to perform on hold (iterative until released)
      default: []
      selector:
        action:
    hold_delay:
      name: Hold Delay
      description: Delay between calling hold actions (milliseconds)
      default: 500
      selector:
        number:
          min: 100
          max: 10000

    state:
      name: State
      description: Indicates if button is pressed and we currently changing the brightness
      default: None
      selector: 
        entity:
          domain: input_boolean

trigger:
  - platform: state
    id: button
    entity_id: !input "button"
    to:
      - "single"
      - "double"
      - "hold"


variables:
   state: !input state
   hold: !input hold

# Can't do 'restart' since it will break wait_for_trigger
mode: single

action:
- choose:
  - conditions:
    - condition: trigger
      id: button
    sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ trigger.to_state.state == "single" }}'
        sequence:
          - if:
              - condition: state
                entity_id: !input light
                state: 'on'
            then:
              - parallel:
                - service: light.turn_off
                  target:
                    entity_id: !input light
                - choose: []
                  default: !input "off_click"
            else:
              - parallel:
                - service: light.turn_on
                  data:
                    brightness_pct: 100
                    kelvin: !input temperature
                  target:
                    entity_id: !input light
                - choose: []
                  default: !input "on_click"

      - conditions:
        - condition: template
          value_template: '{{ trigger.to_state.state == "double" }}'
        sequence:
          - choose: []
            default: !input "dblclick"

      - conditions:
        - condition: template
          value_template: '{{ trigger.to_state.state == "hold" }}'
        sequence:
          - choose: []
            default: !input "longclick"

          - if:
              - condition: template
                value_template: '{{ hold | length > 0 }}'
            then:
              - if:
                  - condition: template
                    value_template: '{{ state }}'
                then:
                  - service: input_boolean.turn_on
                    target:
                      entity_id: !input state

              - repeat:
                  until:
                    - condition: template
                      value_template: '{{ wait.trigger != None }}'
                  sequence:
                  - choose: []
                    default: !input "hold"

                  - wait_for_trigger:
                      - platform: state
                        entity_id: !input button
                        to: 'release'
                    timeout: 
                      milliseconds: !input hold_delay
                    continue_on_timeout: true

              - if:
                  - condition: template
                    value_template: '{{ state }}'
                then:
                  - service: input_boolean.turn_off
                    target:
                      entity_id: !input state