ChatGPT for writing openHAB rules - it's fun

Hi there,

i recently tried out ChatGPT to let it write openHAB rules. Its fun! Try it :slight_smile:

its also possible to optimize existing ones. I wrote: “Please improve this code”

var HifiState, WTVState;

var thread = Java.type('java.lang.Thread')


HifiState = itemRegistry.getItem('CEOLpiccoloDRAN5_Power').getState();
WTVState = itemRegistry.getItem('WTVState').getState();
if (WTVState == 'ON') {
  if (HifiState == 'OFF') {
    events.sendCommand('CEOLpiccoloDRAN5_Power', 'ON');
    thread.sleep(5000);
    events.sendCommand('CEOLpiccoloDRAN5_Volume', '10');
  }
} else if (WTVState == 'OFF') {
  if (HifiState == 'ON') {
    events.sendCommand('CEOLpiccoloDRAN5_Power', 'OFF');
  }
}

and the result was:

var thread = Java.type('java.lang.Thread');

const items = {
    HifiState: itemRegistry.getItem('CEOLpiccoloDRAN5_Power'),
    WTVState: itemRegistry.getItem('WTVState')
};

switch (items.WTVState.getState()) {
    case 'ON':
        if (items.HifiState.getState() === 'OFF') {
            items.HifiState.sendCommand('ON');
            thread.sleep(5000);
            items.HifiState.sendCommand('10');
        }
        break;
    case 'OFF':
        if (items.HifiState.getState() === 'ON') {
            items.HifiState.sendCommand('OFF');
        }
        break;
    default:
        break;
}

including 5 detailed explanations why it has been modified.

5 Likes

Related:

Be cautious with this though. There’s a lot that ChatGPT doesn’t know; for example:

  • that thread.sleep should be a timer instead
  • you can’t use const in a UI script action or script condition like that
  • the Object representing the Items in the itemRegistry are frequently changing so if you fix HifiState and WTVState using const, if that Item is later changed these Objects will become stale and they will no longer represent the device’s current state
  • in fact, if this is ECMAScript 5, you can’t use const at all (const and let were introduced to the language later)

That’s not to down play what it does but to emphasize that you still need to know what you are doing so you can understand the code presented to you. And in some ways, you have to understand both the code and how OH works even more than you would otherwise to avoid pitfalls like these.

1 Like

Thanks for your feedback.
And I understand that it has to be verified. On the other hand, it makes life easier, especially if you are syntactically unsure.

DSL rules are also possible. Just told it to concert the rule above into a DSL rule.

The result:

// Define the items to check
var HifiState = getItem("CEOLpiccoloDRAN5_Power").state
var WTVState = getItem("WTVState").state

// Define the Thread class
var thread = Java.type('java.lang.Thread')

// Check the state of the items
if (WTVState == ON) {
  if (HifiState == OFF) {
    sendCommand(getItem("CEOLpiccoloDRAN5_Power"), ON)
    thread.sleep(5000)
    sendCommand(getItem("CEOLpiccoloDRAN5_Volume"), 10)
  }
} else if (WTVState == OFF) {
  if (HifiState == ON) {
    sendCommand(getItem("CEOLpiccoloDRAN5_Power"), OFF)
  }
}
1 Like

How does it do with different rule engines?

I’m trying to rewrite my DSL rules using ECMAScript 2021+ and have been having some frustration with getting a grip on some of the syntax of the new language. I’m wondering if this might help me learn.

Edit: Tried it out. I told it that I wanted to rewrite openHAB DSL rules to use ECMAScript instead. I think @rlkoshak makes some great points about its limitations, but I’ve already found it useful as I’m trying to learn the new language. I’m impressed: the rule I’m migrating is a wake up routine that gradually increases brightness and color temperature, and the software understood and stated the purpose of the script.

I will say that it’s definitely not writing rules that work out of the box, at least not in ECMAScript 2021+. Trying different iterations and running them back through the chat window is kind of amusing, like two people muddling their way through something neither of them understands.

A couple of other points about ChatGPT:

  • I think the data used to train it is already a couple years old. So any new features to OH added since then ChatGPT will not know.

  • I almost certainly was trained based on scraping the examples here on this forum. I’d say that between 1/3rd and half of all the example code here on the forum is broken (most of the code comes from “I made this rule, it doesn’t work, help” type postings), meaning ChatGPT is working with code that is already known not to work.

Just a couple more caveats and things to watch out for.

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.