summaryrefslogtreecommitdiffstats
path: root/blueprints/automation/motion/motion_switch.yaml
blob: 31ba62c3890a82e5b2da6f69f6c5742ee2648785 (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
blueprint:
  name: Motion-activated Switch
  description: Turn on a light when motion is detected and surroundings are dark
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
#          device_class: motion

    door_entity:
      name: Door Contact
      description: Additionally trigger if door opens while no motion detected
      default: {}
      selector:
        entity:
          domain: binary_sensor
#          device_class: contact

    illumination_test:
      name: Illumination Test
      description: Select how to check if existing illumination is sufficient
      default: sun
      selector:
        select:
          options:
            - label: Illuminance Sensor
              value: sensor
            - label: Sun Elevation
              value: sun
            - label: Always turn on lights
              value: none
    illuminance_entity:
      name: Illuminance Sensor
      description: Sensor providing information about illumination in the room (keep empty to rely on sun position)
      selector:
        entity:
          domain: sensor
          device_class: illuminance
    lux_threshold:
      name: LUX threshold
      description: Threshold bellow which we consider that lighting is needed
      default: 40
      selector:
        number:
          min: 10
          max: 1000
          unit_of_measurement: lux
    sun_elevation:
      name: Sun Elevation
      description: Sun elevation (suggested between 0 and -6) when dusks settles and we need to turn on lights)
      default: 0
      selector:
        number:
          min: -30
          max: 30
          unit_of_measurement: °


    light_target:
      name: Light Target
      default: []
      selector:
        entity:
          multiple: true
          domain: [switch, light]
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 120
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds

    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:



variables:
  lux_threshold: !input lux_threshold
  illuminance_entity: !input illuminance_entity
  test: !input illumination_test

  current_lux: '{{ (states(illuminance_entity) | float) }}'

# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent

trigger:
  - platform: state
    id: "turn_on"
    entity_id: !input motion_entity
    from: "off"
    to: "on"
  - platform: state
    id: "door"
    entity_id: !input door_entity
    from: "off"
    to: "on"
  - platform: state
    id: "turn_off"
    entity_id: !input motion_entity
    from: "on"
    to: "off"
    for:
      seconds: !input no_motion_wait

action:
  - choose:
      - conditions:
          - condition: or
            conditions:
             - condition: trigger
               id: "turn_on"
             - condition: and
               conditions:
                 - condition: trigger
                   id: "door"
                 - condition: state
                   entity_id: !input motion_entity
                   state: 'off'
        sequence:
          - choose:
              - conditions: 
                  - condition: template
                    value_template: '{{ test == "sensor" }}'
                sequence:
                  - if:
                      - condition: template
                        value_template: '{{ (states(illuminance_entity) | float) < lux_threshold }}'
                    then:
                      - choose: []
                        default: !input "on_click"
                      - service: switch.turn_on
                        target: 
                          entity_id: !input light_target

              - conditions: 
                  - condition: template
                    value_template: '{{ test == "sun" }}'
                sequence:
                  - if:
                      - condition: numeric_state
                        entity_id: sun.sun
                        attribute: elevation
                        below: !input sun_elevation
                    then:
                      - choose: []
                        default: !input "on_click"
                      - service: switch.turn_on
                        target:
                          entity_id: !input light_target
            default:
              - choose: []
                default: !input "on_click"
              - service: switch.turn_on
                target: 
                   entity_id: !input light_target

      - conditions:
          - condition: trigger
            id: "turn_off"
        sequence:
          - choose: []
            default: !input "off_click"
          - service: switch.turn_off
            target: 
              entity_id: !input light_target
    default: []