Skip to content

Instantly share code, notes, and snippets.

@tetele
Created February 6, 2024 19:20
Show Gist options
  • Save tetele/ecb0e752d9c5fc1915471211edd79620 to your computer and use it in GitHub Desktop.
Save tetele/ecb0e752d9c5fc1915471211edd79620 to your computer and use it in GitHub Desktop.
Wasp in a box template sensor and automation blueprint

A wasp-in-a-box solution including a template sensor and an automation blueprint to automatically turn lights on/off.

Installation

  1. Copy the contents of wiab-sensor.yaml to your configuration.yaml to create a binary_sensor for occupancy
    • replace binary_sensor.my_door with your door sensor or sensor group (if the room has multiple doors)
    • replace binary_sensor.my_motion_sensor with your motion sensor
    • replace the value 70 with the cooldown period of your motion sensor, in seconds (i.e. the time it takes from when the motion stops to when the sensor switches to off). Use a stopwatch to get it
  2. Use the blueprint in wiab-automation.yaml to create an automation that turns the light on/off
blueprint:
name: Wasp-in-a-box light control
description: Turn on light based on the wasp-in-a-box paradigm. Opening the box and detecting the wasp should turn on the light. Deciding that the wasp is no longer in the box and reverting to a closed box with no motion and no presumed wasp should turn off the light.
domain: automation
author: tetele
input:
wasp_sensor:
name: Activity sensor
description: A binary sensor indicating whether there is activity in the room.
selector:
entity:
domain: binary_sensor
wasp_sensor_cooldown:
name: Activity sensor cooldown
description: The time it takes after activity is no longer detected until the activity sensor returns to "clear".
selector:
duration:
default:
seconds: 2
minutes: 0
hours: 0
box_sensor:
name: Open box sensor
description: A binary sensor indicating whether the box is open.
selector:
entity:
domain:
- binary_sensor
- input_boolean
wasp_in_a_box_sensor:
name: Wasp in box sensor
description: A computed sensor indicating whether the wasp is in the box.
selector:
entity:
domain: binary_sensor
light_entity:
name: Light entity
description: The light to toggle when the wasp is definitely in the box.
selector:
entity:
domain: light
extra_conditions:
name: Extra conditions
description: Conditions required for the automation to run.
selector:
condition:
default: []
mode: restart
trigger:
- id: wasp_detected
platform: state
entity_id: !input wasp_sensor
from: "off"
to: "on"
- id: wasp_cleared
platform: state
entity_id: !input wasp_sensor
from: "on"
to: "off"
- id: box_opened
platform: state
entity_id: !input box_sensor
from: "off"
to: "on"
- id: box_opened_long_ago
platform: state
entity_id: !input box_sensor
from: "off"
to: "on"
for: !input wasp_sensor_cooldown
- id: box_closed
platform: state
entity_id: !input box_sensor
from: "on"
to: "off"
- id: box_closed_long_ago
platform: state
entity_id: !input box_sensor
from: "on"
to: "off"
for: !input wasp_sensor_cooldown
- id: wasp_left_box_long_ago
platform: state
entity_id: !input wasp_in_a_box_sensor
from: "on"
to: "off"
for: !input wasp_sensor_cooldown
condition: !input extra_conditions
variables:
wasp_sensor: !input wasp_sensor
wasp_sensor_cooldown_dict: !input wasp_sensor_cooldown
wasp_sensor_cooldown: >
{{ wasp_sensor_cooldown_dict.hours|float(0)*3600 + wasp_sensor_cooldown_dict.minutes|float(0)*60 + wasp_sensor_cooldown_dict.seconds|float(0) }}
action:
- alias: "what triggered the action"
choose:
- conditions:
# Opening the box and detecting the wasp should turn on the light.
- condition: trigger
id:
- box_opened
- wasp_detected
sequence:
- alias: "turn on the light"
service: light.turn_on
target:
entity_id: !input light_entity
# Deciding that the wasp is no longer in the box and reverting to a closed box with no motion and no presumed wasp should turn off the light.
- conditions:
- condition: trigger
id: wasp_left_box_long_ago
- condition: state
entity_id: !input wasp_sensor
state: "off"
- condition: template
value_template: "{{ as_timestamp(now())|float - states[wasp_sensor].last_changed|as_timestamp|float > wasp_sensor_cooldown }}"
sequence:
- alias: "turn off the light"
service: light.turn_off
target:
entity_id: !input light_entity
- conditions:
- condition: trigger
id: box_closed_long_ago
- condition: state
entity_id: !input wasp_sensor
state: "off"
- condition: template
value_template: "{{ as_timestamp(now())|float - states[wasp_sensor].last_changed|as_timestamp|float > wasp_sensor_cooldown }}"
sequence:
- alias: "turn off the light"
service: light.turn_off
target:
entity_id: !input light_entity
- conditions:
- condition: trigger
id: box_opened_long_ago
- condition: state
entity_id: !input wasp_sensor
state: "off"
- condition: template
value_template: "{{ as_timestamp(now())|float - states[wasp_sensor].last_changed|as_timestamp|float > wasp_sensor_cooldown }}"
sequence:
- alias: "turn off the light"
service: light.turn_off
target:
entity_id: !input light_entity
- conditions:
- condition: trigger
id: wasp_cleared
- condition: state
entity_id: !input wasp_in_a_box_sensor
state: "off"
sequence:
- alias: "turn off the light"
service: light.turn_off
target:
entity_id: !input light_entity
# This is just an optimization
- conditions:
- condition: trigger
id: box_closed
- condition: state
entity_id: !input wasp_sensor
state: "off"
- condition: state
entity_id: !input wasp_in_a_box_sensor
state: "off"
sequence:
- alias: "turn off the light"
service: light.turn_off
target:
entity_id: !input light_entity
template:
- trigger:
- id: door_open
platform: state
entity_id: binary_sensor.my_door # REPLACE
from: "off"
to: "on"
- id: door_closed
platform: state
entity_id: binary_sensor.my_door # REPLACE
from: "on"
to: "off"
- id: door_closed_long_ago
platform: state
entity_id: binary_sensor.my_door # REPLACE
from: "on"
to: "off"
for:
seconds: 70 # REPLACE
- id: motion_detected
platform: state
entity_id: binary_sensor.my_motion_sensor # REPLACE
to: "on"
- id: motion_cleared
platform: state
entity_id: binary_sensor.my_motion_sensor # REPLACE
from: "on"
to: "off"
binary_sensor:
- name: "WIAB occupancy"
unique_id: my_occupancy_waspbox
state: >
{% if trigger.id == "motion_detected" %}
{% if is_state('binary_sensor.my_door', 'off') %} # REPLACE
on
{% else %}
{{ this.state }}
{% endif %}
{% elif trigger.id == "motion_cleared" %}
{% if is_state('binary_sensor.my_door', 'on') %} # REPLACE
off
{% else %}
{{ this.state }}
{% endif %}
{% elif trigger.id == "door_closed" %}
off
{% elif trigger.id == "door_closed_long_ago" %}
{%
if this.state == "off"
and is_state('binary_sensor.my_motion_sensor', 'on') # REPLACE
and (as_timestamp(now())|float - states.binary_sensor.my_motion_sensor.last_changed|as_timestamp|float > 70) # REPLACE entity ID and time value
%}
on
{% else %}
{{ this.state }}
{% endif %}
{% else %}
{{ this.state }}
{% endif %}
device_class: occupancy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment