Example: Dynamically change the Scheduled program time parameters from LM2 visualization

Task

How to allow end-user to change time from visualization system (so he doesn’t go into scripting) when the shutters are opened in the morning and when closed in the evening.

Create objects

User function library program

Add following code to Scripting -> Tools -> User Function Library:

Source code    
  1. function matchdate(now, obj)
  2. -- get object value
  3. local odate = grp.getvalue(obj)
  4. -- check hour and minute parts, seconds are ignored
  5. return type(odate) == 'table' and now.hour == odate.hour and now.min == odate.minute
  6. end
  7.  
  8. function scheduler(startobj, endobj, ctrlobj)
  9. -- current date as table
  10. local now = os.date('*t')
  11.  
  12. -- check schedule start
  13. if matchdate(now, startobj) then
  14. grp.write(ctrlobj, true, dt.bool)
  15. end
  16.  
  17. -- check schedule end
  18. if matchdate(now, endobj) then
  19. grp.write(ctrlobj, false, dt.bool)
  20. end
  21. end

Scheduled program

Create scheduled script that runs every minute.

Source code    
  1. scheduler("auto_scheduler_start", "auto_scheduler_stop", "shutters1")

Launching objects from visualization

Day is not checked in this example, only Time value.

More complex logic on event

  • We can create an Event-based script for control of the object which can execute more complex logic for example:
    – Morning: open shutter1 on 50%, shutter2 – 100%
    – Evening: leave shutter1 opened on 10%, shutter2 – fully closed
Source code    
  1. value = cnv.decode(event.datahex, dt.bool)
  2. -- scheduler start
  3. if value then
  4. grp.write('dimmer 1', 50)
  5. grp.write('dimmer 2', 100)
  6. -- scheduler end
  7. else
  8. grp.write('dimmer 1', 10)
  9. grp.write('dimmer 2', 0)
  10. end