All electronic skill persons know a switched psu needs a "cool" down to drain internal accumulated power BEFORE switching on again. Otherwise you risk to burn your electronics. (many psu like examples can be found)
This is a universal solution for that, its a replacement for commandArray for this device :
Code: Select all
-- ============================================================
-- Protection system (toggle‑protection for power supplies)
-- Stored in RAM‑drive: Z:\temp\protect_state.json
-- ============================================================
protect = {} -- global so all scripts can use it
local PROTECT_DEFAULT = 10 -- seconds
local PROTECT_FILE = "Z:\\temp\\protect_state.json"
-- Load protection state from JSON
local function protect_load()
local f = io.open(PROTECT_FILE, "r")
if not f then return {} end
local content = f:read("*a")
f:close()
if content == "" then return {} end
return json.decode(content)
end
-- Save protection state to JSON
local function protect_save(tbl)
local f = io.open(PROTECT_FILE, "w")
if not f then return end
f:write(json.encode(tbl))
f:close()
end
-- Main function: safeSet(device, action, seconds)
function protect.safeSet(device, action, seconds)
local now = os.time()
local protect_time = seconds or PROTECT_DEFAULT
local key = "protect_until_" .. device
-- Load current state
local st = protect_load()
local protect_until = st[key] or 0
-- OFF → always execute + start protection window
if action == "Off" then
st[key] = now + protect_time
protect_save(st)
commandArray[device] = "Off"
print("safeSet: OFF executed, protection active for "..device)
return
end
-- ON → block or execute
if action == "On" then
if now < protect_until then
print("safeSet: ON blocked for "..device.." (protection active)")
return
end
-- Protection expired → execute ON
cArray[device] = "On"
print("safeSet: ON executed for "..device)
return
end
-- Any other action → pass through
commandArray[device] = action
end
Code: Select all
protect.safeSet("Device", "Off", X)
protect.safeSet("Device", "On")
At the off command it writes a json (can be a uservar if you prefer that = easy to change, but i noticed the json write to my rammdrive is much faster compaired to uservar setting) The json written contains a safety window starttimer (UNIX time)
When a script tries to switch the device on, it will check if its allready allowed to switch on. If not it ignores the command.
This can be a constrain --> it is no longer a switch on command and forget : you need to keep running protect.safeSet("Device", "On") untill its safetytimer has expired. So you need to think about this in your flow design in lua.
NOTICE : in order to make it work as expected all commandArray for this device should be replaced by protect.safeSet() !
Any commandArray="On" will completely overide the protection window and possible break your psu
Use at your own risk ! Electronics break easy : so think twice, do once --> Please test with a dummy device before putting in production...
My coding skills are very limited, so there can be errors inside ( for me it works at this moment)