Skip to content

Instantly share code, notes, and snippets.

@bobmcwhirter
Created July 2, 2025 19:24
Show Gist options
  • Save bobmcwhirter/2198ba2247f101042a435cb1bde9f508 to your computer and use it in GitHub Desktop.
Save bobmcwhirter/2198ba2247f101042a435cb1bde9f508 to your computer and use it in GitHub Desktop.
# # Event-Triggered Tasks
#
# This skill shows how to execute tasks when an event occurs.
def setup(ctx):
motion_sensors = ctx.get_occupancy_sensors()
# When motion sensor attributes change, run `event_triggered_task`
ctx.subscribe(motion_sensors, event_triggered_task)
print("Waiting for motion sensor events...")
def event_triggered_task(ctx, event_info):
# event_info.target is the entity that triggered the event
motion_sensor = event_info.target
print(f"Change Event: Device Name: {motion_sensor.basic_information.node_label}, New State: {motion_sensor.occupancy_sensing.occupied}")
# Explanation:
#
# Whenever any motion sensor changes state, the `event_triggered_task` function
# is called.
#
# IMPORTANT: The subscription applies to all motion sensors, including those
# that may be discovered and added later!!
#
# If you want to subscribe to events from _specific_ motion sensors rather than
# any motion sensor over time, then subscribe to the devices individually.
#
# Subscription callbacks like the `event_triggered_task` function must have TWO
# parameters: the skill's context and an event_info object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment