• Categories
  • Recent
  • Tags
  • Popular
  • Register
  • Login
  • 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.5k 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 3 Aug 2017, 12:04 5 Mar 2017, 07:34

    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.

    J 1 Reply Last reply 6 Mar 2017, 04:32 Reply Quote 0
    • J Offline
      jkandasa @benya
      last edited by 6 Mar 2017, 04:32

      @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 6 Mar 2017, 06:47 Reply Quote 0
      • B Offline
        benya @jkandasa
        last edited by 6 Mar 2017, 06:47

        @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 6 Mar 2017, 07:49 Reply Quote 0
        • B Offline
          benya @benya
          last edited by 6 Mar 2017, 07:49

          @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"
            ]
          }
          J 1 Reply Last reply 6 Mar 2017, 12:06 Reply Quote 1
          • J Offline
            jkandasa @benya
            last edited by 6 Mar 2017, 12:06

            @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 7 Mar 2017, 20:32

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

              J 1 Reply Last reply 8 Mar 2017, 06:33 Reply Quote 0
              • J Offline
                jkandasa @benya
                last edited by 8 Mar 2017, 06:33

                @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 9 Mar 2017, 06:36 Reply Quote 0
                • B Offline
                  benya @jkandasa
                  last edited by benya 3 Sept 2017, 12:06 9 Mar 2017, 06:36

                  @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)
                  J 1 Reply Last reply 9 Mar 2017, 12:11 Reply Quote 1
                  • J Offline
                    jkandasa @benya
                    last edited by 9 Mar 2017, 12:11

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

                    1 Reply Last reply Reply Quote 0
                    2 out of 9
                    • First post
                      2/9
                      Last post

                    0

                    Online

                    619

                    Users

                    531

                    Topics

                    3.4k

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