Logic Machine Forum
Problem runnig script - 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: Problem runnig script (/showthread.php?tid=5071)



Problem runnig script - spoty2008 - 02.11.2023

Hello. There is a problem with script execution. I can't figure out what the reason is.


Code:
local http = require("socket.http")
local json = require("json")
local url = "https://api.openweathermap.org/data/2.5/weather?lat=**,75&lon=**,61&appid=*******35447a18d48ad0b&units=metric"

function createOrUpdateScript(name, script, time)

local h_time, m_time = string.match(time, "(%d+):(%d+)")
local data = {
    type = 'scheduled',
    name = name,
    active = 1,
    params = string.format("%d %d * * *", m_time, h_time),
    subparams = 0,
    --script = '',
    category = 'Service',
    description = '',

 
  local exists = script.get(data.name)
        --log(exists)
    if exists ~= nil then
        log(data.name, 'Script exists, updating')
        data.id = exists.id
        db:update('scripting', data, { id = exists.id })
    else
        log(data.name, 'Script does not exist')
        db:insert('scripting', data)
        data.id = db:getlastautoid()
    end

    script.save(data, true)
    script.reloadsingle(data)
end
--END FUNCTION
    

local response, status, headers, statusline = http.request(url)

if status == 200 then
   
    local data = json.decode(response)
--log(data)
      
      temp_min = data.main.temp_min
      TempReal = data.main.temp
      wind = data.wind.speed
      dusk = os.date("%H:%M",data.sys.sunset)
      sunrise = os.date("%H:%M",data.sys.sunrise)
      rain = data.weather[1].main
      createOrUpdateScript("Light_On", script, dusk)
      os.sleep(0.5)
        createOrUpdateScript("Light_Off", script, sunrise)
      storage.set('temp', temp_min)
      
      grp.write('2/2/5', wind)
      grp.write('2/2/0', TempReal)
      log('temp real ', TempReal)
 
else
    -- HTTP
    log("Errore. Status: " .. status)
      telegram('Errore fetch weather. status: ' .. status)
end


if rain == "Clear" or rain == "Clouds" then
 
  grp.write('2/2/4', false)
  else
 
  grp.write('2/2/4', true)
  end

if (1 >= TempReal) and ( TempReal >= -10) then
  grp.write('2/0/0', true)
 
      else
  grp.write('2/0/0', false)
 
end

This code creates 2 scripts with the current sunrise and sunset times. I will not provide the contents of the created scripts, but if necessary I will provide them. The content is simple: turn the light group on or off. And the problem is that sometimes they just don't get done. I don't understand the reason...
Entry in script: log('done') doesn't output anything to the log.
Perhaps I missed something when creating the script? I checked it on 2 different LMs. The firmware on the devices is the same for 2023.

I'll add. The script is allowed and when I try to launch using the button in the browser, the launch is also unsuccessful, the script simply does not execute. The only way to run it is:
disable -> save -> enable -> run


RE: Problem runnig script - admin - 02.11.2023

You can use scheduler events tied to sunrise/sunset instead of creating scheduled scripts.


RE: Problem runnig script - spoty2008 - 02.11.2023

(02.11.2023, 13:53)admin Wrote: You can use scheduler events tied to sunrise/sunset instead of creating scheduled scripts.

Yes, I can) But intresting why not working..
in future maybe i need create script dynamic and i'm will stuck