sailmich wrote: ↑Wednesday 25 March 2020 11:01
Is it possible to get a time stamp for the xml file?
Yes, in below example I use station_time for that
You will have to change the ssh bootcommand in line 115 as I don't know what command you need for that.
Code: Select all
local url = 'http://xxx.xxx.xxx.xxx/weewx_pws.xml'
local scriptVar = 'getXML'
return
{
on =
{
timer =
{
'every minute at daytime',
},
devices =
{
'triggerhulp',
},
httpResponses =
{
scriptVar,
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},
data =
{
equalCounter = { initial = 0 },
stationTime = { initial = '' },
},
execute = function(dz, item)
local function osCommand(cmd)
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
if commandOutput:find '::ERROR::' then -- something went wrong
dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_DEBUG)
else -- all is fine!!
dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
end
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
local function wind2Degrees(direction)
if direction == 'N' then degrees = 360 end
if direction == 'NE' then degrees = 315 end
if direction == 'E' then degrees = 270 end
if direction == 'SE' then degrees = 225 end
if direction == 'S' then degrees = 180 end
if direction == 'SW' then degrees = 135 end
if direction == 'W' then degrees = 90 end
if direction == 'NW' then degrees = 45 end
return '-'
end
local function humidityStatus(temperature, humidity)
if humidity <= 30 then return dz.HUM_DRY
elseif humidity >= 70 then return dz.HUM_WET
elseif humidity >= 35 and
humidity <= 65 and
temperature >= 22 and
temperature <= 26 then return dz.HUM_COMFORTABLE
else return dz.HUM_NORMAL end
end
local function getData(data)
resultTable = dz.utils.fromXML(data).maintag.realtime
relevantData = {}
for index, record in ipairs(resultTable) do
-- dz.log((record.data._attr.realtime or record.data._attr.units) .. ': ' .. record.data[1],dz.log_DEBUG)
relevantData[(record.data._attr.realtime or record.data._attr.units)] = record.data[1]
end
return(relevantData)
end
function baroForecast(pressure) -- Return text
if pressure == nil then return nil -- Should not happen
elseif pressure < 966 then return dz.BARO_THUNDERSTORM
elseif pressure < 993 then return dz.BARO_CLOUDY_RAIN
elseif pressure < 1007 then return dz.BARO_CLOUDY
elseif pressure < 1013 then return dz.BARO_UNSTABLE
elseif pressure < 1033 then return dz.BARO_STABLE
end
return dz.BARO_SUNNY
end
local function updateDevices(data)
local rain = dz.devices('Regen') -- devicetype Rain
local temperature = dz.devices('WetterAußen') -- devicetype Temperature
local wind = dz.devices('Boen') -- devicetype Wind
local humidity = dz.devices('Humidity') -- devicetype Humidity
local barometer = dz.devices('Barometer') -- devicetype Barometer
temperature.updateTemperature(data.temp)
wind.updateWind(wind2Degrees(data.winddir),data.winddir, data.windspeed, data.windGust, data.temp, data.windchill)
rain.updateRain(data.rain_rate, data.todaysrain )
humidity.updateHumidity(tonumber(data.hum), humidityStatus(tonumber(data.temp), tonumber(data.hum)))
barometer.updateBarometer(data.barometer, baroForecast(tonumber(data.barometer)))
end
if item.isHTTPResponse then
if item.ok then
local rt = getData(item.data)
if dz.data.stationTime == rt.station_time then dz.data.equalCounter = dz.data.equalCounter + 1
dz.log('Received old data. Skipping this reading', dz.LOG_ERROR)
if dz.data.equalCounter > 5 then
dz.log('Received old data for >= 5 consecutive times. Booting weatherpi', dz.LOG_ERROR)
osCommand('ssh bootCommand')
end
return
end
dz.data.stationTime = rt.station_time
dz.data.equalCounter = 0
updateDevices(rt)
else
dz.log('There was a problem handling the request', dz.LOG_ERROR)
dz.log(item, dz.LOG_ERROR)
end
return
end
dz.openURL(
{
url = url,
method = 'GET',
callback = scriptVar,
})
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>>
dzVents wiki