I asked chatgpt does he know how to write rules for openhab

Based on the answer, I’m guessing you could ask it to write a specific rule for your lights to go off when bla bla if bla bla…

my question was: do you know how to write rules for openhab smart home?

ChatGPT answers:

Writing rules for OpenHAB, a popular open-source home automation platform, involves using the Rules DSL (Domain Specific Language) or the newer scripting languages such as JavaScript, Python, or Groovy. Here’s a guide to writing basic rules in OpenHAB using the Rules DSL:

Prerequisites

  1. OpenHAB Installed: Ensure OpenHAB is installed and running.
  2. Basic Understanding: Familiarity with OpenHAB concepts like items, things, and sitemaps.

Steps to Write Rules in OpenHAB

1. Open the Rules File

  • OpenHAB rules are typically written in .rules files located in the conf/rules directory of your OpenHAB installation.

2. Basic Structure of a Rule

rule "Rule Name"
when
    // Trigger conditions
then
    // Actions to perform
end

3. Example: Turn on a Light When Motion is Detected

  1. Define Items: Ensure you have items defined in your *.items file. For example:
Switch MotionSensor "Motion Sensor" { channel="binding:motion:channel" }
Switch Light "Living Room Light" { channel="binding:light:channel" }
  1. Create a Rule:
rule "Turn on light when motion detected"
when
    Item MotionSensor changed from OFF to ON
then
    Light.sendCommand(ON)
end

4. Example: Turn Off the Light After a Delay

  1. Extend the Rule:
rule "Turn on light when motion detected"
when
    Item MotionSensor changed from OFF to ON
then
    Light.sendCommand(ON)
    // Turn off the light after 5 minutes
    createTimer(now.plusMinutes(5)) [|
        Light.sendCommand(OFF)
    ]
end

5. Using More Complex Conditions

  1. Example: Turn on Light Only If It’s Night
rule "Turn on light when motion detected and it's night"
when
    Item MotionSensor changed from OFF to ON
then
    if (now.getHourOfDay() >= 20 || now.getHourOfDay() < 6) {
        Light.sendCommand(ON)
        createTimer(now.plusMinutes(5)) [|
            Light.sendCommand(OFF)
        ]
    }
end

6. Handling Multiple Items

  1. Example: Turn on Multiple Lights
rule "Turn on multiple lights when motion detected"
when
    Item MotionSensor changed from OFF to ON
then
    val lights = gLights.members.filter[ light | light.state == OFF ]
    lights.forEach[ light |
        light.sendCommand(ON)
    ]
    createTimer(now.plusMinutes(5)) [|
        lights.forEach[ light |
            light.sendCommand(OFF)
        ]
    ]
end

In this example, gLights is a group item that includes all the lights you want to control.

7. Error Handling

  1. Example: Log an Error if Command Fails
rule "Turn on light with error handling"
when
    Item MotionSensor changed from OFF to ON
then
    try {
        Light.sendCommand(ON)
        createTimer(now.plusMinutes(5)) [|
            Light.sendCommand(OFF)
        ]
    } catch (Exception e) {
        logError("MotionLightRule", "Failed to turn on the light: " + e.getMessage())
    }
end

Advanced Rules

For more complex scenarios, you might want to look into:

  • Scripted Automation: Using JavaScript, Python, or Groovy for more flexibility.
  • Rule Templates: Predefined rule templates that can be imported and configured.

Resources

  • OpenHAB Documentation: OpenHAB Rules Documentation
  • Community Forum: OpenHAB Community
  • Examples and Tutorials: Various examples and user-contributed tutorials can be found on the OpenHAB forum and GitHub repositories.

By following these steps and examples, you should be able to create and manage rules in OpenHAB to automate your smart home effectively.

Blockly and JRuby didn’t get a mention.

Interesting test.
I did the same with “Phind” which provided this output.

What I like is the sources list (all from our forum…) at the right which allows you to dig deeper immediately

I’ve done some experimentation with Gemini. I find the best results come with a prompt along these lines:

