• Categories
  • Recent
  • Tags
  • Popular
  • Register
  • Login
  • 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.7k 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 26 Feb 2017, 20:33

    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 26 Feb 2017, 21:15 Reply Quote 0
    • T Offline
      Tag MOD @benya
      last edited by 26 Feb 2017, 21:15

      @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 26 Feb 2017, 21:52 Reply Quote 0
      • B Offline
        benya @Tag
        last edited by 26 Feb 2017, 21:52

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

        J 1 Reply Last reply 27 Feb 2017, 00:55 Reply Quote 0
        • J Offline
          jkandasa @benya
          last edited by 27 Feb 2017, 00:55

          @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 27 Feb 2017, 02:59 Reply Quote 0
          • B Offline
            benya @jkandasa
            last edited by 27 Feb 2017, 02:59

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

            J T 2 Replies Last reply 27 Feb 2017, 05:05 Reply Quote 0
            • J Offline
              jkandasa @benya
              last edited by 27 Feb 2017, 05:05

              @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 27 Feb 2017, 07:17 Reply Quote 0
              • T Offline
                Tag MOD @benya
                last edited by Tag 27 Feb 2017, 07:15

                @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 27 Feb 2017, 07:20 Reply Quote 0
                • B Offline
                  benya @jkandasa
                  last edited by 27 Feb 2017, 07:17

                  @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 27 Feb 2017, 07:20

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

                    J 1 Reply Last reply 27 Feb 2017, 08:12 Reply Quote 0
                    • J Offline
                      jkandasa @benya
                      last edited by 27 Feb 2017, 08:12

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

                      1 Reply Last reply Reply Quote 0
                      4 out of 10
                      • First post
                        4/10
                        Last post

                      0

                      Online

                      618

                      Users

                      531

                      Topics

                      3.4k

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