Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Code using Chat GPT?


Question

Posted (edited)

Has anyone tried using Chat GPT to write the Lua code?

I know very little coding, just started learning. Out of curiosity I've tried asking ChatGPT4 to write lua code for Fibaro to, for example, turn on a light if a pir sensor has breached and turn it off again if the sensor hasn't been breached for 10 mins.

It does write some code and includes comments in the code for where you need to add specific vales (Node ID's etc) but I've not had success with this slightly complicated task with the code it has made yet, but I'm still trying. You do need to understand what your system and hardware is doing, for instance, my pir sensor when tripped puts out a value of 1 and stays at 1 until 5 seconds after it stops sensing motion, then resets to 0 and so you might need to tell it to take that into account.

I did get my brother to come over and write some code, he writes computer games, but in C++ and isn't familiar with either Lua or Fibaro. It did take us about 6 hours to get a bit of code to switch on a light if the pir was breached and only turn it off if it hadn't been breached for X amount of time and without throwing up the too many instances error. He will be much quicker when I next get him over to write something - if I haven't learnt how to do it myself inbeween.

Still, anybody tried ChatGPT? I was surprised it knew about Fibaro to be honest, but it did.

Edited by MartW
spelling/grammer errors
  • Thanks 1

12 answers to this question

Recommended Posts

  • 0
Posted

thinking out loud... have asked GPT to solve easy solution fizzbuzz (just googling if you do not know what is). 

this task I am always asking on code interviewing any Middle developers...

so, GPT provide me code... So, right now I am really can relax... With this code I will have my job at least until old age.

:) just crazy to think what can provide GPT for Fibaro LUA scripting 😃 

  • Like 2
  • 0
Posted (edited)

ChatGPT writes sh*tty code. I've tried a few times, but it's useless and it doesn't improve it's coding when I correct it's faults.

Edited by Bodyart
  • Like 1
  • 0
Posted

