Smart Lighting Automation with GenAI and MCP Link to heading
It’s 9pm and time for me to sit down with a fresh latte to watch some TV. As I stumble through my dark media room searching for the tray, disaster strikes: I rest the glass on top of my remote and spill coffee all over. Why can’t my room light up while I get to my chair and then dim when I start my TV?
Modern problems require modern solutions. At my disposal I have Home Assistant, an Everything Presence Lite, and Philips Hue smart bulbs. Importantly, Claude Code can do my thinking for me! (Foreshadowing…)
Design and Equipment Link to heading
There are different factors that go into the logic I’m seeking. First, the room has to be dark which means it’d be after sunset. The TV shouldn’t be on (otherwise the glow of the TV and bias lighting would be good enough to navigate). Finally, someone should be entering the room to trigger a change.
Presence sensors in modern smart homes have upgraded from passive infrared (PIR) sensors to a new tech called mmWave which can detect subtle movements and track up to three people within a room. I have an Everything Presence Lite, though there are many other options on the market.
As seen in a previous blog post, my Denon A/V receiver has a Home Assistant integration so I can know when my system is on or off.
Because this is a state machine, it does require some reasoning about changes to the room’s state that result in a side effect by changing the state of the lights. I’m curious how far I can get with the use of generative AI to do the thinking for me.
Interlude: Home Assistant MCP Link to heading
Model Context Protocol is the new hotness, a way of bringing private integrations to your LLM of choice to meet your goals. On my desktop I install a community Home Assistant MCP Server implementation, which ensures that Claude Code can access information about my smart devices. With that intelligence in hand, Claude Code develops automation with its own understanding about conditions to consider for my situation.
The Automation and the Result Link to heading
Time for a full send!
Prompt: Query Home Assistant for my media room lights, presence sensor, and Denon AV receiver. Create an Automation to ensure that the lights turn on if it’s sunset, the receiver is off, and someone enters the room. If the receiver turns on, turn off the lights. If the receiver turns off, turn on the lights. If the person leaves the room, turn off the lights.
Home Assistant generated a YAML file that I was able to copy/paste into Home Assistant (see the end of the article). Interestingly it identified a condition I hadn’t considered: what if someone is sitting in the room as sunset arrives? I’d want the lights turned on, and the automation included that condition.
I’ve been using the automation for several days and initially it was great! I was worried that as people walked past my media room that the mmWave sensor would inadvertently trigger the automation. No problem there.
One night I entered and exited the room several times and I’m not exactly sure what put the automation in an unhappy state. The lights don’t come on anymore, and it requires more work to understand what’s going on.
Next Steps Link to heading
I need to invest more thought with this automation and working with Claude Code. I plan to work with Claude Code to generate a state machine I’m happy with and then have it implement the automation. Increasingly developers using AI assistants succeed with a more cautious spec/design/implement loop, rather than sending it and letting GenAI to come up with its own interpretations.
If I have the time to invest, I’d be interested in applying formal methods to this simple use case. Mathematically proving that my lights will be on the desired state at all times would build trust in my smart lighting.
In spite of the initial failure, I’m satisfied with this experiment. Claude Code encouraged me to try out a complex automation where previously I relied on habit to keep my remote in a predictable location to trust the darkness.
Update! Link to heading
Minutes after publishing this article I decided to ask Claude Code to help me debug. It found that my presence sensor was not responding! My bean bag bumped into the power adapter so it was unplugged the whole time. Maybe I don’t need to look into state machines and formal methods. 🤔
Appendix: the HA Automation YAML Link to heading
alias: Smart Media Room Lighting Control
description: >-
Controls media room lights based on occupancy, time, and Denon AV receiver
status
triggers:
- entity_id: binary_sensor.everything_presence_lite_4f106c_occupancy
id: occupancy_change
trigger: state
- entity_id: media_player.home_theater
to: "off"
id: receiver_off
trigger: state
- event: sunset
id: sunset
trigger: sun
conditions: []
actions:
- choose:
- conditions:
- condition: trigger
id: occupancy_change
- condition: state
entity_id: binary_sensor.everything_presence_lite_4f106c_occupancy
state: "on"
- condition: sun
after: sunset
- condition: state
entity_id: media_player.home_theater
state: "off"
sequence:
- target:
entity_id: light.media_room_lights
action: light.turn_on
- conditions:
- condition: trigger
id: occupancy_change
- condition: state
entity_id: binary_sensor.everything_presence_lite_4f106c_occupancy
state: "off"
- condition: state
entity_id: media_player.home_theater
state: "off"
- condition: state
entity_id: light.media_room_lights
state: "on"
sequence:
- target:
entity_id: light.media_room_lights
action: light.turn_off
- conditions:
- condition: trigger
id: receiver_off
- condition: state
entity_id: binary_sensor.everything_presence_lite_4f106c_occupancy
state: "on"
- condition: sun
after: sunset
sequence:
- target:
entity_id: light.media_room_lights
action: light.turn_on
- conditions:
- condition: trigger
id: sunset
- condition: state
entity_id: binary_sensor.everything_presence_lite_4f106c_occupancy
state: "on"
- condition: state
entity_id: media_player.home_theater
state: "off"
sequence:
- target:
entity_id: light.media_room_lights
action: light.turn_on
mode: queued
max: 5