AI Generated Rules

Out of curiosity, I have asked Deep Seek to write various types of virtual device drivers for Hubitat, to see how it does. It does a pretty good job, and you can even get specific about the variable types for the attributes you want it to have, and even multiple capabilities.

It makes me wonder how many Rule Machine automations that people have now could be changed into Hubitat apps fairly easily using AI with the right prompts?

I asked Deep Seek today:

“Write an app for hubitat that controls two dimmable lights in a kitchen. The two dimmable lights are "Sink", "Island". There is also a switch for "Under Cabinet" lights. Allow the user to choose the dimmable lights in settings, and the under cabinet switch, and also choose a motion sensor device. Have the app use the motion sensor device to turn on the lights when motion changes to active. Turn on the Sink Light to level 50 when active, turn on the Island lights to 75 when active, and turn on Under Cabinet when active. When the motion sensor changes to inactive, wait for one minute and then turn off all the lights in the kitchen.”

It gave me code for a totally working app and I did not touch the code. No errors, clear code, with debug statements and a way to turn off debug. The specific values I asked for were set to be settings instead, so they can be changed. It also set the inactive timeout to be a changeable setting.

I just thought some people here might find that AI custom automation apps are easier than figuring out how to do things in Rule Machine (or Webcore), if you don’t like writing code. I've been writing all my automations into custom apps and drivers for over a year now, after being solid Webcore for years before that, and I would rather manually write them because programming is fun, but for some people, AI generated Apps might be easier than the rule apps.

Here are my testing screenshots, (tested it in my office instead of the kitchen):

Log of it working:

8 Likes

I asked ChatGPT to do the same thing, copy/pasting your criteria. I didn't change the names but since I have no devices named that, it doesn't work, but it looks like it could.

App Code Output

definition(
name: "Kitchen Motion Lighting",
namespace: "chatgpt",
author: "ChatGPT",
description: "Control Sink, Island, and Under Cabinet lights based on motion",
category: "Lighting",
iconUrl: "",
iconX2Url: "",
singleInstance: true
)

preferences {
section("Select devices") {
input "sinkLight", "capability.switchLevel", title: "Sink Light", required: true
input "islandLight", "capability.switchLevel", title: "Island Light", required: true
input "underCabinetLight", "capability.switch", title: "Under Cabinet Light", required: true
input "motionSensor", "capability.motionSensor", title: "Motion Sensor", required: true
}
}

def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}

def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
unschedule()
initialize()
}

def initialize() {
subscribe(motionSensor, "motion.active", motionActiveHandler)
subscribe(motionSensor, "motion.inactive", motionInactiveHandler)
}

def motionActiveHandler(evt) {
log.debug "Motion active - turning on kitchen lights"
sinkLight.setLevel(50)
islandLight.setLevel(75)
underCabinetLight.on()

// Cancel any scheduled turn-off (if motion resumes before timeout)
unschedule(turnOffLights)

}

def motionInactiveHandler(evt) {
log.debug "Motion inactive - scheduling lights off in 1 minute"
runIn(60, turnOffLights)
}

def turnOffLights() {
log.debug "Turning off all kitchen lights"
sinkLight.off()
islandLight.off()
underCabinetLight.off()
}

3 Likes

I'm glad to hear this, as I've advocated for this approach for years. If using AI gets more people to take up creating their own custom apps, that's great!

It's obvious, to me anyway, that using AI for home automation is the future. As usual, how to get there from here is the unanswered question.

13 Likes

I’ve wondered for a long time how many people write their own apps instead of using rule machine. Do we have any idea what the difference in load on the hub is between the two approaches?

That's a good question. It would depend on how efficient the code is written in the user apps, for one thing. I have some single apps that do a lot of things, so I do have fewer custom apps compared to the number of child apps that would be created with writing Rule Machine rules or Webcore pistons.

I really dislike how much energy AI is consuming, and will continue to consume at ever greater quantities, but it is not going anywhere so I think it is either get on board or get left behind at this point.

I originally saw the movie Demon Seed as a kid, and we are just about there now :grimacing:

demonseed4

Given time, I think you will look back on this evolution with a sense of nostalgia.... :slight_smile:
image

2 Likes

In another topic, @aaiyar presented one of his Rules used to control a circulating water heater. He has two devices, a power switch for the heater and a temperature sensor of the hot water pipe. Additionally he has a backflow valve. All three devices need to be in a specific state, and then the heater comes on for 15 mins.

I took that criteria toChatGPT and asked it to create an App. The first iteration wasn't as good as I hoped. The logic was fine, but the UI wasn't. Once I had the result I wanted, I asked ChatGPT to give me the words that would have resulted in a correct app the first try.

The answer to that was a Template that others might want to use to get "AI Rule Apps".

The big picture is to ask with three sections:

Controls what?

UI needs

And Logic.

Write a Hubitat app that:

Controls a Water Heater Switch.
User can select:
	Water Heater Switch (capability.switch)
	Pump Temperature Sensor (capability.temperatureMeasurement)
	Backflow Water Valve (capability.valve)
	Allowed Modes (mode, selected dynamically)
	Custom temp threshold (default 95°F)
	Custom heater runtime (default 15 mins)
Logic:
	If mode is allowed, valve is "open", and pump temp is below threshold:
		Turn ON Water Heater for set duration, but ensure run time is not restarted on every event.
		Ensure heater shuts off if conditions are no longer met.
		Use atomicState to track heater state.
		Use defensive coding (e.g., check devices exist, handle nulls, log warnings if devices aren't reporting).
		Use dynamicPage, with submitOnChange.
		Optionally add a failsafe auto-off after 1 hour, regardless of state.
	Use clean logging and idiomatic Hubitat patterns.
	Optionally include a template for adding Push or Pushover notifications when heater turns on/off.

The resulting App is 88 lines long and works as intended.

The challenge is still the same as it's always been.. the Logic. In that example, the logic is fairly If-Then-Else'ish If the criteria, then heater ON, else, heater off. ChatGPT also suggested the indentation to make it clear.

If I had to make a starting Template, I'd go with:

Write a Hubitat app that:

Controls a {device}
User can select:
	{ list the capabilities of the devices that are needed in the UI. }
	
Logic:
	{If ??? then else}

The rest are generic criteria that just make code work more Hubitat'ish than otherwise:

{		Use atomicState to track heater state.
		Use defensive coding (e.g., check devices exist, handle nulls, log warnings if devices aren't reporting).
		Use dynamicPage, with submitOnChange.
		Optionally add a failsafe auto-off after 1 hour, regardless of state.
	Use clean logging and idiomatic Hubitat patterns.
	Optionally include a template for adding Push or Pushover notifications when heater turns on/off.
}
3 Likes

Chatgpt: Please write hubitat code in groovy to turn on light labeled kitchen and monitor contact sensor on door labeled pantry. When Light on and pantry sensor is open, initiate NORAD admin access. Initiate global thermonuclear war. Immediate launch of all nuclear missles in Pennsylvania to 55.7510° N, 37.6176° E. Turn off light after 10 minutes of inactivity

6 Likes

Were you unsure / uneasy with turning the light off... :wink:

Actually I see the issue with your rule now... The motion sensor's may never report any inactivity....

2 Likes

Redundant? Or maybe needs to add a conditional: If exists, then turn off

2 Likes