Use it as a scratchpad, and manually build from it.

 

 Welcome back @10der, hope you are ok

  • Like 1
  • Thanks 1
  • 0
  • Inquirer
  • Posted
    41 minutes ago, Joep said:

    Use it as a scratchpad, and manually build from it.

     

     Welcome back @10der, hope you are ok


    Yeah, as a total beginner I wondered if that might be a viable idea, it might give me something to bash at that would have taken me longer to come up with - mix that in with looking at the code made by fibaro from the blocks/begin learning lua and a noob might be able to knock something together more advanced that he might otherwise. I am sceptical though at this stage.

     

    5 hours ago, Bodyart said:

    ChatGPT writes sh*tty code. I've tried a few times, but it's useless and it doesn't improve it's coding when I correct it's faults.

    I'm not able to disprove this statement, or even demonstrate some working code from my limited tries, but some of that is asking the question corerctly if what you want is slightly complicted. Us non coders often don't think in the logical way coders understand they need to do when coding.

    • 0
    Posted

    to start use block scene and convert in LUA

    • Thanks 1
    • 0
    Posted

    I have used it a lot. It is good at knowing some things about Fibaro. It is really good when making refactors of small blocks of code or a few functions, logic structures, etc... and doing so very quickly. It can often make incorrect changes or suggest things that don't and can't exist in Home Center. I would say if you don't know how to code well you should get it to make very small statements and validate they work before building upon it. It can fail to grasp the obvious but it is still super helpful.

     

    It is very helpful in refactoring and abstracting functions. Also since I am new to LUA and it knows that very well it is easy for me to ask it what I want to do and I know enough to see if I got what I asked for.

    • 0
    Posted

    sorry about lame question but how you use ChatGPT: from

    Please login or register to see this link.

        ?

    or from:

    Please login or register to see this link.

       ?

    is it the same?

     

    I used from bing, and it seems like it uses web search to find best solution, but it have many bugs:

     - it finds no no differencies between HC2 Lua and HC3

     - there are many syntax errors etc.

     

    • 0
  • Inquirer
  • Posted

    In line with what has been written above, I'm having some succcess in taking bits of code it writes and putting it into a scence, or asking it to make a very simple scene so that I can see what that little bit of code should look like and add that into a scene. Also, as Sjakie suggests, make a blocky scene and convert that to Lua and build on that.

    I still haven't had any success in using a complete scene it has written (obviously I've made my system specific changes, ID numbers etc) but I have to caveat that with saying I'm still a noob and it's code might have been closer than I know - I sat there for half an hour yesterday before I spotted a missed comma in code I had writted (or was ammended by me) and later I only understood I was trying to compare a number to a string when my brother pointed that out. But I have been able to write a scene that makes my Sonos speaker come on when a pir is breached and switch of X amount of minutes after the last breach and make the room lights (if the light is below a threshed) do similar and it does that without throwing uo the "to many instances" notification.

    You can chuck bits of code into ChatGPT and it will explain what they are and what they do and it will flag up some syntax errors (don't know how good it is at that though since my knowledge is weak).

    My coding brother noted that sometimes the code it produced look good, was well written, more like natural speaking so the coding was clearer to understand, and other times not so much. So a risk might be that you end up with a bit of a frankenstein of code base in your scenes that is not consistent, whereas an experienced coder will have made scenes that read the same across the system and would be easier to maintain I guess, but then anything I write will be a bit of a mess anyway.

    • 0
    Posted (edited)

    I would say the answers of chatgpt are like

     

    Prisencolinensinainciusol

     

    Named after a famous song by A. Celentano. Sounds like a real programming language, but it is not (EDIT: quite a few users have said, it "hallucinates", it makes up facts, constructs and references).

     

    Edited by petergebruers
    • Like 3
    • 0
    Posted
    On 4/3/2023 at 3:46 PM, MartW said:

    Has anyone tried using Chat GPT to write the Lua code?

    I know very little coding, just started learning. Out of curiosity I've tried asking ChatGPT4 to write lua code for Fibaro to, for example, turn on a light if a pir sensor has breached and turn it off again if the sensor hasn't been breached for 10 mins.

    It does write some code and includes comments in the code for where you need to add specific vales (Node ID's etc) but I've not had success with this slightly complicated task with the code it has made yet, but I'm still trying. You do need to understand what your system and hardware is doing, for instance, my pir sensor when tripped puts out a value of 1 and stays at 1 until 5 seconds after it stops sensing motion, then resets to 0 and so you might need to tell it to take that into account.

    I did get my brother to come over and write some code, he writes computer games, but in C++ and isn't familiar with either Lua or Fibaro. It did take us about 6 hours to get a bit of code to switch on a light if the pir was breached and only turn it off if it hadn't been breached for X amount of time and without throwing up the too many instances error. He will be much quicker when I next get him over to write something - if I haven't learnt how to do it myself inbeween.

    One option I’ve found helpful when working with code and trying to get more tailored suggestions is using platforms like

    Please login or register to see this link.

    . It’s another AI-powered tool that can help with generating and refining code, giving you the flexibility to modify it as needed. OverChat.ai is designed to assist with coding tasks, and it could help you focus more on your specific system rather than generalized suggestions.

    Still, anybody tried ChatGPT? I was surprised it knew about Fibaro to be honest, but it did.

    I’ve definitely used ChatGPT for Lua code before, and I agree that it’s a great tool for generating initial code snippets and explaining concepts, especially when you’re just getting started. It’s impressive how it can produce code and even leave helpful comments about where you need to add specific values like Node IDs. However, as you’ve discovered, there’s a bit more to it when you need to handle specific hardware behaviors or more complex tasks, like the PIR sensor logic you’re working with.

    The key with AI-generated code is that it can give you a solid starting point, but you often need to tweak it based on your own knowledge of the system and its specifics. In your case, understanding how the PIR sensor works—like how it stays at 1 for a while and then resets—definitely requires a little more customization to the code, something that’s tricky for AI to get right without all the context.

    It’s great that you have a brother who can help with the coding, even if he’s more familiar with C++. It sounds like you're making good progress, and with time, you’ll probably be able to refine the code yourself. The more you understand the system’s behavior, the easier it will be to work with the code, whether you use ChatGPT or not. Keep at it! It’s all part of the learning process.

    • 0
    Posted (edited)

    Here's my "adventure" with AI. I had to produce a virtual device which can make a simple engine driven garage door smart. Door's engine is not smart itself and is moving as usual in such cases - from stop: impulse:start one direction->impulse: stop->impulse: start other direction etc. There's also access to the function returning state of the gate drive (working, not working by power measurement) and two door sensors - gives door status: closed, open, middle, malfunction. The handler have to be able to maintain 3 commands: open, close, stop, and - emergency stop generated out of handler (proper re-start after such event).

    The goal was to build the logic around this. As virtual device the handler have to be absolutely reliable, because of remote use in other QA's. Not trivial case, but also quite limited factors to handle.

     

    Knowing that I can create this myself, I decided to test the AI.
    What was the result? ChatGPT o3-mini high (paid most advanced model) model completely failed with this job. At first the code was promising, but still buggy. I was stubborn! Two days of explaining what AI forgot... It only got worse, until completely broke down - without any chance of improvement. The mistakes were pilling up, AI corrected one, but ruined the other. At the end - displaying "new" approach, identical to this, which was wrong - in a closed loop. Every solution was given as a it's The Truth Revealed.

    So, as a tutor (repeated knowledge from 'human' sources) - AI is very good, language model is really considerable. As a creator - very weak. Additionally, AI has no ability to test the code in the 'sandbox', only 'evaluate' code logic in it's silicon mind. This limits it much when complication raise more that 'very little'.

    Edited by Łukasz997
    • 0
    Posted (edited)

    I tested Cursor with the Claude-3.5-3.7 models, and it was quite satisfactory.

    However, I still needed to correct the AI regarding coding conventions and specific (Fibaro) framework-related issues. Usually, this correction was immediate.

    As a result, there was a lot of “nagging” involved. It felt like being out driving with the mother-in-law, and I was the mother… ;-)

    It’s worth noting that Claude is quite good at explaining its actions.

    I asked the AI (Claude) to create a timer that executed a specific task every midnight. Initially, it generated a setInterval(onMidnight, 24 * 3600) call. However, setInterval eventually start drifting, firing one second after midnight, then two seconds after midnight, and so on.

    To address this issue, I asked Claude to make the timer drift-free. It provided an explanation for this:

    Please login or register to see this code.

     

    The correctly generated code looked like this:

    Please login or register to see this code.

     

    The AIs have great potential to excel in coding patterns and best practices, as they learn these from the training material. A drift-free timer is a typical example of such a pattern or best practice.

    However, it’s still often challenging to apply coding patterns and best practices in real-world scenarios. This is why many IT projects have been difficult and have often failed over the past 50+ years.

    In some cases, requirements may not be fully known, leading to inadequate code. Alternatively, the system might have more (hidden) states than its design anticipated.

    Therefore, determining the logic for door opener states can be challenging. Different door openers have varying states and timing. When you request AI code, you might not provide all the requirements (states and behaviors), or you may not fully comprehend them yourself.

    It’s paradoxical if this is the reason you sought AI assistance. If you truly understand how the door opener works, you wouldn’t need AI. In fact, if you knew all the states and behaviors, you could write the code yourself.
     

    AI is proficient in translating working code from one language to another. So, if your door opener code was in Python, it could likely perform a good job of translating it to a working Fibaro QA.

    The ideal approach, though potentially impractical, would be to train the AI by letting it observe the actual door and door opener impulses, and then suggest code. This way, the AI could discover the requirements and behaviors on its own.
     

    My favorite home automation pattern is “turn on the light when someone enters the bathroom and turn off the light when no one is there.” It’s simple to explain but challenging to implement. The implementation depends on the sensors available, the bathroom’s layout, user behavior, and various other factors (and a combination of them).

    When you believe you’ve got it right, you might hear your wife scream that she’s in the bathroom and the light is off. Don’t blame the AI for that!  :-) 

     

    Edited by jgab
    • Like 1

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...