executeCommandLine to change system time

OpenHab run on a server that may be used without access to internet. It reads an energy meter with an ESP8266 microcontroller that reports meter values via mqtt over wifi. Each hour the meter reports accurate time in a string (“yyyy-mm-dd hh:mm:ss”). The following rule in meant to set the system time based on this string.

  • Platform information:
    • RPi with openhabian and openHAB 2.5.0~S1549-1 (Build #1549)
    • Rules code related to the issue
rule "Update clock from meter"
	Item PW_hourStamp changed
	logInfo("Clock updated to:", "" + PW_hourStamp.state)
	var String hourStamp = "'" + PW_hourStamp.state +"'"
	val String results = executeCommandLine("sudo date --set " + hourStamp)
	logInfo("Error from update is", "" + results)

Output from testing the rule:

2020-01-01 13:48:38.308 [INFO ] [thome.model.script.Clock updated to:] - 2020-01-01 13:00:10 
2020-01-01 13:48:38.377 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'sudo date --set '2020-01-01 13:00:10''
2020-01-01 13:48:39.683 [INFO ] [me.model.script.Error from update is] - null

Using sudo does not seem to work. Omitting the sudo makes the following output:

2020-01-01 13:52:02.531 [INFO ] [thome.model.script.Clock updated to:] - 2020-01-01 13:00:10
2020-01-01 13:52:02.542 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'date --set '2020-01-01 13:00:10''
2020-01-01 13:52:02.544 [INFO ] [me.model.script.Error from update is] - null

Is there a better way to update the system time from a rule?

I would appreciate any advice that points me to in the right direction :smiley:

Best regards Per

You could try this:


Also the quotes with doubled qoutes could be an issue.

1 Like

Why not use an ntp server and the functionality built in to the OS?
That is the proper solution for accurate time. If you are trying to sync with a device that has an incorrect time, fix that device.

1 Like

Why use a rule? openhabian includes NTP that should sync time automatically.

1 Like

@Josar. Thanks, I will try that

@mstormi, @Bruce_Osborne : I should have explained the situation better. If connected to internet, ntp is better. The need is when the server is not connected to internet. In that case, the time data from the meter should be used. The problem description is updated.

Is it an accurate time source? Is it possibly for that device to act as an ntp server?

@Bruce_Osborne: The meter is an accurate time source, but not possible to use as an ntp server. It delivers a string every hour with correct local time.

1 Like