This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

LUA scripting using ChatGPT for the LogicMachine
#1
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!
Reply
#2
cool, what was the prompt you use ?
-----------
FRANCE SMARTHOME & SMARTBUILDING INTEGRATION
SE ECO EXPERT
Reply
#3
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..
Reply
#4
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.
-----------
FRANCE SMARTHOME & SMARTBUILDING INTEGRATION
SE ECO EXPERT
Reply
#5
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
Reply
#6
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/lo...-statuses/
Reply


Forum Jump: