• Categories
    • Recent
    • Tags
    • Popular
    • Register
    • Login

    Controlling global variable and timer from Groovy

    Scheduled Pinned Locked Moved General Discussion
    scriptgroovytimer
    9 Posts 2 Posters 3.4k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      benya
      last edited by jkandasa

      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.

      jkandasaJ 1 Reply Last reply Reply Quote 0
      • jkandasaJ Offline
        jkandasa @benya
        last edited by

        @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).

        B 2 Replies Last reply Reply Quote 0
        • B Offline
          benya @jkandasa
          last edited by

          @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?

          B 1 Reply Last reply Reply Quote 0
          • B Offline
            benya @benya
            last edited by

            @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"
              ]
            }
            jkandasaJ 1 Reply Last reply Reply Quote 1
            • jkandasaJ Offline
              jkandasa @benya
              last edited by

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

              1 Reply Last reply Reply Quote 0
              • B Offline
                benya @jkandasa
                last edited by

                @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?

                jkandasaJ 1 Reply Last reply Reply Quote 0
                • jkandasaJ Offline
                  jkandasa @benya
                  last edited by

                  @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);
                  
                  B 1 Reply Last reply Reply Quote 0
                  • B Offline
                    benya @jkandasa
                    last edited by benya

                    @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)
                    jkandasaJ 1 Reply Last reply Reply Quote 1
                    • jkandasaJ Offline
                      jkandasa @benya
                      last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post

                      0

                      Online

                      587

                      Users

                      529

                      Topics

                      3.4k

                      Posts
                      Copyright © 2015-2025 MyController.org | Contributors | Localization