Using ChatGPT to write scripts (as a beginner)

I have a genuine question whether you think that, having a software engineering background (UX/UI, product design, business processes), but not so much so in programming and development (a wide array of languages, knowing some architecture, CS, but not too deep in either) - would it make sense to ignore learning of the OH scripting language (but also translates to Arduino, and my future idea of connecting it all to the DB/UI representation), and just outsource it all to GPT 4?

My end goal is to practice a bit in smart home applications, make a nice smart home, practice collateral technologies (like Linux), and then have some fun along the way. Would learning how to write OH scripts (or MQTT commands on Arduino) make it? I am just one of the people, who is kind of trying to “start from the base” (e.g., doing data analytics AFTER knowing statistics, foundational knowledge etc.).

I find that, in my main job, which is PhD research, I find that ChatGPT is actually quite useful (and can actually short cut a lot of the “manual work” which I learned already a long time ago, so for me it’s very basic). And I heard that Sr. Developers are forgetting how to write loops, since they are at a higher level of abstraction, which I totally get when I want to open R or python again and I know that I need to transform the data in such and such way, so I don’t remember the exact syntax of a FOR loop either.

But still, I feel like I can be doing something “not properly”, “not ground-up”, and so it’s like soldering a given set of components onto the plate, and that’s it - you just know how to solder the components, not to make the PCB design, perform circuit analysis, etc.

What are your opinions/recommendations? (Hope that’s the right theme for this question hah)

Thanks!

Based on my experience with GPT and Bard, no. They almost never produce anything that is usable without edits and you basically have to be skilled in the rules language you are using to recognize and fix the rule to make it work.

In both cases, half the time if I ask for a rule in JavaScript it will give a Frankenstein’s monster amalgam of Rules DSL and JavaScript. I’ve yet to get either to produce something reasonable for use in a UI rule. They both rarely get any of the interactions with the OH rules API or working with the helper libraries right except for Rules DSL and there the problem is the rules are so close they look reasonable, but always have some fatal and subtle flaw that renders them unusable.

GTP and Bard and the others are much better at generic programming problems or working with really popular APIs (e.g. AWS, Docker, Popular databases, probably even Arduino, etc.). openHAB is way to small. There are more bad examples for them to train on than good examples too (just look around the forum, most of the code is “this doesn’t work, help!”).

Tl;dr, with the current state of LLMs, you’ll have to learn one of the OH rules languages well to know enough to fix the crap these chatbots produce. You may as well save the effort and learn the languages in the first place.

Maybe when/if there is a custom openHAB app on ChatGPT’s app store this will change but for now, it’s just not worth it.

Note, rules for openHAB tend to be pretty short, simple and high level. “When x happens and the state of y do z”. The “z” is rarely very complicated. So it’s not like you need to go through months of study to become proficient. It’s usually just looking up a couple of commands and you are done.

I’m a strongly of the opinion that I want to solve home automation problems, not syntax errors. Therefor, depending on your comfort level I’d recommend:

Don’t Do
Use a chatbot to code rules for OH Use Blockly which, being visual, is easier to remember and understand for most. If not Blockly, use JS Scripting or jRuby which both have great helper libraries which makes interacting with the OH API in rules pretty simple.
Use a chatbot to code microncontrolers Use Tasmota, ESPEasy, ESPHome, etc. instead. Then all the behaviors and MQTT stuff becomes configuration, not coding.

Are these the things you care about? If not you are not missing anything. Focus on solving the problems you care about. You don’t have to be an expert in machine code to use MS Word, why hold yourself to a similar standard for home automation?

Senior developers know how computer work. They know how to make a computer do something and do it in an elegant manner given how computers work. R or Python are mere syntax. You don’t have to remember how to do that in every language in the world. You do need to know that for loops are a thing and what they do. You can look up the rest.

1 Like

Hi Rich,

Thank you for a reply!

Been using GPT for R. It is actually not very usable for “high-level abstraction tasks”, like describing the analysis operation takes too much time, and the result is garbage indeed. It helped me, though, to break up the analysis operation I was trying to do, into several steps, and then get the GPT prompts “to do what I want to do”, but more direct tasks.

Though, I found ChatGPT excelling at certain data analysis tasks directly (i.e., when I ask it to analyse directly the data, like summarizing it). Ok, not “excelling”, but very close to that. Needed a double-check, but saved me from doing it the “traditional way” (of running multiple iterations of data reading).

Also, CoPilot was somewhat good for writing code for the JS react. But again, a popular framework, a JS language, maybe a lot of the training data. My experience then was that I actually understood not so much how that works. When I was doing Python (manually, no side input), then I learned much better.

So in the end, I kind of use it like a starting point, and then from trial and error, I make the code smooth, by editing it? But ok, I was doing this on R (and javascript), where I kind of know something. So I totally get the point of “better learning it in the first place”. I was kind of taking a look at the MQTT (I want to create arduino and then connect it to OH), but I am yet to get to that.

Ok, got that.

Blockly seems like a nice visualization. Kind of reminds me of “model-driven engineering”, where the entities and attributes and their encapsulation are attempted at visualizing, hence arriving at the conceptual model, which is then easier to comprehend than a one-dimensional model, that is syntax/text, and then one that is used for generating the program.

