This page is also available in the other languages: English Deutsch

Time

Send date and time to the KNX bus.

The current time (date & time) is generally not available on a KNX bus. It is not necessary for this information to be available because most KNX devices work without knowing the current time.

However, it can be useful in some cases - for example when showing the current time on a display.

With a script you can send the current time to a group address. Either the system time or NTP can be used as a time source. Depending on the requirements, the time can be encoded as data type 10.001 (time), 11.001 (date) or 19.001 (date and time).

System Time

The system time is the time of the running system. If hkknx is running on a Rasbberry Pi, then the system time is the time on the Raspberry Pi.

Note
The system time is reset when the Raspberry Pi is restarted. The Raspberry Pi OS then updates the system time using timesyncd via NTP. If the Raspberry Pi is not connected to the Internet, then the system time is incorrect.

In a script, the system time is queried with the function Now in the “time” package. In the following script, the system time is sent every minute as data type 19.001 to the group address 1/0/3.

 1var hkknx = import("hkknx")
 2var time = import("time")
 3
 4for {
 5     // Query system time
 6     var now = time.Now()
 7     // Convert time to data type 19.001
 8     var dateTime = hkknx.DPT19001(now)
 9     // Send value to group address 1/0/3
10     hkknx.GroupWrite(dateTime, "1/0/3")
11     // Wait 1 minute
12     <-time.After(1 * time.Minute)
13}

NTP

The Network Time Protocol is a networking protocol for clock synchronization between computer systems over the Internet. It can be used in scripts to query the current time and send it to a group address.

Since hkknx v2.8.0 there is a new “ntp” package, which contains the function Time to query the time from an NTP server.

In the following script, the current time is queried every minute from the NTP server pool.ntp.org and sent as data type 19.001 to the group address 1/0/3.

 1var ntp = import("ntp")
 2var log = import("log")
 3var hkknx = import("hkknx")
 4var time = import("time")
 5
 6for {
 7     var t, err = ntp.Time("pool.ntp.org")
 8     if err != nil { // Check for errors
 9         // Write the error to the log
10         log.Println(err)
11     } else {
12         // Convert time to data type 19.001
13         var dateTime = hkknx.DPT19001(t)
14         // Send value to group address 1/0/3
15         hkknx.GroupWrite(dateTime, "1/0/3")
16     }
17    
18     // wait 1 minute
19     <-time.After(1 * time.Minute)
20}

A list of NTP servers is available at www.ntppool.org.


On This Page
© Matthias Hochgatterer – MastodonGithubRésumé