Logic Machine Forum
Script working as "resident" but not "scheduled" - 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: Script working as "resident" but not "scheduled" (/showthread.php?tid=5856)



Script working as "resident" but not "scheduled" - jerryhenke - 23.01.2025

Hi.
I am running a PID-controller from a resident script, every 60 seconds.
However my water temperature change is quite slow changing and I try to run the same script with the same PID-controller from scheduled scripts instead.

I have now copied all code and disabled the resident script.
However, when i schedule this script it doesn't run.
No log, nothing. Even if i klick "run script" nothing happens. 
Other scheduled scripts works just fine.

Any clues?

Code:
-- Resident: Golvvärme styrvärde regulator pid nedan:

-- Ange avrundningssteget här (t.ex. 0.1, 0.2, 0.25, 0.5, etc.)
local round_step = 0.20  -- Du kan ändra detta till det steg du vill ha..

-- Funktion för att avrunda till närmaste "step"
local function round_to_nearest(value, step)
  return math.floor(value / step + 0.5) * step
end

-- Variabel för att slå på eller av temperaturskillnadskontrollen
local temperature_diff_enabled = false  -- Sätt till 'true' för att kontrollera temperaturskillnaden, 'false' för att inaktivera

-- Hämta nuvarande och börvärde från sensorer och avrunda med angivet steg
local current_value = round_to_nearest(grp.getvalue('5/5/1'), round_step) -- Framledning ärvärde
local setpoint_value = round_to_nearest(grp.getvalue('5/5/0'), round_step) -- Framledning börvärde

-- Kontrollera temperaturskillnad efter avrundning
local temperature_diff = math.abs(setpoint_value - current_value)
local limit = 0.20 -- Gränsen för temperaturskillnad

-- Initiera PID om det behövs
if not p then
  require('user.pid')
  p = PID:init({
    current = '5/5/1', -- Framledning ärvärde
    setpoint = '5/5/0', -- Framledning börvärde
    output = '5/5/15',  -- GV styrvärde
    round_step = round_step -- Här skickas round_step till PID
  })
end

-- Kontrollera om temperaturskillnaden ska användas för att köra PID
if temperature_diff_enabled then
  -- Endast kör PID om temperaturskillnaden är signifikant
  if temperature_diff > limit then
    local previous_output = p.output
    p:run()

    if p.output ~= previous_output then
      -- Logga värdena tillsammans med styrvärdet
      log(string.format("Styrvärde skickat till 5/5/15: %.2f (Ärvärde: %.2f, Börvärde: %.2f, Skillnad: %.2f°C)",
                        p.output, current_value, setpoint_value, temperature_diff))
    end
  else
    log("Ingen signifikant avvikelse (" .. string.format("%.2f", temperature_diff) .. "°C), limit " .. string.format("%.2f", limit) .. "°C, PID ej körd. (Börvärde: " .. string.format("%.2f", setpoint_value) .. "°C, Ärvärde: " .. string.format("%.2f", current_value) .. "°C)")
  end
else
  -- PID körs alltid, oavsett temperaturskillnad
  local previous_output = p.output
  p:run()

  if p.output ~= previous_output then
    -- Logga värdena tillsammans med styrvärdet
    log(string.format("Styrvärde skickat till 5/5/15: %.2f (Ärvärde: %.2f, Börvärde: %.2f, Skillnad: %.2f°C)",
                      p.output, current_value, setpoint_value, temperature_diff))
  end
end



RE: Script working as "resident" but not "scheduled" - admin - 23.01.2025

PID script needs to run in a loop to store the current state so scheduled script cannot be used.
Use a resident script with an extra os.sleep() at the end to make the total sleep interval longer.


RE: Script working as "resident" but not "scheduled" - jerryhenke - 23.01.2025

(23.01.2025, 11:28)admin Wrote: PID script needs to run in a loop to store the current state so scheduled script cannot be used.
Use a resident script with an extra os.sleep() at the end to make the total sleep interval longer.

Amazing. Thanks.

(23.01.2025, 11:28)admin Wrote: PID script needs to run in a loop to store the current state so scheduled script cannot be used.
Use a resident script with an extra os.sleep() at the end to make the total sleep interval longer.

One more question.
Should I still have it as resident with 60 seconds? will there not be a conflict if i do like this?

Quote:-- Resident: Golvvärme styrvärde regulator pid nedan:

-- Ange avrundningssteget här (t.ex. 0.1, 0.2, 0.25, 0.5, etc.)
local round_step = 0.20  -- Du kan ändra detta till det steg du vill ha.

-- Funktion för att avrunda till närmaste "step"
local function round_to_nearest(value, step)
  return math.floor(value / step + 0.5) * step
end

-- Variabel för att slå på eller av temperaturskillnadskontrollen
local temperature_diff_enabled = false  -- Sätt till 'true' för att kontrollera temperaturskillnaden, 'false' för att inaktivera

-- Hämta nuvarande och börvärde från sensorer och avrunda med angivet steg
local current_value = round_to_nearest(grp.getvalue('5/5/1'), round_step) -- Framledning ärvärde
local setpoint_value = round_to_nearest(grp.getvalue('5/5/0'), round_step) -- Framledning börvärde

-- Kontrollera temperaturskillnad efter avrundning
local temperature_diff = math.abs(setpoint_value - current_value)
local limit = 0.20 -- Gränsen för temperaturskillnad

-- Initiera PID om det behövs
if not p then
  require('user.pid')
  p = PID:init({
    current = '5/5/1', -- Framledning ärvärde
    setpoint = '5/5/0', -- Framledning börvärde
    output = '5/5/15',  -- GV styrvärde
    round_step = round_step -- Här skickas round_step till PID
  })
end

-- Kontrollera om temperaturskillnaden ska användas för att köra PID
if temperature_diff_enabled then
  if temperature_diff > limit then
    local previous_output = p.output
    p:run()

    if p.output ~= previous_output then
      log(string.format("Styrvärde skickat till 5/5/15: %.2f (Ärvärde: %.2f, Börvärde: %.2f, Skillnad: %.2f°C)",
                        p.output, current_value, setpoint_value, temperature_diff))
    end
  else
    log("Ingen signifikant avvikelse (" .. string.format("%.2f", temperature_diff) .. "°C), limit " .. string.format("%.2f", limit) .. "°C, PID ej körd. (Börvärde: " .. string.format("%.2f", setpoint_value) .. "°C, Ärvärde: " .. string.format("%.2f", current_value) .. "°C)")
  end
else
  local previous_output = p.output
  p:run()

  if p.output ~= previous_output then
    log(string.format("Styrvärde skickat till 5/5/15: %.2f (Ärvärde: %.2f, Börvärde: %.2f, Skillnad: %.2f°C)",
                      p.output, current_value, setpoint_value, temperature_diff))
  end
end

-- Lägg till en paus på 120000 millisekunder (2 minuter)
os.sleep(120000)



RE: Script working as "resident" but not "scheduled" - admin - 23.01.2025

Sleep time is set in seconds. Resident script executes os.sleep after each loop so just keep in mind this additional delay when adding your own.