From 93ad678ea0aa8f739de236d310cfa9b7e4ca29ec Mon Sep 17 00:00:00 2001 From: "J.P. Krauss" Date: Fri, 6 May 2022 10:45:15 -0700 Subject: [PATCH] Update Presence Automations --- packages/presence/presence.yaml | 236 +++++++++++++++++++++++++++++++- 1 file changed, 235 insertions(+), 1 deletion(-) diff --git a/packages/presence/presence.yaml b/packages/presence/presence.yaml index bdc707a..4afa4ea 100644 --- a/packages/presence/presence.yaml +++ b/packages/presence/presence.yaml @@ -53,4 +53,238 @@ device_tracker: jen_iphone_bt_entry: monitor/entry/jen_iphone/device_tracker # ----------------------------------------------------------------------------- -# Automations for Presence Detection \ No newline at end of file +# Extended Presence States +input_select: + jp_presence_state: + name: J.P. + options: + - Home + - Just Arrived + - Just Left + - Away + - Extended Away + + jen_presence_state: + name: Jen + options: + - Home + - Just Arrived + - Just Left + - Away + - Extended Away + + house_presence_state: + name: House Occupancy + options: + - Home + - Just Arrived + - Just Left + - Away + - Extended Away + +# ----------------------------------------------------------------------------- +# Extended Presence Sensors +binary_sensor: + + # Home / Not-Home Sensors + - platform: template + sensors: + jp_presence: + friendly_name: J.P. + device_class: presence + value_template: "{{ is_state('person.jpk', 'home') }}" + + jen_presence: + friendly_name: Jen + device_class: presence + value_template: "{{ is_state('person.jen', 'home') }}" + + house_presence: + friendly_name: House + device_class: presence + value_template: >- + {{ is_state('person.jpk', 'home') + or is_state('person.jen', 'home') }} + +sensor: + + # Extended Presence Status + - platform: template + sensors: + jp_presence_status: + value_template: '{{ states.input_select.jp_presence_state.state }}' + friendly_name_template: 'J.P. Presence Status' + + - platform: template + sensors: + jen_presence_status: + value_template: '{{ states.input_select.jen_presence_state.state }}' + friendly_name_template: 'Jen Presence Status' + + - platform: template + sensors: + house_presence_status: + value_template: '{{ states.input_select.house_presence_state.state }}' + friendly_name_template: 'House Presence Status' + +# ----------------------------------------------------------------------------- +# Automations for Presence Detection +automation: + + # Run Bluetooth Arrival Scan at Home Assistant startup + - alias: Startup Arrival Scan + trigger: + - platform: homeassistant + event: start + mode: queued + action: + - service: mqtt.publish + data: + topic: monitor/scan/arrive + payload: scan + + # Handle Just Arrived/Just Left State Transitions + - alias: Mark Person as Just Arrived + trigger: + - platform: state + entity_id: person.jpk + to: 'home' + - platform: state + entity_id: person.jen + to: 'home' + - platform: state + entity_id: binary_sensor.house_presence + to: 'on' + mode: queued + action: + - service: input_select.select_option + data_template: + entity_id: > + {% if trigger.entity_id == 'person.jpk' %} + input_select.jp_presence_state + {% elif trigger.entity_id == 'person.jen' %} + input_select.jen_presence_state + {% elif trigger.entity_id == 'binary_sensor.house_presence' %} + input_select.house_presence_state + {% else %} + {% endif %} + option: > + {% if trigger.entity_id == 'person.jpk' %} + {% if states.input_select.jp_presence_state == 'Just Left' %} + Home + {% else %} + Just Arrived + {% endif %} + {% elif trigger.entity_id == 'person.jen' %} + {% if states.input_select.jen_presence_state == 'Just Left' %} + Home + {% else %} + Just Arrived + {% endif %} + {% elif trigger.entity_id == 'binary_sensor.house_presence' %} + {% if states.input_select.house_presence_state == 'Just Left' %} + Home + {% else %} + Just Arrived + {% endif %} + {% else %} + {% endif %} + + - alias: Mark Person as Just Left + trigger: + - platform: state + entity_id: person.jpk + from: 'home' + - platform: state + entity_id: person.jen + from: 'home' + - platform: state + entity_id: binary_sensor.house_presence + from: 'on' + mode: queued + action: + - service: input_select.select_option + data_template: + entity_id: > + {% if trigger.entity_id == 'person.jpk' %} + input_select.jp_presence_state + {% elif trigger.entity_id == 'person.jen' %} + input_select.jen_presence_state + {% elif trigger.entity_id == 'binary_sensor.house_presence' %} + input_select.house_presence_state + {% else %} + {% endif %} + option: 'Just Left' + + # Handle Timed State Changes to Home/Away/Extended Away + - alias: Mark Person as Home + trigger: + - platform: state + entity_id: input_select.jp_presence_state + to: 'Just Arrived' + for: + minutes: 5 + - platform: state + entity_id: input_select.jen_presence_state + to: 'Just Arrived' + for: + minutes: 5 + - platform: state + entity_id: input_select.house_presence_state + to: 'Just Arrived' + for: + minutes: 5 + mode: queued + action: + - service: input_select.select_option + data_template: + entity_id: '{{ trigger.entity_id }}' + option: 'Home' + + - alias: Mark Person as Away + trigger: + - platform: state + entity_id: input_select.jp_presence_state + to: 'Just Left' + for: + minutes: 5 + - platform: state + entity_id: input_select.jen_presence_state + to: 'Just Left' + for: + minutes: 5 + - platform: state + entity_id: input_select.house_presence_state + to: 'Just Left' + for: + minutes: 5 + mode: queued + action: + - service: input_select.select_option + data_template: + entity_id: '{{ trigger.entity_id }}' + option: 'Away' + + - alias: Mark Person as Extended Away + trigger: + - platform: state + entity_id: input_select.jp_presence_state + to: 'Away' + for: + hours: 24 + - platform: state + entity_id: input_select.jen_presence_state + to: 'Away' + for: + hours: 24 + - platform: state + entity_id: input_select.house_presence_state + to: 'Away' + for: + hours: 24 + mode: queued + action: + - service: input_select.select_option + data_template: + entity_id: '{{ trigger.entity_id }}' + option: 'Extended Away'