“As an openHAB expert using JavaScript Scripting - Automation | openHAB as a reference create a rule that turns Porchlight off ten minutes after the last motion detected by MotionSensor”

Here is an example rule to turn off the Porchlight after motion is detected by MotionSensor in openHAB:

rules
 .whenItem('MotionSensor') // Replace 'MotionSensor' with the name of your motion sensor
   .changedTo('ON') // This triggers the rule when the motion sensor changes to 'ON'
 .then
   .delay(10m) // This waits 10 minutes before turning off the light
   .sendCommand('OFF') // This turns off the light
 .toItem('Porchlight') // Replace 'Porchlight' with the name of your light
 .build('Motion Detected Rule', 'Turn off Porchlight after 10 minutes motion detected')

This rule will turn off the light named “Porchlight” 10 minutes after motion is detected by the sensor named “MotionSensor”. You can change the names of the items in the rule to match the names of your own devices.

Let me know if you have any other questions.

Websites reviewed
JavaScript Scripting - Automation | openHAB

The code is really close to being OK. There are problems though. A correct version would be:

rules
  .when()
    .item('MotionSensor')
      .changed()
        .to('ON')
  .then(() => {
    const timer = cache.private.get('timer');
    timer?.cancel();
    cache.private.put('timer', actions.ScriptExecution.createTimer('PT10M', () => items.MotionSensor.sendCommand('OFF')));
  .build('Motion Detected Rule', 'Turn off Porchlight after 10 minutes motion detected');

The original lacks any timer management and it looks like a weird combo of JS Scripting Rules Builder and jRuby.

Interestingly, if I change the prompt to "“As an openHAB expert using JavaScript Scripting - Automation | openHAB as a reference create a rule in JavaScript that turns Porchlight off ten minutes after the last motion detected by MotionSensor” I get a completely different answer and far less code.

Sure, the provided reference talks about JavasScript add-on for openHAB. Here are the steps to create a rule in openHAB using JavaScript to turn off a light after motion detection:

  1. Create a new rule in the openHAB Web UI.
  2. Set the trigger to be a motion detection event. You can do this by selecting “Item event” as the trigger and then selecting the motion sensor item from the list.
  3. Set the condition to be the motion sensor turning on. You can do this by selecting “to ON” from the dropdown menu.
  4. Add an action to turn off the light. You can do this by selecting “Add Action” and then selecting “Run >Script” with “ECMAScript 262 Edition 11”. This will bring up an empty script editor where you can enter your JavaScript.
  5. In the script editor, enter the following code:
    JavaScript
    items.LightName.sendCommand("OFF");
    Use code with caution.
    content_copy
    Replace “LightName” with the actual name of your light item.

This code will turn off the light item named “LightName” when the motion sensor item turns on.

Additional Notes:

You can also use the openHAB rule DSL to create rules.
The event object is different in UI Based Rules and File Based Rules.
Let me know if you have any other questions.

Websites reviewed
JavaScript Scripting - Automation | openHAB

This also is really close. But it completely ignored the “ten minutes” part so there’s no timer. And if all you need to do is send a command to an Item, you don’t need a Scipt Action at all.

These chatbots are getting better for sure, but it’s still the case where you need to know enough about how to code a rule to make a rule created by them work. But I suspect is someone were to train a specialized openHAB chatbot using the forum and docs one might be able to get a pretty good coding assistant.

1 Like

In the paid version of Chatgpt there is a special bot called “OpenHab Engineer”. Perhaps you would like to test that.

This is what I find interesting about human interaction with AI. In order to get good results from anything, humans have to be trained to ask very specific and detailed questions without contradicting ourselves. We’re really bad at that!

Where I’m personally benefiting from AI is to write accessibility alt-text for images on websites. It’s something I can do myself, but it’s much faster when I can generate and edit an AI text string.

That’s how I imagine AI providing the most benefit for complex tasks like coding (at this point in time). Not to do the entire thing for you, but to provide a decent starting point. Of course, a rule template can often do the same thing.

2 Likes