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

    How to pass custom value relative to current time

    scheduled pinned locked moved General Discussion
    scriptjavascript
    10 posts 3 posters 4.1k 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'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 one-reply-to-this-post last-reply-time reply quote 0
      • T offline
        Tag MOD @benya
        global: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 one-reply-to-this-post last-reply-time reply quote 0
        • B offline
          benya @Tag
          global:last-edited-by,

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

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

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

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

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

                        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