Smarter Scenes with Smarter Vision
Last lesson your camera controlled the smart home based on presence. Now it will react to where you are. You will divide the camera frame into detection zones and give each its own Home Assistant scene, so sitting on the left of the couch sets a relax scene while the desk corner sets a focus scene. This is context-aware automation: the house responds not just to whether someone is there, but to where.
From presence to position
You already detect a person in the frame. The upgrade is to notice where in the frame they are. Divide the view into zones, for example the left half and the right half, and map each to a different behavior: the left side of the couch triggers a relax scene, the desk corner triggers a focus scene, and an empty frame sets an idle mode. The house now reacts to context, where you are and what you are likely doing, rather than mere presence.
Coordinate filtering
Zones come from simple math on the detection's position. Each detection has a center coordinate. If the person's horizontal center is less than half the frame width, they are on the left; otherwise they are on the right. That single comparison splits the frame into two zones, and you can define more by comparing against other coordinates. Because the zones depend on the frame, keep the camera in a fixed position so a given spot always maps to the same zone. You can use a USB webcam instead of the Pi camera if placement is easier.
Multiple regions and the cost of more work
Real systems often watch several regions, or even several cameras, at once. Handling two regions in one program is straightforward, but it is not free: processing more images per cycle lowers your frame rate, roughly halving it when you double the work. That trade-off is central to real projects, where one device must often do many jobs at once. It pushes you to write efficient code, since a slower frame rate can mean missed events. For a simple presence task a modest frame rate is fine, but knowing the cost lets you optimize when it matters.
Working through it
Fix the camera and plan zones. Mount the camera so the view does not move, and decide which areas of the frame map to which scene (for example left equals relax, right equals focus).
Add coordinate filtering. In the detection loop, compute the person's center and compare it to the frame width to decide which zone they are in.
Call a scene per zone. For each zone, call the matching Home Assistant scene, and set an idle scene when no one is detected, reusing the API call from last lesson.
Try multiple regions. Define two regions of interest and act on each separately. Watch the frame rate drop as the program processes more, and note where you could optimize.
Extend the context. Add rules based on the number of people or the time of day so the same camera drives richer, situation-aware behavior.
Trigger a scene based on which zone the person is in
# assume detection gives a bounding box with a center; frame has a width
frame_width = 640
def zone_scene(person_box):
x_center = person_box.center_x
if x_center < frame_width / 2:
return "scene.relax_mode" # left side of the room
else:
return "scene.focus_mode" # right side of the room
# inside the detection loop, per frame:
# if a person is detected:
# ha_service("scene", "turn_on", zone_scene(person_box))
# else:
# ha_service("scene", "turn_on", "scene.idle_mode")
One comparison of the person's horizontal center against half the frame width splits the view into a left and a right zone, each mapped to a scene. No one detected falls back to an idle scene. Add more comparisons for more zones.
Common mistakes and troubleshooting
Zones behave inconsistently. Keep the camera fixed; if it moves, the same spot maps to a different zone.
Frame rate tanks with multiple regions. Expect the drop and optimize, such as combining logic into one pass or lowering resolution, if the rate matters.
Scenes flip constantly as the person shifts near a boundary. Add a small dead zone or a short delay so a person on the line does not toggle scenes repeatedly.
No idle behavior. Set an idle scene when no one is detected so the room returns to a default.
Next: Next you will tie the whole month together in the Month 4 capstone: a fully aware room.