How to pass custom value relative to current time



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



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



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


  • ADMIN

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



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


  • ADMIN

    @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);  
    }
    


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



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



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


  • ADMIN

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


Log in to reply