I think, I kind of get at the picture from another perspective that the domain is engineering, and not coding contest. That’s reassuring, though somehow I always get this impression of the “coding block wall”. As if syntax is indeed a very bad interface to interact with the program, and it takes some time to overcome it as a bloc. For example, I always hate trying to understand how to write to do a simple task, but for which I don’t know the right syntax for. ChatGPT then kinda helps at that moment, but it also forces to rethink the syntax. I think though that in the future syntax-making machine of LLM can become very good (at least, copilot or something specialized), and then it’d be more about highest-level abstractions, of defining the entities, understanding the domain and then translating it onto the machine…

But ok, off-topic. Thanks, clarified it a bit.

Note that JavaScript is one of the languages OH supports. In fact, Blockly “compiles” to JavaScript and we pay close attention to making sure that the JS Blockly produces is reasonable. That lets Blockly be a nice teaching tool. You can build up the blocks then look at the JS produced and learn how it works.

MQTT is a messaging protocol. There are a few key concepts to learn (publishers, subscibers, QOS, and LWT) but beyond that MQTT is mainly something you use. Not really something you need to learn in depth. And how you use it is going to differ between OH and what ever microcontroller language you are using.

What I like about Blockly for this is you end up with a drag and drop menu of all the options. You don’t have to remember the syntax. You just need to read the names/descriptions of the blocks to find the one you want.

Instead of

(blank screen) you start with this:

Then you can kind of muddle through. For example, I know I want to send a command to an Item, let’s look at “Items and Things”.

There it is, 7th from the top. Now I want to compare now with the state of a DateTime Item and only if it’s later send the command. That’s a comparison so let’s look under “Logic” and I see blocks for an if statement and comparisons. But it’s not clear how to do DateTime comparisons. Let’s look under Dates & Times and there I see now and some date time comparison operation blocks.

It’s not zero knowledge but it avoids the dreaded blank page problem. And if you want to see the syntax click the code icon at the bottom.

function zdtCompare(zdt1, zdt2, compareOp, precision, compDate) {
  switch (precision) {
    case 'years':
     zdt2 = zdt2.withMonth(zdt1.monthValue());
    case 'months':
     zdt2 = zdt2.withDayOfMonth(zdt1.dayOfMonth());
    case 'days':
     zdt2 = zdt2.withHour(zdt1.hour());
    case 'hours':
     zdt2 = zdt2.withMinute(zdt1.minute());
    case 'minutes':
     zdt2 = zdt2.withSecond(zdt1.second());
    case 'seconds':
     zdt2 = zdt2.withNano(zdt1.nano());
  }
  if (compDate === 'date') {
    zdt1 = zdt1.toLocalDate();
    zdt2 = zdt2.toLocalDate();
  } else if (compDate === 'time') {
    zdt1 = zdt1.toLocalTime();
    zdt2 = zdt2.toLocalTime();
  }
  switch (compareOp) {
    case 'before':
      return zdt1.isBefore(zdt2);
    case 'equal':
      return zdt1.equals(zdt2);
    case 'after':
      return zdt1.isAfter(zdt2);
    case 'beforeEqual':
      return zdt1.isBefore(zdt2) || zdt1.equals(zdt2);
    case 'afterEqual':
      return zdt1.isAfter(zdt2) || zdt1.equals(zdt2);
  }
}


if (zdtCompare((time.ZonedDateTime.now()), (time.toZDT(items.getItem('MyItem'))), 'after', 'seconds', 'dateandtime')) {
  items.getItem('MyItem').sendCommand('value');
}

DateTimes are complicated so that probably wasn’t the best demonstration. But I think you get the point. It’s not zero knowledge by any means. You have to know what core concepts are in OH to know where to look. But just seeing the choices goes a long way towards helping you get going.

I also want to point out the existence of rule templates. I in particular like to create templates that solve generic but common problems (e.g. do something when an Item remains in a state for a given amount of time, like a reminder when a door is left open or run a state machine based on the time of day). Some of the more complicated common home automation problems don’t require much if any coding at all on your part. You can instantiate and configure a rule template to do the complicated stuff (detect when a door remains open for a given amount of time) and just focus on the parts that are unique to you (e.g. what to do when that door is open for more than that amount of time).

I do think LLMs will become good enough eventually. The whole “private GPTs” ChatGTP is doing looks promising. But for now, they just are not good enough.

it’s a bit OT, but:
the following picture is supposed to be a cartoon, but it pretty much sums up “generative AI” atm.
There’s some activities like Github CoPilot or the likes and they’re getting specifically learned with specific programming languages - but IMHO the whole AI stuff is just overselled with an underwhelming experience. Sure enough, some “dull” activities can be supported with AI, but unless you’ll make AI “really” sentient, it will only reproduce similar errors done by millions of people…

that being said: no, AI generated openHAB rules will not be something you want to use in your “production environment” - without spending hours to correct them…

True, it’s generally oversold. But I mean “niche uses” here and there specifically. I think it’s kind of cool for that: that is, if you find such a use, know the “good prompt” and expect the result only within the boundaries of its capabilities, and not expecting it to do a lot. I mean, it’s something like Excel to me: if I know how to use it, it can give nice insights.

I’ve found it helpful to develop a framework of how code would flow by describing it in plain English, adding features one at a time so I can see what it is doing. Then move the code into an IDE and flesh it out with real parameters/commands. I’m writing rules in Python, but you can just ask for the same code in Javascript.

What I’ve found very useful, is asking it to develop regex expressions. Not only does it create them but also describes how each command works. I’m sure I’ve just always had an irrational fear of regex, but this was liberating for me.

For writing scripts, you just need an accurate prompt if your prompt is accurate your results will be great. At the start, I faced this problem a lot. After some time, I got a tool that generates prompts for you. That tool helped me a lot and saved me a lot of time.

This smells like a generic spam.