klayman
(John Doe)
March 10, 2023, 11:18pm
1
Hi,
I have a water protection device which has a web API presumably returning JSON data. Unfortunately I have no clue how to get it into OpenHab 3.
The URL is http://192.168.20.18:5333/safe-tec/get/BAR and results in
<html>
<head>
<meta name="color-scheme" content="light dark">
</head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">
{"getBAR":"4900 mbar"}
</pre>
</body>
</html>
I’ve tried to create a Thing with the HTTP Binding and then a channel and an item via the GUI. Code looks as follows:
UID: http:url:31c960574b
label: Syr SafeTech Connect
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: false
baseURL: http://192.168.20.18:5333/safe-tec/
delay: 0
stateMethod: GET
refresh: 30
commandMethod: GET
contentType: application/json
timeout: 3000
bufferSize: 2048
channels:
- id: SyrWaterPressure
channelTypeUID: http:number
label: Water Pressure
description: ""
configuration:
mode: READONLY
stateExtension: get/BAR
What am I doing wrong here?
Thanks,
Klayman
The result you have shown is JSON wrapped in XML.
What do your logs say?
klayman
(John Doe)
March 11, 2023, 8:41am
3
The logs are rather quiet. I now changed the channel type to String just to see if any data arrives. Seems to work, now I get {“getBAR”:“4800 mbar”} as the value of my item. How can I parse this now into a numerical value of 4.80? This is how my code looks like:
UID: http:url:e6162a0ae4
label: Syr SafeTech Connect
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: true
baseURL: http://192.168.20.18:5333/safe-tec/
delay: 0
stateMethod: GET
refresh: 3
commandMethod: GET
timeout: 3000
bufferSize: 2048
channels:
- id: SyrPressure
channelTypeUID: http:string
label: Water Pressure
description: ""
configuration:
mode: READONLY
stateExtension: get/BAR
Thank you!
Step one is to extract the 4800 mbar
Install the JSONPATH Transformation Service, then add
stateTransformation: JSONPATH:$.getBAR
to your Thing Channel configuration.
Your Item should now contain 4800 mbar
after the next update/refresh.
klayman
(John Doe)
March 11, 2023, 2:19pm
5
Done, now it shows 4900 mbar. What next, how can I strip the mbar and divide by 1000?
Ap15e
(BK)
March 11, 2023, 5:07pm
6
REGEX:(.*BAR":"([0-9]+).*)∩JS:(| parseFloat(input) / 1000)
(install REGEX and JS transformations) might work (untested).
rlkoshak
(Rich Koshak)
March 11, 2023, 9:14pm
7
You could leave it, define your Item as a Number:Pressure and set the state description pattern on the Item as %.0f µbar
. OH will do the conversion in units for you.
klayman
(John Doe)
March 11, 2023, 11:00pm
8
Ok, I now defined the item as Number:pressure and applied “%.0f µbar” as a pattern
value: " "
config:
pattern: "%.0f µbar"
readOnly: true
It now shows “4900 µbar” but when I click on Analyze, there is no data in the diagram…
klayman
(John Doe)
March 12, 2023, 11:31am
9
I think I found it: Because I changed the item from string to float, the persistence cannot work with the new data format under the same item. Unfortunately I don’t know how to delete old data fields from influxdb, even if I delete the item itself and recreate it under the same name, the error remains…
rlkoshak
(Rich Koshak)
March 12, 2023, 4:17pm
10
I haven’t used InfluxDB in some time but I do know they have excellent docs.
klayman
(John Doe)
March 13, 2023, 10:59am
11
Answer to self: Log on to linux console, start influxdb and then issue “drop measurement itemname”
2 Likes