Hey everyone, I’m working on a project where I want to synchronize my OpenHAB server’s system time with an energy meter that reports accurate timestamps. Here’s what I’ve been up to and where I could use some guidance:
The Setup
I’m running OpenHAB 2.5.0~S1549-1 on a Raspberry Pi with openHABian. The energy meter communicates via MQTT using an ESP8266 microcontroller. Every hour, the meter sends a timestamp string in the format yyyy-mm-dd hh:mm:ss
. My goal is to use this timestamp to update the system time on my OpenHAB server.
The Rule
Here’s the rule I’ve written to handle this:
rule
rule “Update clock from meter”
when
Item PW_hourStamp changed
then
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)
end
The Issue
When I test this rule, it doesn’t seem to update the system time. Here’s what the logs show:
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
Interestingly, omitting sudo
results in no error messages, but the system time still doesn’t update:
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
What I’ve Tried
- Permissions: I’ve checked that the
sudo
command is working correctly and that the OpenHAB user has the necessary permissions. - Command Variations: I’ve tried different variations of the
date
command, including specifying the timezone. - Logging: I’ve added extensive logging to track the command’s execution and output.
Questions
- Is there a better way to update the system time from a rule in OpenHAB?
- Could there be an issue with how
sudo
is handling the command in this context? - Are there any alternative commands or methods that might work more reliably?
Next Steps
I’m considering the following approaches:
- Using
hwclock
instead ofdate
to set the hardware clock. - Checking if the
chrony
service is running and properly configured. - Adjusting the
sudoers
file to grant specific permissions for thedate
command.
Any advice or suggestions would be greatly appreciated! Thanks in advance for your help.
Best regards,
Per