Created
July 2, 2025 19:24
-
-
Save bobmcwhirter/2198ba2247f101042a435cb1bde9f508 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # 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