Get time from xml file Topic is solved

Moderator: leecollings

Post Reply
sailmich
Posts: 245
Joined: Wednesday 17 February 2016 22:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany
Contact:

Get time from xml file

Post by sailmich »

I have two Rpi 3 running. First is running newest Domoticz beta on the other is weewx running connected to my personal weather station. I have some lua scripts running to get temperature, wind, UV .... Everything is running good but from time to time weewx is hanging and than the xml file from were I take all the data is old. I tried already everything on weewx to avoid this but still happening from time to time. What I would like to do is compare the time in my xml file with system time on my domoticz server and when bigger than 10 minutes than to a ssh sudo reboot the weewx server. I don't know how to get the time out of the xml file and how to compare it with os time.
Script how I get gust.
Spoiler: show
-- Title: script_device_weewx.lua
-- Date: 10-01-2016
-- this script reads the weewxpws xml file and uploads current data to virtual device

commandArray = {}
time = os.date("*t")
if ((time.min % 1)==0) then

os.execute('sudo wget http://MyweewxIP/weewx_pws.xml -O /path/to/weewx_pws.xml')

local filename = "/path/to/weewx_pws.xml"

local line = ''
local windGust = 0
local speeddevice = ''

for line in io.lines(filename) do
if string.find(line,"\"windGust\"") then
windGust=tonumber(string.match(line, "%d+.%d+"))
windGustKMH=windGust*3.6
end
end

-- Update virtual device id
speeddevice='148|0|'..windGustKMH..';0'

--print(speeddevice))

