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.

Resident script pump circulation
#1
Hi,
I did a resident script that every minute checks the status of 2 valves and when one of them goes ON, a pump has to be start after 5 minutes. 

Instead if valves are both OFF pump has to be stop immediately.

Considering that I have to use a os.sleep() command, I would like to avoid to run many script in parallel.

What do you suggest?

Thanks.
Reply
#2
yHi,

Resident scripts don’t run in parallel, only event based do that.

BR,

Erwin
Reply
#3
Do you mean that if script starts every minute and inside it there's a os.sleep() of 5 minutes, the second script doesn't start? Thanks
Reply
#4
Scripts never block each other. You can safely use os.sleep in resident scripts. Though for larger delays I recommend using scheduled scripts.
Reply
#5
Hi Admin,
You're right but I cannot use a scheduled script. Otherwise I risk to want 10 minutes before starting the pump.
I need to don't run a script if the previous one is running. What do you suggest??
Thanks.
Reply
#6
1. Add tag valve to your valve objects. Create an event script that is mapped to valve tag:
Code:
-- get OR'ed value of all tagged objects
value = false
objects = grp.tag('valve')
for _, object in ipairs(objects) do
  value = value or object.value
end

if value then
  -- save pump timer if timer has not been started yet
  time = storage.get('pump_on_time', 0)
  if time == 0 then
    storage.set('pump_on_time', os.time())
  end
else
  -- turn pump off
  storage.set('pump_on_time', 0)
  grp.checkwrite('pump', false)
end

2. Create either a resident script with large sleep time or a scheduled script that runs every minute:
Code:
-- turn pump on when time delta >= 5 minutes
time = storage.get('pump_on_time', 0)
if time > 0 then
  delta = os.time() - time

  if delta >= 5 * 60 then
    grp.checkwrite('pump', true)
  end
else
  grp.checkwrite('pump', false)
end

In both scripts replace pump with pump object name or group address.
Reply
#7
Code:
-- check if previous script needs to be killed
script_pompa_circolazione_notte = 'Pompa_circolazione_notte'
pompa_notte_pid = storage.get(script_pompa_circolazione_notte)  
if pompa_notte_pid == nil then
   pompa_notte_pid_os = os.getpid()
   storage.set(script_pompa_circolazione_notte, pompa_notte_pid_os)
else
   pompa_notte_pid_os = os.getpid()  
   storage.set(script_pompa_circolazione_notte, pompa_notte_pid_os)  
   os.kill(pompa_notte_pid, signal.SIGKILL)
end

-- put here your script with os.sleep
Valvola_zona_notte = grp.getvalue('2/0/106')
os.sleep(0.5)
Valvola_bagno_zona_notte = grp.getvalue('2/0/105')
if Valvola_zona_notte or Valvola_bagno_zona_notte == true then
   os.sleep(360) -- attendi 360 secondi poi avvia la pompa di zona
grp.write('2/0/3', true)
else
 grp.write('2/0/3', false)
end

-- end of your scrript

-- clean up storage
storage.delete(script_pompa_circolazione_notte)

Surfing on the forum I found an instruction to avoid to add running script. Here above what I did.

It seems working. What I would like to do is kill the os.sleep time (360 seconds) if in the meanwhile logic OR is false.

I hope I have been cleared.

Thanks.
Reply
#8
You script is not fully correct. Each telegram sent to valve objects will reset wait timer. In some egde cases pump can never be turned on.
Reply
#9
Hi Admin, you,'re right. In fact this morning pump did not srart. Can you suggest me how to update the script?
Thanks.
Reply
#10
You can do it also as resident and use tag 'circulation_pump' for valves:
Code:
valveStatesTag = 'circulation_pump'
pumpGA = '1/1/10'

opened, first_timestamp = false, 0

valveStates = grp.tag(valveStatesTag)
for v = 1, #valveStates, 1 do
 valveState = valveStates[v]
 if valveState.value then
    opened = true
    if first_timestamp > 0 then
        if first_timestamp > valveState.updatetime then first_timestamp = valveState.updatetime end
    else
        first_timestamp = valveState.updatetime
    end
 end
end

if opened then
    opened_5_minutes = (os.microtime() - first_timestamp) >= 600
    if opened_5_minutes then
        grp.checkwrite(pumpGA, true)
    else
        grp.checkwrite(pumpGA, false)
    end
else
    grp.checkwrite(pumpGA, false)
