Logic Machine Forum
Help with timer/time in LUA 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: Help with timer/time in LUA script (/showthread.php?tid=2935)



Help with timer/time in LUA script - stigen - 24.10.2020

Hi!


I am trying to controll my heating cable outside the front door, for melting the snow in winter.

I pulling the weather forecast in node red and get a "true" signal if the forecast says snow, and "false" it not.
I also want the outdoor temperature to control the time for the heating cable. ( so its not ON all the time)

My plan is to have OFF, AUTO and ON mode.
In OFF mode its forced off.
In ON mode its forced on.
In AUTO i want the outside temperature and forecast to control the "on" time.

Like, if the forecast is "true" and the temperature is between +4 and 0, the heating is ON in 25 minutes and OFF in 35 minutes, then repeat as long as the temperature is in range.

Then if the forecast is "true" and the temperature is between 0 to -4, the heating is ON for 35 minutes and OFF for 25 minutes, then repeat.

And if the forecast is "true" and the temperature is between -4 to -8, the heting is ON for 50 minutes an OFF for 10 minutes then repeat.

Something like that.

So how can i do this in LUA script, to get the times right?

i tried with os.sleep in resident script, but that doesnt work.

Any ideas? Cool


RE: Help with timer/time in LUA script - Daniel - 26.10.2020

I assume you know how to write your conditions. Very simple solution for your time on/Off is just create 6 scheduled scripts. One for on and one for off for each condition and set minute in which you want to send on or off. In your script based on which condition just enable pair of the scheduled scripts and disable when condition is not meet.


RE: Help with timer/time in LUA script - admin - 26.10.2020

Create a scheduled scripts that runs every minute.
Change group addresses as needed:
1/1/1 - mode (2 = auto)
1/1/2 - forecast (true/false)
1/1/3 - temperature value
1/1/4 - output control

To simplify things the output will turn on at 0 minutes of every hour if needed and will turn off after X minutes depending on the current temperature.
Code:
mode = grp.getvalue('1/1/1')
if mode ~= 2 then
  return
end

forecast = grp.getvalue('1/1/2')
temperature = grp.getvalue('1/1/3')
if not forecast or temperature > 4 then
  offtime = 0
-- 0..4
elseif temperature >= 0 then
  offtime = 25
-- -4..0
elseif temperature >= -4 then
  offtime = 35
-- -8..-4
elseif temperature >= -8 then
  offtime = 50
-- lower than -8
else
  offtime = 60
end

min = os.date('*t').min
value = min < offtime
grp.checkwrite('1/1/4', value)