summaryrefslogtreecommitdiffstats
path: root/blueprints/automation/motion/occupancy_tracking.yaml
blob: 4edf52caea312a8081e51992e69129cc269d5807 (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
# This can be used stand-alone or in combination with 'motion_switch' (to allow more complex light-on scenarios, e.g. based on current illumination level)
#   For this reason, 'light on' should be programmed in 'on_action' if necessary
# We can use blueprint using the same entrance/inside sensors, however, this is least robust variant
#   Re-entry events (take a towel go back in) will break automation
#   We can try to prevent by ensuring there is no repeated motions for prolonged time (but, then, blueprint will fail if we do something at entrance)

blueprint:
  name: Occupancy tracking
  description: Tries to track occupancy of single-person places (toilet bathroom)
  domain: automation
  input:
    entrance:
      name: Entrance
      description: This can be door sensor or motion sensor monitoring passage via entrance area
      selector:
        entity:
          domain: binary_sensor
    inside:
      name: Inside
      description: Triggers when there is motion inside (this could be the same like entrance or additional sensor, e.g. we monitor motions as 'inside' and door sensor as 'entrance'). Independent sensors are much more robust
      selector:
        entity:
          domain: binary_sensor
    light:
      name: Light
      selector:
        entity:
          domain: [switch, light]
    occupancy:
      name: Occupancy
      description: State variable (normally created via Helpers) tracking current state
      selector:
        entity:
          domain: input_boolean

    reentry_wait:
      name: Re-entry Wait
      description: Time without motions to handle re-entry events (seconds)
      default: 1
      selector:
        number:
          min: 0
          max: 30
          unit_of_measurement: seconds

    reentry_timeout:
      name: Re-entry Timeout
      description: Timeout to assume re-entry if no 'no-motions' trigger were fired within timeout (seconds)
      default: 10
      selector:
        number:
          min: 1
          max: 300
          unit_of_measurement: seconds

    on_action:
      name: on_action
      description: Actions to perform while occupancy is detected (lights will not be turned on automatically to allow more complex scenarios via motion-switch automation)
      default: []
      selector:
        action:
    off_action:
      name: off_action
      description: Additional actions to perform when place gets free
      default: []
      selector:
        action:

variables:
  reentry_wait: !input reentry_wait

# Lights are still on while we are waiting for re-entry. So, we don't need to allow re-trigger in this case and 'single' should do
mode: single

trigger:
  # Either door is open or motion in the entrance area is detected
  - platform: state
    id: "entrance"
    entity_id: !input entrance
    from: "off"
    to: "on"

  - platform: state
    id: "inside"
    entity_id: !input inside
    from: "off"
    to: "on"

  # Light is turned off
  - platform: state
    id: "light_off"
    entity_id: !input light
    from: "on"
    to: "off"

action:
  - choose:
      # Occupancy goes 'off' if lights are manually (or on timeout) switched off
      #   We can't do the same to turn occupancy 'on' as 'entrance' event might be fired after manually turning lights on (it will be interpreted as leaving)
      #   Will be retriggered back 'on' on any movement inside (so, it is not so critical if something is wrong)
      - conditions:
          - condition: trigger
            id: "light_off"
        sequence:
          - parallel:
              - service: input_boolean.turn_off
                target:
                  entity_id: !input occupancy
              - choose: []
                default: !input "off_action"

      # Occupancy goes 'on' once movement inside is detected (we can't rely on trigger as inside & entrance might coincide)
      - conditions:
#          - condition: trigger
#            id: "inside"
          - condition: state
            entity_id: !input inside
            state: 'on'
          - condition: or
            conditions:
              - condition: state
                entity_id: !input occupancy
                state: 'off'
              - condition: state
                entity_id: !input light
                state: 'off'
        sequence:
          - parallel:
              - service: input_boolean.turn_on
                target:
                  entity_id: !input occupancy
              - choose: []
                default: !input "on_action"

      # Occupancy goes 'off' once entrance triggered while occapncy is 'on' and there are no movements inside for a while
      - conditions:
#          - condition: trigger
#            id: "entrance"
          - condition: state
            entity_id: !input entrance
            state: 'on'
          - condition: state
            entity_id: !input occupancy
            state: 'on'
        sequence:
          - if:
              - condition: template
                value_template: '{{ (reentry_wait | float) > 0 }}'
            then:
              - wait_for_trigger:
                  - platform: state
                    entity_id: !input inside
                    to: 'off'
                    for:
                      seconds: !input reentry_wait
                timeout:
                  seconds: !input reentry_timeout
                continue_on_timeout: false

          - parallel:
              - service: input_boolean.turn_off
                target:
                  entity_id: !input occupancy
              - service: switch.turn_off
                target: 
                  entity_id: !input light
              - choose: []
                default: !input "off_action"

    default: []