summaryrefslogtreecommitdiffstats
path: root/blueprints/automation/lights/light_button.yaml
blob: 34116bc7727d8a591837902400356a188b568773 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
    on_light:
      name: On Light
      description: Turn light on automatically or allow complex scenarios using 'on_click'
      default: 'yes'
      selector:
        select:
          options:
            - label: Turn on lights automatically
              value: 'yes'
            - label: Manage lights using 'on_click' handler
              value: 'no'
    off_light:
      name: Off Light
      description: Turn light off automatically or allow complex scenarios using 'off_click'
      default: 'yes'
      selector:
        select:
          options:
            - label: Turn off lights automatically
              value: 'yes'
            - label: Manage lights using 'off_click' handler
              value: 'no'
    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
   light_on: !input on_light
   light_off: !input off_light

# 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:
                - if:
                    - condition: template
                      value_template: '{{ light_off == "yes" }}'
                  then:
                    - service: light.turn_off
                      target:
                        entity_id: !input light
                - choose: []
                  default: !input "off_click"
            else:
              - parallel:
                - if:
                    - condition: template
                      value_template: '{{ light_on == "yes" }}'
                  then:
                    - 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