From Detection to Action
This lesson connects two months of work: your AI camera from Month 3 now controls your smart home. Instead of blinking an LED or sending a chat message when the camera sees a person, it will tell Home Assistant to turn on a light or run a scene. You will get an access token, find your device's ID, and swap the old action for a call to Home Assistant, turning vision detection into home automation, all running privately on the same Pi.
Upgrading, not rebuilding
In Month 3 you built a real-time detector that streams from the Pi camera, finds a person in a region of interest, and triggers an action, blinking an LED or sending a Discord or Telegram alert. You are not rebuilding any of that. You are swapping the one action line: instead of driving a GPIO pin or posting a webhook, that line will now command Home Assistant. The same brain, now wired to the rest of your smart home.
The access token
For your Python program to control Home Assistant, it needs permission. Home Assistant provides a long-lived access token: in your user profile under security, you create one and copy it into your script. That token is what lets the code authenticate and send commands, so treat it like a password and do not share it. This is the same idea as an API key from earlier lessons.
Entity IDs
Every device in Home Assistant has an entity ID, a unique name like switch.sonoff_lamp. To control a device from code you need its exact ID, which you find in Developer Tools under States by searching for the device (for example a Sonoff switch). You use one entity ID per device you want to command. Getting this exactly right is essential, since the API acts on whatever ID you give it.
Calling the API and combining the systems
With the token and entity ID, your Python code makes a web request to Home Assistant to turn a device on, off, or toggle it. You first test this alone, running a small script that toggles a lamp to confirm the connection works. Then you drop that call into the detection loop where the old action was: when a person is detected in the region of interest, instead of blinking an LED, the code calls Home Assistant to turn on the reading light or run a movie scene. Testing the pieces separately, first the toggle, then the detection, keeps debugging simple.
Working through it
Create a long-lived access token. In Home Assistant, open your user profile, go to security, create a long-lived access token, and copy it into your script.
Find the device's entity ID. In Developer Tools, States, search for your device (such as a Sonoff switch) and copy its entity ID into your code.
Test the connection. Run a small script that toggles the device. If the lamp flips on and off, your token and entity ID are correct.
Reuse the Month 3 detection. Open your detection code, keep the region of interest and person detection, and locate the action line that previously blinked an LED or sent an alert.
Swap in the Home Assistant call. Replace that action with the call that toggles the light or runs a scene, so a person in the zone now controls the smart home.
Control Home Assistant from Python
import requests
HA_URL = "http://localhost:8123"
TOKEN = "YOUR_LONG_LIVED_TOKEN" # from your HA profile
HEADERS = {"Authorization": f"Bearer {TOKEN}"}
def ha_service(domain, service, entity_id):
url = f"{HA_URL}/api/services/{domain}/{service}"
requests.post(url, headers=HEADERS, json={"entity_id": entity_id})
# test it alone first: toggle a lamp
if __name__ == "__main__":
ha_service("switch", "toggle", "switch.sonoff_lamp")
The token authenticates the request and the entity ID names the device. Running this alone should toggle the lamp, confirming the link before you wire it into detection.
Detection now controls the light
# inside the Month 3 detection loop, for each frame:
# if label == "person" and in_region(box, ROI):
# # OLD: blink an LED or send a Discord alert
# # NEW: tell Home Assistant to turn on the reading light
# ha_service("light", "turn_on", "light.reading_lamp")
# else:
# ha_service("light", "turn_off", "light.reading_lamp")
The detection logic is unchanged from Month 3; only the action swapped. When a person is detected in the region, the code calls Home Assistant to turn on the light instead of blinking an LED, and turns it off when they leave. You could call a scene the same way.
Common mistakes and troubleshooting
The API call is rejected. Check the token is correct and included as a Bearer authorization header, and that Home Assistant is reachable at the URL.
Nothing happens for the right device.