end
Done is better than perfect
Reply
#11
Thank you. Which resident time do you suggest?
Reply
#12
For such think I like use 1 minute interval but if you want better reaction you can set less time like 30 or 15s oryou can adjust further this script e.g. you can choose Resident 0s and create auto adjusting the time for next calculation like below:

Code:
valveStatesTag = 'circulation_pump'
pumpGA = '1/1/10'

opened, first_timestamp = false, 0

valveStates = grp.tag(valveStatesTag)
for v = 1, #valveStates, 1 do
 valveState = valveStates[v]
 if valveState.value then
   opened = true
   if first_timestamp > 0 then
       if first_timestamp > valveState.updatetime then first_timestamp = valveState.updatetime end
   else
       first_timestamp = valveState.updatetime
   end
 end
end

if opened then
   opened_5_minutes = (os.microtime() - first_timestamp) >= 300
   if opened_5_minutes then
       grp.checkwrite(pumpGA, true)
        nextCalculationAfter = 30
   else
       grp.checkwrite(pumpGA, false)
        nextCalculationAfter = 301 - (os.microtime() - first_timestamp) -- run a little after possible change
   end
else
   grp.checkwrite(pumpGA, false)
    nextCalculationAfter = 301
end

log('Next calculation after: ' .. nextCalculationAfter .. 's.')
os.sleep(nextCalculationAfter)


You can also disable this script if heating controller is off.
Done is better than perfect
Reply
#13
Cool, in which way I can disable the script?

Thanks
Reply
#14
(10.03.2018, 08:05)Domoticatorino Wrote: Cool, in which way I can disable the script?

Thanks

By use such command:
Code:
script.disable(script_name)
Done is better than perfect
Reply
#15
Obviously in a different script?
Reply
#16
(10.03.2018, 14:12)Domoticatorino Wrote: Obviously in a different script?

Yes, but you can also take all script in 1 if statement and check the status of controller. Enjoy?
Done is better than perfect
Reply
#17
Code:
valveStatesTag = 'pompa_circolazione_giorno'
pumpGA = '2/0/1'

opened, first_timestamp = false, 0

valveStates = grp.tag(valveStatesTag)
for v = 1, #valveStates, 1 do
 valveState = valveStates[v]
 if valveState.value then
   opened = true
   if first_timestamp > 0 then
       if first_timestamp > valveState.updatetime then first_timestamp = valveState.updatetime end
   else
       first_timestamp = valveState.updatetime
   end
 end
end

if opened then
   opened_5_minutes = (os.microtime() - first_timestamp) >= 120
   if opened_5_minutes then
       grp.checkwrite(pumpGA, true)
   else
       grp.checkwrite(pumpGA, false)
   end
else
   grp.checkwrite(pumpGA, false)
end

Hi, I use this script above and it seems does not work. I use 120 sec to check without loosing too time. Unfortunately Pump does not start.
What do you think?
Thanks.
Reply
#18
Please check the updated version.
Done is better than perfect
Reply
#19
It is a Schneider HL 2.0.1
Reply
#20
(10.03.2018, 17:14)Domoticatorino Wrote: It is a Schneider HL 2.0.1

I mean updated version of above scriptWink I've adjusted it to be more universal, added new variables:
Code:
valveStatesTag = 'circulation_pump'
pumpGA = '1/1/10'
openingValveTime = 300 -- time for fully open the valve (maximum time for next calculation)
minInterval = 30 -- minimum offset for next calculation

opened, firstTimestamp = false, 0

valveStates = grp.tag(valveStatesTag)
for v = 1, #valveStates, 1 do
 valveState = valveStates[v]
 if valveState.value then
   opened = true
   if firstTimestamp > 0 then
       if firstTimestamp > valveState.updatetime then firstTimestamp = valveState.updatetime end
   else
       firstTimestamp = valveState.updatetime
   end
 end
end

if opened then
   fullyOpened = (os.microtime() - firstTimestamp) >= openingValveTime
   if fullyOpened then
       grp.checkwrite(pumpGA, true)
       nextCalculationAfter = minInterval
   else
       grp.checkwrite(pumpGA, false)
       nextCalculationAfter = openingValveTime + 1 - (os.microtime() - firstTimestamp) -- run a little after possible change
   end
else
   grp.checkwrite(pumpGA, false)
   nextCalculationAfter = openingValveTime + 1
end

log('Next calculation after: ' .. nextCalculationAfter .. 's.')
os.sleep(nextCalculationAfter)
Done is better than perfect
Reply


Forum Jump: