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

    How to pass custom value relative to current time

    Scheduled Pinned Locked Moved General Discussion
    scriptjavascript
    10 Posts 3 Posters 3.6k 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'd like node to make certain action at some point in the future.
      Node requests current time from gateway through requestTime() and receiveTime() and gets future time through S_CUSTOM/V_VAR1.

      How could MyController calculate and pass time value that has specific offset from current time?

      T 1 Reply Last reply Reply Quote 0
      • T Offline
        Tag MOD @benya
        last edited by

        @benya

        Hi!,
        Reading your question, i am not sure what you try to accomplish......

        • You request time from the gateway

        • You set a time value in V_VAR1

        It is not clear to me where you want to calculate the time difference.....

        Anyway, on the arduino you could use the time library, located here
        This uses epoch (seconds since 1970) it gives you a bignumber which is easy use when you try to calculate a time difference.

        Hope this helps.

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

          @Tag I want to calculate timestamp (=current_time+offset) on MyController side and pass it to the node

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

            @benya The time you are receiving from MyController already updated with timezone an offset value. https://github.com/mycontroller-org/mycontroller/blob/06c0607703d8337d9c5f85df9b63419026f1be2e/modules/core/src/main/java/org/mycontroller/standalone/message/McMessageEngine.java#L272~L276

            Let me know if your requirement still not satisfied with this.

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

              @jkandasa I don't mean timezone offset, but rather arbitrary constat.
              For example, node is supposed to turn on light for 5 minutes, therefore controller sends current time plus 300 to indicate when to turn off light.

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

                @benya Right now there is no support running script for request from a node. however, you can update a sensor variable periodically(Note: when you update sensor variable value, same data will be sent to that node too).

                I do not know what is your actual requirement. however, you can achieve most of the things via the controller. and can controller your things from the controller.

                To run this script you have to map your S_CUSTOM/V_VAR1 with my-custom-var on UID Tags.

                JavaScript:
                var myImports = new JavaImporter(java.io, java.lang, java.util, java.text);
                
                with(myImports) {
                  var customVar = mcApi.uidTag().getByUid("my-custom-var").getResource();
                  var myOffset = 300; //in seconds
                  var currentTimestamp = Math.round(new Date().getTime() / 1000); //get current timestamp and remove milliseconds
                  customVar.value = currentTimestamp + myOffset;
                  
                  //Send date to target
                  mcApi.sensor().sendPayload(customVar);  
                }
                
                B 1 Reply Last reply Reply Quote 0
                • T Offline
                  Tag MOD @benya
                  last edited by Tag

                  @benya

                  if it is always the same value, you can also add this easily in your sketch by using a counter in a loop based on the millis() function. however the implementation with mycontroller allows you to more easily change the value.

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

                    @jkandasa Looks like a good solution for me. Where could I read more about my controller scripting?

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

                      @Tag My node doesn't have RTC and millis() gets affected by sleep. Getting current time from gateway seems more robust

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

                        @benya have a look on http://forum.mycontroller.org/category/24/scripts and TAG with SCRIPT

                        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