I’m sure this one exists already, and certainly there’s the “Motion Activated Light” blueprint built into Home Assistant, but I have a particular use case.
-
I combined the state of a motion sensor (binary sensor) and a presence sensor (binary sensor) into a custom sensor (sensor). The built-in HA blueprint only accepts binary sensors, so I wouldn’t be able to lean on my custom sensor.
-
The HA script allows toggling a “light” sensor, but I’m actually trying to control a ZigBee “switch” entity. Yes, I could change the device type to “light” (especially since it is controlling a light bulb ultimately at the moment), but if this switch was ever controlling something that’s not a light, that would get confusing.
Instead, I whipped up a small automation blueprint that accepts a “sensor”, and based on its on/off state (plus a delay for off), will trigger different on/off actions. If you wanted, you could have “on” do something completely different than “off”.
Blueprint
# This script will trigger different actions based on whether a sensor is on or off.
blueprint:
name: Based on sensor
description: Trigger an action when a sensor is on or off
source_url: https://raw.githubusercontent.com/nwithan8/configs/main/home_assistant/blueprints/automations/based_on_sensor.yaml
domain: automation
input:
sensor:
name: Sensor
description: A sensor that toggles on and off
selector:
entity:
domain:
- sensor
- binary_sensor
- input_boolean
- light
- switch
- outlet
sensor_on_state:
name: Sensor "on"
description: State the sensor is considered "on"
selector:
text:
sensor_on_action:
name: When enabled
description: The action to perform when the sensor is enabled
selector:
action:
default: []
sensor_off_state:
name: Sensor "off"
description: State the sensor is considered "off"
selector:
text:
sensor_off_action:
name: When disabled
description: The action to perform when the sensor is disabled
selector:
action:
default: []
duration:
name: Wait duration
description: How long to wait after the sensor is disabled before triggering the action
default: "0:00:30"
trigger:
- id: "off"
entity_id: !input sensor
for: !input duration
platform: state
to: !input sensor_off_state
- id: "on"
entity_id: !input sensor
platform: state
to: !input sensor_on_state
condition: []
action:
- choose:
# Sensor enabled
- conditions:
- condition: trigger
id: "on"
sequence: !input sensor_on_action
# Sensor disabled
- conditions:
- condition: trigger
id: "off"
sequence: !input sensor_off_action
mode: single
Setup
I use this to turn a switch in my garage on and off when presence is detected, but it’s generic enough that you can use this for any simple “on” or “off” action based on a boolean.
Sensor “on” and Sensor “off” need to be the word of the state that the sensor will be in. For some, this might be True/False, others on/off.