ariela
(Andrea Riela)
April 1, 2018, 9:02am
1
Hi all,
I wish a Happy Easter to all of you.
I’m looking for a good solution how to parse a file and retrieve some data to use in my items.
the command I’m using to create the file is:
/usr/bin/curl -s -XGET ‘https://api.openuv.io/api/v1/uv?lat=xxxmyLATxxx&lng=xxxmyLNGxxx ’ -H “x-access-token: xxxmyAPIkeyxxx” > /etc/openhab2/myscripts/openuv.txt
and the command to retrieve, for example, the current uv index is:
sed -e ‘s/.“uv”:(. ),“uv_time”.*/\1/’ openuv.txt
Another application of this kind of rule should be to detect the alarms from meteoalarm.eu. But in that case I don’t maybe need to create a file, just parse directly the html online.
Any suggestion how to approach this piece?
thanks
Andrea
vzorglub
(Vincent Regaud)
April 1, 2018, 9:05am
2
What is the content of the file?
ariela
(Andrea Riela)
April 1, 2018, 9:07am
3
considering just the first example:
openuv.txt
{“result”:{“uv”:0,“uv_time”:“2018-03-31T18:43:05.376Z”,“uv_max”:4.2129,“uv_max_time”:“2018-03-31T11:29:01.105Z”,“ozone”:286.9,“ozone_time”:“2018-03-31T18:06:09.514Z”,“safe_exposure_time”:{“st1”:null,“st2”:null,“st3”:null,“st4”:null,“st5”:null,“st6”:null},“sun_info”:{“sun_times”:{“solarNoon”:“2018-03-31T11:29:01.105Z”,“nadir”:“2018-03-30T23:29:01.105Z”,“sunrise”:“2018-03-31T05:41:01.146Z”,“sunset”:“2018-03-31T17:17:01.064Z”,“sunriseEnd”:“2018-03-31T05:44:04.304Z”,“sunsetStart”:“2018-03-31T17:13:57.905Z”,“dawn”:“2018-03-31T05:11:30.986Z”,“dusk”:“2018-03-31T17:46:31.223Z”,“nauticalDawn”:“2018-03-31T04:37:13.570Z”,“nauticalDusk”:“2018-03-31T18:20:48.639Z”,“nightEnd”:“2018-03-31T04:02:30.268Z”,“night”:“2018-03-31T18:55:31.942Z”,“goldenHourEnd”:“2018-03-31T06:20:26.971Z”,“goldenHour”:“2018-03-31T16:37:35.239Z”},“sun_position”:{“azimuth”:1.3869527730340767,“altitude”:-0.2801036994396005}}}}
Unfortunately I can’t use (or at least, I failed every time) curl in a rule because of that header with the API key in the request. Also HTTP binding seems not a viable solution.
The site is saying I can use also other solutions, not only curl:
javascript
node.js
ruby
java
swift
Andrea
vzorglub
(Vincent Regaud)
April 1, 2018, 9:16am
4
Buona Pasqua
Put the file in the conf/html folder
Retreive the file with the http binding with the url address as `localhost:8080/openuv.txt
and then use a JSONPATH transform to retreive you data
1 Like
ariela
(Andrea Riela)
April 1, 2018, 9:20am
5
“Put the file in the conf/html folder”
how can put the output of this curl in conf/html folder? via rule is possible? or do I need to put an action in the cron (but not sure I can create as openhab:openhab).
vzorglub
(Vincent Regaud)
April 1, 2018, 9:25am
6
Simply change the destination file in your command:
/usr/bin/curl -s -XGET ‘https://api.openuv.io/api/v1/uv?lat=xxxmyLATxxx&lng=xxxmyLNGxxx’ -H “x-access-token: xxxmyAPIkeyxxx” > /etc/openhab2/myscripts/openuv.txt
You can use executeCommand
in OH to run the command
ariela
(Andrea Riela)
April 1, 2018, 9:27am
7
mmm I think I will receive an error, I don’t know if openhab user can do curl + https.
Let me try right now
Andrea
vzorglub
(Vincent Regaud)
April 1, 2018, 9:35am
8
If it doesn’t work, you will need to change the permissions for openhab user.
Not very good at that… I google it everytime
ariela
(Andrea Riela)
April 1, 2018, 9:36am
9
it seems working fine … the file is ok, with right permissions.
conf/html is /etc/openhab2/html in OH2?
vzorglub
(Vincent Regaud)
April 1, 2018, 9:41am
10
It is on my machine. I did an apt based installation.
ariela
(Andrea Riela)
April 1, 2018, 10:01am
11
WOW it works thanks @vzorglub much appreciated.
Now I need to work on parsing the html from meteoalarm.eu
In that case I can’t use jsonpath … maybe a script with “sed” will do the trick
vzorglub
(Vincent Regaud)
April 1, 2018, 10:12am
12
What data do you get from meteoalarm.eu?
ariela
(Andrea Riela)
April 1, 2018, 10:35am
13
directly from the source:
view-source:https://www.meteoalarm.eu/en_UK/0/0/EE002-Hiiumaa.html
this is an example with 2 alerts. As you know, Wunderground is not publishing alerts data anymore, so I need to find another way.
Idea is to parse directly the html, find the right words (Alert/Level), and in case of multiple alerts put the output in array and put in OH items.
I should use also the RSS … maybe easier. We have also today and tomorrow in the same page:
http://meteoalarm.eu/documents/rss/ee/EE002.rss
In this case I should try to find:
“Today”
“awt:1 level:2” -> that means alert 1 level 2
…
“Tomorrow”
…
any suggestion?
Andrea
vzorglub
(Vincent Regaud)
April 1, 2018, 10:47am
14
XPATH transform?
Sorry I haven’t really touched these. More of a json kind of guy.
You could get the html into a js transform…