How-To sync time on your drifting Zwave devices (nightly)

I’ve tried to find solution for years because my MCOHOME Zwave thermostats drift heavily. Can’t be bothered to click ‘set time and date’ in zwave-js UI every bloody day.

Had an idea long time ago, now implemented it. I suspect there are others who might find this useful.

In Zwave JS UI (Settings → General) add a Scheduled Job (Enabled, but not On Init). Cron string can be like 0 3 * * * for once every night 3 AM. And code:

const { logger } = this;

logger.info('Time sync starting');

const errors = [];
const promises = [];

driver.controller.nodes.forEach(async (node) => {
  if (node.status === 0 || node.status === 3) { // Unknown or Dead
    errors.push(node.id);
    return;
  }
  
  promises.push(
    node.setDateAndTime().then((success) => {
      if (success) {
        logger.info('Time sync successful for node: ' + node.id);
      } else {
        errors.push(node.id);
      }
    }).catch(() => {
      errors.push(node.id);
    })
  );
});

Promise.all(promises).then(() => {
  if (errors.length > 0) logger.info('Time sync failed for nodes: ' + errors.join(', '));
});

3 Likes

Nice work!

Oh nice! I haven’t tried the scheduler…learn something new everyday :slight_smile:

For what it’s worth, I used the following a year or so ago just to try and set the clock on a device using an Action from HA (haven’t played much with it since):

action: zwave_js.invoke_cc_api
data:
  method_name: set  
  command_class: "129"  
  endpoint: "0"  
  parameters:
    - 17    
    - 12
    - 6  
  device_id:    
    - f8d15_your_ha_device_id_c74873

where the 129 command class is the CLOCK_COMMAND
17 is the hour,
12 is the minute and
6 is day (Saturday).
It seemed to work.
Here is the corresponding GET:

action: zwave_js.invoke_cc_api
data:
  method_name: get  
  command_class: "129"  
  endpoint: "0"  
  parameters: [ ]  
  device_id:    
    - f8d15_your_ha_device_id_c74873

You’ll see the results of these commands in the ZWaveJS debug level logs.

1 Like

Fantastic, thank you so much for this. I can’t use command class 129 with my zwave heaters, they don’t support it, so this is the closest I can get. The clocks drift horribly.

I’m trying to use your example but running into difficulty. Z-Wave JS v15.15.3 and Z-Wave JS UI 11.5.2 running on HaOS. I’ve pasted your code into the Scheduled Job section and set the crontab to run and hit save. The job runs at the correct time but I’m getting the following error:

ERROR GATEWAY: Error executing scheduled job Time Sync: require is not defined

I’m not sure how to proceed. Any guidance is greatly appreciated. Thanks!

It’s been a couple of weeks, anyone have an idea on why it’s failing for me? googling just leads me in circles about missing dependencies and such, all of which didn’t appear to be applicable to the HA environment. Any and all assistance is appreciated.

@BenCranston I don’t think it is the fault of this code, it appears to be a problem in the addon itself. Not even the ping snippet given as an example is working. I’m having the same issue after updating recently.

Alright, thanks for the confirmation that I’m in good company. :slight_smile: I don’t know my way out of a wet paper bag as it relates to javascript.