commandArray['UpdateDevice'] = speeddevice
end
return commandArray
My try but get stuck:(
Spoiler: show
-- Title: script_device_weewx.lua
-- Date: 10-01-2016
-- this script reads the weewxpws xml file and uploads current data to virtual device

commandArray = {}
time = os.date("*t")
if ((time.min % 1)==0) then

os.execute('sudo wget http://MYweewxIP/weewx_pws.xml -O /path/to/weewx_pws.xml')

local filename = "/path/to/weewx_pws.xml"

local line = ''
local station_time = 0

for line in io.lines(filename) do
if string.find(line,"\"station_time\"") then
station_time=tonumber(string.match(line, "%d+.%d+")) attempt to call a nil value
How to compare station_time with ostime?
"ssh MYweewxIP sudo reboot"
end
end
end

return commandArray
The xml where I would like to get the station time from.
Spoiler: show
<realtime><data realtime="station_time">12:26 CEST</data></realtime>
Thanks for any help.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Get time from xml file

Post by ben53252642 »

Play with this on the weewx machine:

watchdog.sh

Code: Select all

#!/bin/bash
while true; do
if (find /PATH/TO/XMLFILE -mmin +3 -type f | grep -q /); then
echo "$(date +%T) XML file not updating, potential fault detected, restarting the service..."
service NAME stop
service NAME start
fi
echo "$(date +%T) Sleeping for 30 seconds before next check"
sleep 30
done
If the xml file is not modified for more than 3 minutes then the restart command is executed, it checks every 30 seconds.

I suggest running it in a screen session in loop on boot. eg:

sudo -u root screen -dmS mywatchdogscript /scripts/watchdog.sh

Check how often the xml file is changed normally then set the minutes currently +3 accordingly to detect when its failing and restart the service.

Don't forget to make the watchdog file executable: sudo chmod 755 watchdog.sh

If restarting the service doesn't fix it, you could always just reboot the whole Pi when it detects data no longer going into the xml file, eg:

Code: Select all

#!/bin/bash
while true; do
if (find /PATH/TO/XMLFILE -mmin +3 -type f | grep -q /); then
echo "$(date +%T) XML file not updating, potential fault detected, rebooting in 30 seconds..."
sleep 30
sudo reboot
fi
echo "$(date +%T) Sleeping for 30 seconds before next check"
sleep 30
done
Last edited by ben53252642 on Wednesday 28 August 2019 15:14, edited 2 times in total.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
sailmich
Posts: 245
Joined: Wednesday 17 February 2016 22:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany
Contact:

Re: Get time from xml file

Post by sailmich »

Thanks, I will check this.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Get time from xml file

Post by ben53252642 »

Re-check that code, had set +3 to -3 for testing.

Should be +3 (eg + x minutes)

Some one line testers if you want to play with it on the terminal:

create a file somewhere eg: /test

Run the below command and nothing should be output if its less than 3 minutes old and the message should echo if its older.

if (find /test -mmin +3 -type f | grep -q /); then echo "file is older than x minutes"; fi

38761237687641234.JPG
38761237687641234.JPG (36.37 KiB) Viewed 1794 times
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Get time from xml file

Post by ben53252642 »

Also consider running this on the weather Pi, I use it on all my sensor Pi's to auto reboot them if they can't see the main domoticz due to eg not auto reconnecting to the WiFi network.

It auto reboots the Pi if it can't see the target IP for 15 minutes.

Code: Select all

#!/bin/bash

target="192.168.0.5"

# Wait for network to come up
sleep 120

while true; do

echo "Checking target $target every minute for 15 minutes"
ping -c15 -i900 "$target" > /dev/null

if [ $? != 0 ]
then
  sudo /sbin/shutdown -r now
fi

sleep 1
done
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
sailmich
Posts: 245
Joined: Wednesday 17 February 2016 22:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany
Contact:

Re: Get time from xml file

Post by sailmich »

Watchdog service doesn't start after reboot. :o I use the watchdog.conf to check if the file is older than 3 minutes.
Spoiler: show

sudo service watchdog status
● watchdog.service - watchdog daemon
Loaded: loaded (/lib/systemd/system/watchdog.service; static)
Active: inactive (dead)
After starting the service manually.
Spoiler: show
sudo service watchdog status
● watchdog.service - watchdog daemon
Loaded: loaded (/lib/systemd/system/watchdog.service; static)
Active: active (running) since Thu 2019-08-29 09:43:38 CEST; 9s ago
Process: 1074 ExecStart=/bin/sh -c [ $run_watchdog != 1 ] || exec /usr/sbin/watchdog $watchdog_options (code=exited, status=0/SUCCESS)
Process: 1071 ExecStartPre=/bin/sh -c [ -z "${watchdog_module}" ] || [ "${watchdog_module}" = "none" ] || /sbin/modprobe $watchdog_module (code=exited, status=0/SUCCESS)
Main PID: 1076 (watchdog)
CGroup: /system.slice/watchdog.service
└─1076 /usr/sbin/watchdog

Aug 29 09:43:38 Wetterstation watchdog[1076]: int=1s realtime=yes sync=no so...0
Aug 29 09:43:38 Wetterstation watchdog[1076]: ping: no machine to check
Aug 29 09:43:38 Wetterstation watchdog[1076]: file: /var/www/weewx_pws.xml:300
Aug 29 09:43:38 Wetterstation watchdog[1076]: pidfile: no server process to ...k
Aug 29 09:43:38 Wetterstation watchdog[1076]: interface: no interface to check
Aug 29 09:43:38 Wetterstation watchdog[1076]: temperature: no sensors to check
Aug 29 09:43:38 Wetterstation watchdog[1076]: test=none(0) repair=none(0) al...o
Aug 29 09:43:38 Wetterstation watchdog[1076]: cannot set timeout 60 (errno =...)
Aug 29 09:43:38 Wetterstation watchdog[1076]: hardware watchdog identity: Br...r
Aug 29 09:43:38 Wetterstation systemd[1]: Started watchdog daemon.
Hint: Some lines were ellipsized, use -l to show in full.
sailmich
Posts: 245
Joined: Wednesday 17 February 2016 22:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany
Contact:

Re: Get time from xml file

Post by sailmich »

Found in the www for Jessie it's necessary to add follow line in /lib/systemd/system/watchdog.service
[Install]
WantedBy=multi-user.target

Now watchdog is starting at boot:)
Thank you for your help!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest