How to parse a file

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

What is the content of the file?

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

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

“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).

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

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

If it doesn’t work, you will need to change the permissions for openhab user.
Not very good at that… I google it everytime

it seems working fine … the file is ok, with right permissions.

conf/html is /etc/openhab2/html in OH2?

It is on my machine. I did an apt based installation.

WOW it works :slight_smile: thanks @vzorglub :slight_smile: 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

What data do you get from meteoalarm.eu?

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

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…