• header.categories
    • header.recent
    • header.tags
    • header.popular
    • register
    • login

    Controlling global variable and timer from Groovy

    scheduled pinned locked moved General Discussion
    scriptgroovytimer
    9 posts 2 posters 3.8k views 1 watching
    loading-more-posts
    • oldest-to-newest
    • newest-to-oldest
    • most-votes
    reply
    • reply-as-topic
    guest-login-reply
    deleted-message
    • B offline
      benya
      global: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 one-reply-to-this-post last-reply-time reply quote 0
      • jkandasaJ offline
        jkandasa @benya
        global: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 topic:replies-to-this-post, 2 last-reply-time reply quote 0
        • B offline
          benya @jkandasa
          global: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 one-reply-to-this-post last-reply-time reply quote 0
          • B offline
            benya @benya
            global: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 one-reply-to-this-post last-reply-time reply quote 1
            • jkandasaJ offline
              jkandasa @benya
              global:last-edited-by,

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

              one-reply-to-this-post last-reply-time reply quote 0
              • B offline
                benya @jkandasa
                global: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 one-reply-to-this-post last-reply-time reply quote 0
                • jkandasaJ offline
                  jkandasa @benya
                  global: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 one-reply-to-this-post last-reply-time reply quote 0
                  • B offline
                    benya @jkandasa
                    global: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 one-reply-to-this-post last-reply-time reply quote 1
                    • jkandasaJ offline
                      jkandasa @benya
                      global:last-edited-by,

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

                      one-reply-to-this-post last-reply-time reply quote 0
                      • first-post
                        last-post

                      0

                      online

                      644

                      users

                      532

                      topics

                      3.4k

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