Re: Water level river Waal
Posted: Saturday 30 December 2023 10:05
I will take a look at it and hopefully come up with a solution, either today or tomorrow.
Regards
Regards
Code: Select all
local WHDev = 'Waterhoogte Nijmegen' -- Create a General/Distance sensor to be used.
local scriptName = 'waterhoogteNijmegen'
local function split (str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
return {
active = true,
-- active = false,
on = {
timer = {
'every 10 minutes', -- there is no need to go below 10 mintes.
},
httpResponses = {
scriptName
}
},
logging = {
level = domoticz.LOG_INFO,
-- level = domoticz.LOG_DEBUG,
marker= 'WHN'
},
execute = function(dz, item)
if item.isTimer then
dz.openURL({
url = 'https://waterinfo.rws.nl/api/chart/Get?mapType=waterhoogte&locationCode=Nijmegen-haven%28NIJM%29&values=-6,3',
method = 'GET',
callback = scriptName,
})
end
if (item.isHTTPResponse) then
if (item.ok) then
-- For DEBUG purpose, can be deleted
local csvHandle
csvHandle = assert(io.open('/tmp/'..scriptName..'.csv', "w"))
csvHandle:write(item.data)
csvHandle:close()
-- For DEBUG purpose
local waterHeight, WHTmp
local i, k, t = {}
t = split(item.data, "\n")
for i = 1, #t do
k = t[i]
WHTmp = split(k, ';')[4]
if WHTmp == nil or WHTmp == '' then
break
end
waterHeight = split(k, ';')[4]
end
dz.log(WHDev..': '..tostring(waterHeight), dz.LOG_INFO)
dz.devices(WHDev).updateDistance(tonumber(waterHeight))
else
dz.log('There was a problem handling the request', dz.LOG_ERROR)
dz.log(item, dz.LOG_ERROR)
end
end
end
}
Code: Select all
[
{
"id": "47db295ed5bd870e",
"type": "http request",
"z": "f1324d44.f6688",
"name": "Waterstand Nijmegen Haven",
"method": "GET",
"ret": "txt",
"paytoqs": "body",
"url": "https://waterinfo.rws.nl/api/chart/Get?mapType=waterhoogte&locationCode={{{location}}}&values=-6,3",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 420,
"y": 400,
"wires": [
[
"76acdca27168cb32"
]
]
},
{
"id": "a970ebacb7a9b8e7",
"type": "inject",
"z": "f1324d44.f6688",
"name": "10 minutes Inject",
"props": [
{
"p": "location",
"v": "Nijmegen-haven(NIJM)",
"vt": "str"
},
{
"p": "idx",
"v": "30",
"vt": "num"
}
],
"repeat": "600",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"x": 150,
"y": 400,
"wires": [
[
"47db295ed5bd870e"
]
]
},
{
"id": "76acdca27168cb32",
"type": "csv",
"z": "f1324d44.f6688",
"name": "CSV",
"sep": ";",
"hdrin": true,
"hdrout": "none",
"multi": "mult",
"ret": "\\n",
"temp": "semicolon seperated values",
"skip": "0",
"strings": false,
"include_empty_strings": false,
"include_null_values": false,
"x": 650,
"y": 400,
"wires": [
[
"3eb0f740b4a226f7"
]
]
},
{
"id": "3eb0f740b4a226f7",
"type": "function",
"z": "f1324d44.f6688",
"name": "Function",
"func": "let location;\nlet actual;\nlet estimated;\nlet time;\nlet level;\n\nfor (i = 0; i < msg.payload.length; i++) {\n if (typeof (msg.payload[i][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"]) === 'undefined') {\n actual = msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"];\n estimated = msg.payload[i-1][\"Waterhoogte verwacht Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"];\n time = msg.payload[i-1].Datum + \" \" + msg.payload[i-1][\"Tijd (NL tijd)\"];\n location = msg.payload[i-1].Locatie;\n\nswitch (true) {\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > 1220):\n level = 4\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > 1055):\n level = 3\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > 965):\n level = 2\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > 535):\n level = 1\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] < 535):\n level = 0;\n break;\n\n }\n \n msg.payload = { \"command\" : \"udevice\", idx : msg.idx, \"nvalue\": level, \"svalue\": \"Location: \" + location + \"; Date/Time: \" + time + \"\\n\" + \"Actual: \" + actual + \" cm\" + \"; Estimated: \" + estimated + \" cm\"};\n\n return msg;\n }\n}",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 820,
"y": 400,
"wires": [
[
"e62499116c78e3a7",
"6b2b322a97983848"
]
]
},
{
"id": "e62499116c78e3a7",
"type": "debug",
"z": "f1324d44.f6688",
"name": "debug 230",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1030,
"y": 460,
"wires": []
},
{
"id": "6b2b322a97983848",
"type": "mqtt out",
"z": "f1324d44.f6688",
"name": "Naar Domoticz",
"topic": "domoticz/in",
"qos": "0",
"retain": "false",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "108bbff65a6cdbf3",
"x": 1040,
"y": 400,
"wires": []
},
{
"id": "108bbff65a6cdbf3",
"type": "mqtt-broker",
"name": "",
"broker": "192.168.10.51",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]
I have Spijkenisse as measuring stationFireWizard wrote: ↑Monday 01 January 2024 20:49 Which measuring station do you use? Nijmegen haven?
I do not see that issue.
Regards
Code: Select all
[
{
"id": "47db295ed5bd870e",
"type": "http request",
"z": "f1324d44.f6688",
"name": "Waterstand Nijmegen Haven",
"method": "GET",
"ret": "txt",
"paytoqs": "body",
"url": "https://waterinfo.rws.nl/api/chart/Get?mapType=waterhoogte&locationCode={{{location}}}&values=-6,3",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 420,
"y": 400,
"wires": [
[
"76acdca27168cb32"
]
]
},
{
"id": "a970ebacb7a9b8e7",
"type": "inject",
"z": "f1324d44.f6688",
"name": "10 minutes Inject",
"props": [
{
"p": "location",
"v": "Nijmegen-haven(NIJM)",
"vt": "str"
},
{
"p": "idx",
"v": "30",
"vt": "num"
},
{
"p": "level_limit",
"v": "{\"laag\":535,\"normaal\":535,\"verhoogd\":965,\"hoog\":1220,\"extreem_hoog\":1340}",
"vt": "json"
}
],
"repeat": "600",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"x": 150,
"y": 400,
"wires": [
[
"47db295ed5bd870e"
]
]
},
{
"id": "76acdca27168cb32",
"type": "csv",
"z": "f1324d44.f6688",
"name": "CSV",
"sep": ";",
"hdrin": true,
"hdrout": "none",
"multi": "mult",
"ret": "\\n",
"temp": "semicolon seperated values",
"skip": "0",
"strings": false,
"include_empty_strings": false,
"include_null_values": false,
"x": 650,
"y": 400,
"wires": [
[
"86b147ecec3872c9"
]
]
},
{
"id": "6b2b322a97983848",
"type": "mqtt out",
"z": "f1324d44.f6688",
"name": "Naar Domoticz",
"topic": "domoticz/in",
"qos": "0",
"retain": "false",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "38b35487299a036d",
"x": 1060,
"y": 400,
"wires": []
},
{
"id": "86b147ecec3872c9",
"type": "function",
"z": "f1324d44.f6688",
"name": "Function",
"func": "let location;\nlet actual;\nlet estimated;\nlet time;\nlet level;\n\nfor (i = 0; i < msg.payload.length; i++) {\n if (typeof (msg.payload[i][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"]) === 'undefined') {\n actual = msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"];\n if (typeof (msg.payload[i][\"Waterhoogte verwacht Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"]) !== 'undefined') {\n estimated = msg.payload[i-1][\"Waterhoogte verwacht Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"];\n }\n time = msg.payload[i-1].Datum + \" \" + msg.payload[i-1][\"Tijd (NL tijd)\"];\n location = msg.payload[i-1].Locatie;\n\nswitch (true) {\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.extreem_hoog):\n level = 4\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.hoog):\n level = 3\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.verhoogd):\n level = 2\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.normaal):\n level = 1\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] < msg.level_limit.laag):\n level = 0;\n break;\n\n }\n \n if (typeof estimated !== 'undefined') {\n msg.payload = { \"command\" : \"udevice\", \"idx\" : msg.idx, \"nvalue\": level, \"svalue\": \"Location: \" + location + \"; Date/Time: \" + time + \"\\n\" + \"Actual: \" + actual + \" cm\" + \"; Estimated: \" + estimated + \" cm\"};\n } else {\n msg.payload = { \"command\" : \"udevice\", \"idx\" : msg.idx, \"nvalue\": level, \"svalue\": \"Location: \" + location + \"; Date/Time: \" + time + \"\\n\" + \"Actual: \" + actual + \" cm\"};\n }\n return msg;\n }\n}",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 820,
"y": 400,
"wires": [
[
"6b2b322a97983848"
]
]
},
{
"id": "38b35487299a036d",
"type": "mqtt-broker",
"name": "Raspberry Pi 1",
"broker": "192.168.10.51",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]
Code: Select all
[
{
"id": "4e91eb13761f6744",
"type": "http request",
"z": "f1324d44.f6688",
"name": "Waterstand Spijkenisse",
"method": "GET",
"ret": "txt",
"paytoqs": "body",
"url": "https://waterinfo.rws.nl/api/chart/Get?mapType=waterhoogte&locationCode={{{location}}}&values=-6,3",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 410,
"y": 460,
"wires": [
[
"e368146f43533174"
]
]
},
{
"id": "c7b8ef209738446b",
"type": "inject",
"z": "f1324d44.f6688",
"name": "10 minutes Inject",
"props": [
{
"p": "location",
"v": "Spijkenisse(SPIJ)",
"vt": "str"
},
{
"p": "idx",
"v": "30",
"vt": "num"
},
{
"p": "level_limit",
"v": "{\"laag\":-67,\"normaal\":-67,\"verhoogd\":210,\"hoog\":10000,\"extreem_hoog\":10000}",
"vt": "json"
}
],
"repeat": "600",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"x": 150,
"y": 460,
"wires": [
[
"4e91eb13761f6744"
]
]
},
{
"id": "e368146f43533174",
"type": "csv",
"z": "f1324d44.f6688",
"name": "CSV",
"sep": ";",
"hdrin": true,
"hdrout": "all",
"multi": "mult",
"ret": "\\n",
"temp": "semicolon seperated values",
"skip": "0",
"strings": false,
"include_empty_strings": false,
"include_null_values": false,
"x": 650,
"y": 460,
"wires": [
[
"f950e6c89def1967"
]
]
},
{
"id": "f950e6c89def1967",
"type": "function",
"z": "f1324d44.f6688",
"name": "Function",
"func": "let location;\nlet actual;\nlet estimated;\nlet time;\nlet level;\n\nfor (i = 0; i < msg.payload.length; i++) {\n if (typeof (msg.payload[i][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"]) === 'undefined') {\n actual = msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"];\n if (typeof (msg.payload[i][\"Waterhoogte verwacht Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"]) !== 'undefined') {\n estimated = msg.payload[i-1][\"Waterhoogte verwacht Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"];\n }\n time = msg.payload[i-1].Datum + \" \" + msg.payload[i-1][\"Tijd (NL tijd)\"];\n location = msg.payload[i-1].Locatie;\n\nswitch (true) {\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.extreem_hoog):\n level = 4\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.hoog):\n level = 3\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.verhoogd):\n level = 2\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] > msg.level_limit.normaal):\n level = 1\n break;\n\n case (msg.payload[i-1][\"Waterhoogte Oppervlaktewater t.o.v. Normaal Amsterdams Peil in cm\"] < msg.level_limit.laag):\n level = 0;\n break;\n\n }\n \n if (typeof estimated !== 'undefined') {\n msg.payload = { \"command\" : \"udevice\", \"idx\" : msg.idx, \"nvalue\": level, \"svalue\": \"Location: \" + location + \"; Date/Time: \" + time + \"\\n\" + \"Actual: \" + actual + \" cm\" + \"; Estimated: \" + estimated + \" cm\"};\n } else {\n msg.payload = { \"command\" : \"udevice\", \"idx\" : msg.idx, \"nvalue\": level, \"svalue\": \"Location: \" + location + \"; Date/Time: \" + time + \"\\n\" + \"Actual: \" + actual + \" cm\"};\n }\n return msg;\n }\n}",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 820,
"y": 460,
"wires": [
[
"55c72613d26c5709"
]
]
},
{
"id": "55c72613d26c5709",
"type": "mqtt out",
"z": "f1324d44.f6688",
"name": "Naar Domoticz",
"topic": "domoticz/in",
"qos": "0",
"retain": "false",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "38b35487299a036d",
"x": 1060,
"y": 460,
"wires": []
},
{
"id": "38b35487299a036d",
"type": "mqtt-broker",
"name": "Raspberry Pi 1",
"broker": "192.168.10.51",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]