esp-matter icon indicating copy to clipboard operation
esp-matter copied to clipboard

Trying to use TimeSynchronizationCluster (CON-1501)

Open bnadim opened this issue 11 months ago • 8 comments

Describe the question/query that you have Hi, i am currently trying to use the TimeSynchronizationCluster to get time. I am using a device connected to a HomePod which should support time synchronization.

So far i have tried to add this code based on light switch example:

#ifdef CONFIG_ENABLE_SNTP_TIME_SYNC
    endpoint_t *root_node_ep = endpoint::get_first(node);
    ABORT_APP_ON_FAILURE(root_node_ep != nullptr, ESP_LOGE(TAG, "Failed to find root node endpoint"));

    cluster::time_synchronization::config_t time_sync_cfg;
    static chip::app::Clusters::TimeSynchronization::DefaultTimeSyncDelegate time_sync_delegate;
    time_sync_cfg.delegate = &time_sync_delegate;
    time_sync_cluster = cluster::time_synchronization::create(root_node_ep, &time_sync_cfg, CLUSTER_FLAG_SERVER | CLUSTER_FLAG_CLIENT);
    ABORT_APP_ON_FAILURE(time_sync_cluster != nullptr, ESP_LOGE(TAG, "Failed to create time_sync_cluster"));

    cluster::time_synchronization::feature::time_sync_client::add(time_sync_cluster);
    /*cluster::time_synchronization::feature::time_zone::config_t tz_cfg;
    cluster::time_synchronization::feature::time_zone::add(time_sync_cluster, &tz_cfg);*/
#endif

And i try to read the value using:

attribute_t *utc_time = attribute::get(time_sync_cluster,TimeSynchronization::Attributes::UTCTime::Id);
    if (utc_time == nullptr) {
        ESP_LOGI(TAG, "utc_time attribute is null");
    } else {
        esp_matter_attr_val_t val = esp_matter_invalid(nullptr);
        attribute::get_val(utc_time, &val);

        ESP_LOGI(TAG, "TIME IS: %lli", val.val.u64);
    }

which throw an error esp_matter_core: get_val(1144): Attribute is not managed by esp matter data model

So i am pretty sure i am not using properly. I wanted to use CLUSTER_FLAG_CLIENT flag only on the cluster, but i saw in code that it doesn't create in this case utc_time and granularity attributes which are mandatory based on matter spec 1.3.

Who can i get synchronized time ? I think it's missing an example.

Thank you for your kind help.

Additional context ...

bnadim avatar Jan 07 '25 08:01 bnadim

The UTCTime attribute is managed by AttributeAccessInterface instead of esp-matter data model so you cannot get its value with the APIs provided in esp-matter component, Please refer to this, the unix UTC time can be get System::SystemClock().GetClock_RealTime().

wqx6 avatar Jan 08 '25 03:01 wqx6

Hey wqx6,

Thank you for you feedback. I tried, but my time doesn't get synced. I can't find what i am missing. Do you have a "full example" please ? I believe it would make a very good example. Thank you very much for your help.

Regards

bnadim avatar Jan 10 '25 11:01 bnadim

Using this cluster is very painful. Just turn on the standard NTP client via menuconfig which is 1000 times easier.

jonsmirl avatar Jan 14 '25 00:01 jonsmirl

Thank you for you feedback. I tried, but my time doesn't get synced. I can't find what i am missing. Do you have a "full example" please ? I believe it would make a very good example. Thank you very much for your help.

What commissioner are you using? I tested the time-sync cluster with the following steps:

  • Commission the device with chip-tool and then open a commissioning window.
  • Use Apple home app to commission the device, the Apple home app will set the UTC time and time zone during commissioning
  • Read the UTC time with chip-tool.

wqx6 avatar Jan 16 '25 03:01 wqx6

Hey,

thank you for your feedback. @wqx6 indeed i finally managed to get time at commissioning. My issue is after restart i lose time and i didn't find to request it unless i recommission the device. Apple set the utc time at commissioning but not trusted source. Do you have any idea on how to "continuously" sync with an apple device ?

@jonsmirl i tried standard ntp, but it doesn't get sync if connected to thread/matter network of apple. I juste get timeouts. Have you done it with apple ?

Thank you

bnadim avatar Jan 17 '25 21:01 bnadim

Standard NTP will ignore Matter and use the existing NTP protocol. Use pool.ntp.org which will attach to the nearest public server. Of course this only works from a network with Internet connectivity. If you have an Apple Home device you have to have Internet connectivity since it also needs it.

This might be a thread problem, does thread route onto the Internet by default? For sure it is possible for Thread to route onto the Internet but I don't know if that is capability is turned on. All of the Matter devices I work with are wifi based.

jonsmirl avatar Jan 18 '25 00:01 jonsmirl

Since thread is IPv6 only I believe you need to use 2.pool.ntp.org or time.google.com. pool.ntp.org is timing out because it does not support IPv6 clients.

jonsmirl@ares:~$ nslookup pool.ntp.org
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	pool.ntp.org
Address: 38.81.211.177
Name:	pool.ntp.org
Address: 172.235.32.243
Name:	pool.ntp.org
Address: 198.60.22.240
Name:	pool.ntp.org
Address: 107.172.16.131

jonsmirl@ares:~$ nslookup 2.pool.ntp.org
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	2.pool.ntp.org
Address: 173.71.68.71
Name:	2.pool.ntp.org
Address: 66.244.16.123
Name:	2.pool.ntp.org
Address: 74.208.72.129
Name:	2.pool.ntp.org
Address: 69.164.213.136
Name:	2.pool.ntp.org
Address: 2606:82c0:21::e
Name:	2.pool.ntp.org
Address: 2600:1700:3d24:740f:9524:529a:6489:d48f
Name:	2.pool.ntp.org
Address: 2001:470:e8dc:10::123
Name:	2.pool.ntp.org
Address: 2600:4040:e0da:f000::cbb9:201a

jonsmirl@ares:~$ nslookup time.google.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	time.google.com
Address: 216.239.35.12
Name:	time.google.com
Address: 216.239.35.8
Name:	time.google.com
Address: 216.239.35.0
Name:	time.google.com
Address: 216.239.35.4
Name:	time.google.com
Address: 2001:4860:4806:c::
Name:	time.google.com
Address: 2001:4860:4806:4::
Name:	time.google.com
Address: 2001:4860:4806::
Name:	time.google.com
Address: 2001:4860:4806:8::

jonsmirl avatar Jan 18 '25 03:01 jonsmirl

Hello,

If you need help to use the time synchronization cluster, take a look here: https://github.com/espressif/esp-matter/issues/1269

I made it working.

There is just a little bug but it is not very important:

  • Most of the logs display the correct date/time
  • One line of logs display the wrong date/time
  • In the code when I use chip::System::SystemClock().GetClock_RealTime() i get the correct time (this is the most important).

caipiblack avatar Feb 06 '25 14:02 caipiblack