Proposed improvement in case of Cold Boot after a central power outage

Subforum for general discussions. Do not dump your questions/problems here, but try to find the subforum where it belongs!

Moderators: leecollings, remb0

Post Reply
meal
Posts: 103
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: France
Contact:

Proposed improvement in case of Cold Boot after a central power outage

Post by meal »

Hello,

In case of a central power outage both internet router and domoticz are down.

When the power is back both internet router and domoticz are starting up and domoticz may start before internet is up and running.

In that case domoticz may start without successful time synchronization.

In order to give a chance to domoticz to start with a successful time synchronization i have updated domoticz.sh file in my system in such a way as to try a time synchronization every 30 second for 5 minutes. If the time synchronization is successful domoticz starts with a synchronized time. After the 5 minutes delay domoticz starts without successful time synchronization.

Current source code line 71 onwards:

Code: Select all

		count=30
		while [ ! -f "/run/systemd/timesync/synchronized" ]
		do
		    count=$((count-1))
		    if [ $((count)) -lt 1 ]
		    then
			# If failed, print error message, exit loop and start Domoticz anyway
			printf "\nWARNING: Time synchronization failed, check network and /etc/systemd/timesyncd.conf\n"
			printf "Starting Domoticz without successful time synchronization...\n"
			break
		    fi
		    printf "."
		    sleep 1
		done
Proposed source code line 71 onwards:

Code: Select all

		count=10
		while [ ! -f "/run/systemd/timesync/synchronized" ]
		do
		    count=$((count-1))
		    if [ $((count)) -lt 1 ]
		    then
				# If failed, print error message, exit loop and start Domoticz anyway
				printf "\nWARNING: Time synchronization failed, check network and /etc/systemd/timesyncd.conf\n"
				printf "Starting Domoticz without successful time synchronization...\n"
				break
		    fi
			printf "\nRestart systemd-timesyncd for time synchronization...\n"
			sudo systemctl restart systemd-timesyncd
		    sleep 30
		done
BR
meal
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by waltervl »

So what is the difference in behavior between original code and your code?

I edited your post to get it more readable.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
lost
Posts: 616
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by lost »

There's an easier way, using ntp-wait: By default, this'll do 100 retries every 6s waiting for NTP sync.
So just adding this 2 lines at the beginning of domoticz.sh do_start() does the job, waiting up to 10mn:

Code: Select all

ntp-wait
[ $? -ne 0 ]  && echo WARNING : NTP KO, Domoticz may not start!!! || echo INFO : NTP OK, starting Domoticz...
Using -n and -s options ntp-wait retries/delay between them may be tune to match one's needs.
meal
Posts: 103
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: France
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by meal »

@Waltervl

The difference with the original code is that every 30s I will try to synchronise the time with
sudo systemctl restart systemd-timesyncd

The original code tries the time sync only once.

BR
meal
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by waltervl »

You could discuss if it is the responsibility of Domoticz (through the Domoticz startup script) that time should be synced. It should be another startup process that should do that.
The original code does not try to sync, only waits 30 seconds for time to be synced. As that should normally be enough but seems for some too short. Perhaps it should be increased to 300 seconds matching the 5 minutes you do?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
lost
Posts: 616
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by lost »

waltervl wrote: Monday 12 February 2024 13:16 The original code does not try to sync, only waits 30 seconds for time to be synced. As that should normally be enough but seems for some too short.
A fiber or even ADSL sync is usually several minutes.

Do not run into this issue anymore as my fiber router now have a backup battery (20V+5V so able to feed both router and PI 3B hosting Domoticz that are co-located, so I keep internet and phone using sip up during power outages, as well as my home management for at least 1h), but also had to extend this (ntp-wait default is 10mn, never reached) in the past before finally letting domoticz start before time-sync (was causing a watchdog at the time).
meal
Posts: 103
Joined: Monday 04 March 2019 14:59
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.7
Location: France
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by meal »

Hello,

I have a similar configuration as @lost with a UPS and backup battery.

But when a power outage lasts half a day (if it occurs) the UPS is no more operating and domoticz shutdowns.

When power if back domoticz if not sync will start with a time lag of several hours which potentially could mess up the device history in the database. I solved this issue by forcing the time sync every 30seconds for 5 minutes which for me leaves me enough time to have internet up and running before domoticz is started.

BR
meal
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by waltervl »

The time sync should happen anyway I suppose by the startup of your system OS (I did not investigate this) if you trigger it every 30 seconds or not.
So you just have to wait patiently until the time has synced. In the current script we wait (unpatiently) max 30 seconds until there is a file /run/systemd/timesync/synchronized to indicate time has synced.

So if you wait for say max 10 minutes instead of max 30 seconds Domoticz will startup after being synced.
So changing in current domoticz.sh count=30 to count=600 should solve this.

Edit: https://www.freedesktop.org/software/sy ... .conf.html
PollIntervalMinSec= defaults to 32 seconds
ConnectionRetrySec= Defaults to 30 seconds

So it seems the RPi will try itself on a cold boot every 30 seconds to sync the time by the systemd-timesyncd system service. So no need to push a time sync from the Domoticz startup script. Just patiently wait until it is synced (wait for file /run/systemd/timesync/synchronized).
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by Kedi »

If you put at the beginning of do_start() this code the domoticz start script just wait until the time is synchronized.

Code: Select all

        sudo ntpdate nl.pool.ntp.org
or use the lastest /etc/init.d/domoticz.sh script.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by waltervl »

Kedi wrote: Tuesday 13 February 2024 11:43 If you put at the beginning of do_start() this code the domoticz start script just wait until the time is synchronized.

Code: Select all

        sudo ntpdate nl.pool.ntp.org
or use the lastest /etc/init.d/domoticz.sh script.
read the topic again, this is not needed.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by Kedi »

When you installed Domoticz a few years ago and do a normal update the script /etc/init.d/domoticz.sh is not updated.
So there might be a startup script present which does not include the 'time-sync'
My original startup script from 9 years ago with my enhancements is still there.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by waltervl »

Or you copy the domoticz/domoticz.sh contents to /etc/init.d/domoticz.sh (after checking the normal config changes you did in /etc/init.d/domoticz.sh)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
solarboy
Posts: 300
Joined: Thursday 01 November 2018 19:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.6
Location: Portugal
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by solarboy »

I bought a cheap Adafruit real-time-clock and never looked back.
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),SMA Hub (docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Proposed improvement in case of Cold Boot after a central power outage

Post by waltervl »

solarboy wrote: Tuesday 13 February 2024 14:18 I bought a cheap Adafruit real-time-clock and never looked back.
Indeed the best solution!
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest