Publish System Time

Hi
I’m trying to get the current datetime from the system and publish it via mqtt.
My goal is to have commands with a timeout such open a gate.

In order to do this i need my arduino’s to know my RPi time and attach a item with a time.
so I have my item like this:

Switch        varArduino1DPPin22        "Pulsa Digital 22"          <switch>             {mqtt=">[mosquitto:a1/i/dp22:command:ON:JS(currenttime.js)],>[mosquitto:a1/i/dp22:command:OFF:0.500],<[mosquitto:a1/o/d22:state:ON:1],<[mosquitto:a1/o/d22:state:OFF:0"}

my transform currenttime.js

(function(i){
  //var millis = Date.now();
  //return "1.500." + millis; // number of milliseconds since 00:00:00 UTC on January 1, 1970
})(input)

Here I have my first problem: The javascript date is not respecting my daylights saving time, so the date don’t mach with the oh date (ntp) and my RPi date

Then I need to make oh to publish the date every minute or so (or even better make oh reply to a mqtt command), so my arduino’s can synchronize the proper time with oh. How can I accomplish this?

Thanks for any help

I’d recommend using the NTP binding to get the current time in a DateTime Item. Then create an Item with an incoming MQTT subscription to a topic. Then set up a Rule to trigger when a message is received by that Item and publish the current date time in the DateTime Item using the MQTT publish Action.

One thing you have to realize though is that epoch (i.e. the date time you get when you convert a date time to milliseconds is ALWAYS going to be the number of milliseconds since 00:00:00 UTC. Epoch has no concept of time zones. Epoch for a given second is the same no matter where you are in the world. If you are needing to send epoch to your Arduinos it is up to your Arduinos to understand what that value means for your time zone. This is standard coding practice and is how all computers handle time.

If you can’t or won’t implement time zone handling in the Arduinos then you can add/subtract the offset in your OH rule, but you will have to implement the code to understand daylight savings or any other things like that.

To get epoch from a DateTime Item use something like:

(vSunrise_Time.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli