Controlling global variable and timer from Groovy



  • I am planning to connect a multi-position switch with a few PIR sensors.

    When PIR1 gets tripped and switch is OFF, switch should be set to position1.
    Switch should be turned OFF after 3 minutes.

    When PIR1 gets tripped and switch is not OFF, switch should be kept in this state extra 3 minutes and then turned OFF.
    When PIR2 gets tripped, switch should be set to position2 for 3 minutes.

    To implement this scenario, I plan to define global variable to hold switch state and timer to turn switch off when timeout expires.

    Now questions:

    Should I use "Utilities->Variables repository" to define global variable?
    Could someone post a code snippet to show how to read/write global variable in the Groovy script?

    I guess timer should be "normal" (not recurring)?
    Timer should be connected to the "Turn OFF" operation?
    How could I control timer in the Groovy script (e.g. change time when it should trigger)?

    I need Groovy sample that runs some command line tool or shell script.


  • ADMIN

    @benya

    Should I use "Utilities->Variables repository" to define global variable?
    Could someone post a code snippet to show how to read/write global variable in the Groovy script?

    You can get follow http://forum.mycontroller.org/category/24/scripts have a lot of examples.

    def myVar = mcApi.variable().get("my-variable")
    //Read values
    myVar.getId()  //Returns internal id(for end user not useful)
    myVar.getKey() //Key(here: my-variable)
    myVar.getValue() //value as String
    myVar.getValue2() //value2 as String
    myVar.getValue3() //value3 as String
    
    //Change value
    myVar.setValue("my-new-value") //Should be in the form of String
    
    //to save
    myVar.save()
    //or
    mcApi.variable().update(myVar)
    

    I guess timer should be "normal" (not recurring)?
    Timer should be connected to the "Turn OFF" operation?
    How could I control timer in the Groovy script (e.g. change time when it should trigger)?

    You can control your timer. Even you can control your operations. I will add example soon.

    I need Groovy sample that runs some command line tool or shell script.

    MyController scripts designed to run on MyController itself. For now, we cannot run outside (command line).



  • @jkandasa said:

    @benya

    I need Groovy sample that runs some command line tool or shell script.

    MyController scripts designed to run on MyController itself. For now, we cannot run outside (command line).

    Do you mean I cannot do "ps".execute() from Groovy or system("ps") from Perl?



  • @benya I verified, Groovy could execute external tools. Tested with Demo system:

    return "ls".execute().text.split("\n")
    

    Result:

    {
      "mcResult": [
        "Publicbroker-wstestmosquittoorg8080mqtt",
        "h2",
        "start.bat",
        "start.sh",
        "start_.sh",
        "stop.sh",
        "test_19Kz0-tcpwantsoftru1883",
        "test_48n0f-tcpwantsoftru1883",
        "test_FzOqu-tcpwantsoftru1883",
        "test_epPgp-tcpwantsoftru1883",
        "tmp"
      ]
    }

  • ADMIN

    @benya I thought you asked to execute script itself from command line.



  • @jkandasa said:

    You can get follow http://forum.mycontroller.org/category/24/scripts have a lot of examples.

    I saw only 9 topics in this list.

    Did you have a chance to prepare sample to show how to control timer?


  • ADMIN

    @benya

    def filters = [:];
    //Add filter to get your timer
    filters.put("name", "Disable-PIR-rules");
    //your timer
    def myTimer = mcApi.timer().get(filters);
    
    //Do actions
    myTimer.setEnabled(false);
    
    //Update this timer
    mcApi.timer().update(myTimer);
    


  • @jkandasa Seems mcApi.timer().update(myTimer); causes timer to loose references to the operations and doesn't trigger operation when enabled again.

    I had better luck with the following code:

    def filters = [:];
    filters.put("name", "Timer1");
    def myTimer = mcApi.timer().get(filters);
    
    def timerIds = [myTimer.getId()];
    mcApi.timer().disable(timerIds)
    
    mcApi.timer().enable(timerIds)

  • ADMIN

    @benya Thanks for the update, let me check mcApi.timer().update(myTimer)


Log in to reply
 

6
Online

442
Users

231
Topics

1389
Posts

Looks like your connection to MYCONTROLLER.ORG was lost, please wait while we try to reconnect.