Logic Machine Forum
LUA scripting using ChatGPT for the LogicMachine - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: LUA scripting using ChatGPT for the LogicMachine (/showthread.php?tid=4780)



LUA scripting using ChatGPT for the LogicMachine - managementboy - 22.05.2023

Hi everyone,

just tried talking to ChatGPT to ask it to code me a script to get my external IP, store it locally and if there is a change to update my no-ip dyndns for me.

Check out this code it returned on the first try:

Code:
-- Import required libraries
local http = require("socket.http")
local ltn12 = require("ltn12")
local mime = require("mime")
local storage = require("storage")

-- Define your No-IP account details
local username = "your_noip_username"
local password = "your_noip_password"
local hostname = "your_noip_domain"

-- Define the URL for retrieving the external IP address
local ipUrl = "http://api.ipify.org/"

-- Perform the request to retrieve the IP address
local response = {}
local result, status = http.request {
  url = ipUrl,
  method = "GET",
  sink = ltn12.sink.table(response)
}

-- Check the response status
if status == 200 then
  local externalIP = table.concat(response)
  log("External IP address: " .. externalIP)
 
  -- Retrieve the stored IP address from LogicMachine storage
  local storedIP = storage.get("externalip")
 
  -- Compare the retrieved IP with the stored IP
  if storedIP == externalIP then
    log("No change in IP address. No update needed.")
    return
  end
 
  -- Save the updated IP address to LogicMachine storage
  storage.set("externalip", externalIP)
  log("IP address updated and saved to LogicMachine storage.")
 
  -- Continue with No-IP update code
  -- Define the update URL with the retrieved IP address
  local updateUrl = "http://dynupdate.no-ip.com/nic/update?hostname=" .. hostname .. "&myip=" .. externalIP
 
  -- Define the authentication header
  local auth = username .. ":" .. password
  local authHeader = "Basic " .. (mime.b64(auth))

  -- Define the request headers
  local headers = {
    ["Authorization"] = authHeader
  }

  -- Perform the update request
  local updateResponse = {}
  local updateResult, updateStatus = http.request {
    url = updateUrl,
    method = "GET",
    headers = headers,
    sink = ltn12.sink.table(updateResponse)
  }

  -- Check the update response status
  if updateStatus == 200 then
    local responseBody = table.concat(updateResponse)
    log("Update successful. Response from No-IP: " .. responseBody)
  else
    log("Update failed. Status code: " .. updateStatus)
  end
else
  log("Failed to retrieve the external IP address. Status code: " .. status)
end

the only change necessary was removing the
 local storage = require("storage")
line and it runs in a resident scrip without fail!


RE: LUA scripting using ChatGPT for the LogicMachine - domotiqa - 07.11.2024

cool, what was the prompt you use ?


RE: LUA scripting using ChatGPT for the LogicMachine - Erwin van der Zwart - 07.11.2024

O god please don’t go this way, i’m starting to get scripts from SI’s that also created them via ChatGPT, but they don’t work and asking me to check it, i came to a point that my advise is: put it back in ChatGPT and ask why it does not work.. In other words, if you go this way without any idea what you are doing because you think AI will solve your issues, sorry your on your own..


RE: LUA scripting using ChatGPT for the LogicMachine - domotiqa - 13.11.2024

Yeah but it is important to see what these AI can do. Some customer start to speak about HA and AI. I already did integration between LM sending email to chatgpt with csv to analyse. A good point is that with all his API LM is still a good solution.


RE: LUA scripting using ChatGPT for the LogicMachine - Ranjeet - 21.11.2024

Hii Sir ,

I have created visualization where when i created area (Master Bedroom).on left side of this area i have put light icon with object. When we click on this area it will go to master bedroom page where all lights are given
So I need lua script where if any Lights are ON in this area then it should show on master bedroom left icon which i created.if lights are off then it should also reflect on icon


RE: LUA scripting using ChatGPT for the LogicMachine - admin - 21.11.2024

Please create a separate thread next time since your question is not relevant to this topic.
Use this script for central status: https://kb.logicmachine.net/scripting/logic-functions-central-statuses/