[SOLVED] Parse file and make items

That’s good. I forgot I did that!!

1 Like

btw that’s an ingenious trick (using jetty), especially easy for people who don’t want to write custom scripts to load and then parse content out of a file !

It now all depends if he can get his weather station to write the realtime.txt file in that directory.
Or just run a cron script to copy that file in that directory every minute or so

1 Like

This is the realtime.txt file and it updates every minute. The weather software runs on the same RPI that I run OpenHAB and is located here /home/pi/CumulusMX

16-11-18 09:39:11 7.1 86 4.9 0.3 1.0 315 0.0 0.0 1024.2 NW 1 m/s C hPa mm 38.2 +0.6 4.5 2454.9 0.0 19.4 54 7.1 +0.0 7.6 00:00 6.4 07:58 3.1 02:21 5.1 02:16 1024.2 09:20 1020.9 00:08 3.0.0 3043 2.7 7.1 6.4 0.0 0.00 0 116 0.0 2 1 0 ESE 893 ft 5.7 0.0 77 0 

by the way: there are multiple addons (like the Toolbox) on https://cumuluswiki.wxforum.net/a/Software that will help you “transform” the realtime.txt file into something more useful (and maybe easier to be read by OH2)

Have you tried any of those tools?

Ok so:

rule "Get weater station data"
when
    Time cron "1 * * ? * * *" // Every minute at 1 second past
then
    executeCommandLine("cp /home/pi/CumulusMX/realtime.txt /etc/openhab2/html/realtime.txt", 5000) //Copies the file into the html shared folder
    Thread::sleep(500) // give time for the system to copy the file
    val String rawWeather = sendHttpGetRequest("http://localhost:8080/static/realtime.txt")
    logInfo("RAW DATA", rawWeather)
    val weatherArray = rawWeather.split("_")
    Pressure.postUpdate(weatherArray.get(10))
    ...
end
2 Likes

I tried the script and created the items, but gets this error.

Error during the execution of rule ‘Get weater station data’: 10

Ok, let’s debug…

rule "Get weater station data"
when
    Time cron "1 * * ? * * *" // Every minute at 1 second past
then
    val String test1 = executeCommandLine("cp /home/pi/CumulusMX/realtime.txt /etc/openhab2/html/realtime.txt", 5000) //Copies the file into the html shared folder
    logInfo("TEST1", test1)
    Thread::sleep(500) // give time for the system to copy the file
    val String rawWeather = sendHttpGetRequest("http://localhost:8080/static/realtime.txt")
    logInfo("RAW DATA", rawWeather)
    // Example: 16-11-18 09:39:11 7.1 86 4.9 0.3 1.0 315 0.0 0.0 1024.2 NW 1 m/s C hPa mm 38.2 +0.6 4.5 2454.9 0.0 19.4 54 7.1 +0.0 7.6 00:00 6.4 07:58 3.1 02:21 5.1 02:16 1024.2 09:20 1020.9 00:08 3.0.0 3043 2.7 7.1 6.4 0.0 0.00 0 116 0.0 2 1 0 ESE 893 ft 5.7 0.0 77 0 

    val weatherArray = rawWeather.split("_")
    Pressure.postUpdate(weatherArray.get(10))
end

2018-11-16 13:12:01.071 [INFO ] [eclipse.smarthome.model.script.TEST1] -

2018-11-16 13:12:01.595 [INFO ] [ipse.smarthome.model.script.RAW DATA] - 16-11-18 13:11:42 9.2 79 5.7 1.0 1.7 135 0.0 0.0 1024.9 SE 1 m/s C hPa mm 58.7 +0.2 4.5 2454.9 0.0 19.7 54 9.2 +0.5 9.2 12:42 6.4 07:58 3.4 11:41 5.4 10:41 1025.1 13:02 1020.9 00:08 3.0.0 3043 3.1 9.2 8.7 0.0 0.00 0 190 0.0 1 1 0 S 1413 ft 7.5 0.0 164 0

2018-11-16 13:12:01.602 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘Get weater station data’: 10

Om that’s good we are getting the data
This was my fault the split("_") should be .split(" ") with a space instead of an underscore!!
Next:

rule "Get weater station data"
when
    Time cron "1 * * ? * * *" // Every minute at 1 second past
then
    val String test1 = executeCommandLine("cp /home/pi/CumulusMX/realtime.txt /etc/openhab2/html/realtime.txt", 5000) //Copies the file into the html shared folder
    //logInfo("TEST1", test1)
    Thread::sleep(500) // give time for the system to copy the file
    val String rawWeather = sendHttpGetRequest("http://localhost:8080/static/realtime.txt")
    logInfo("RAW DATA", rawWeather)
    // Example: 16-11-18 09:39:11 7.1 86 4.9 0.3 1.0 315 0.0 0.0 1024.2 NW 1 m/s C hPa mm 38.2 +0.6 4.5 2454.9 0.0 19.4 54 7.1 +0.0 7.6 00:00 6.4 07:58 3.1 02:21 5.1 02:16 1024.2 09:20 1020.9 00:08 3.0.0 3043 2.7 7.1 6.4 0.0 0.00 0 116 0.0 2 1 0 ESE 893 ft 5.7 0.0 77 0 

    val weatherArray = rawWeather.split(" ")
    Pressure.postUpdate(weatherArray.get(10))
end
2 Likes

Im getting this error but i’m getting data in my item

2018-11-16 13:49:36.470 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘cumulus.rules’, using it anyway:

The value of the local variable test1 is not used

Or put a soft link to that file so it appears in both folders simultaneously.

That’s just a warning and not an error. You can ignore it. You can uncomment the logInfo right after that line to make the warning go away.

Thank you very much vzorglub for your help. It’s working and I’m a very happy man.

Thanks
Please tick the post that gave you the solution
hc_292

Could you show an example on how to do that, please?

I believe that Rich means: instead of cpln -s in your command line

executeCommandLine("ln -s /home/pi/CumulusMX/realtime.txt /etc/openhab2/html/realtime.txt", 5000)

edit: on the other hand, this has to be done only once (from the O/S), so not needed within the rule that runs every minute.

2 Likes

Angelos has it exactly right. Run that command once from the command line and /etc/openhab2/html/realtime.txt will point to /home/pi/CumulusMX/realtime.txt until /etc/openhab2/html/realtime.txt is removed.

It’s kind of like a shortcut. It comes in really handy for situations like these. Though be aware that regardless what the permissions are on the link, the openhab users must have permission to read the original file as well.

If you are running in Docker this won’t work without some additional work.

2 Likes

Hello, I want to pick up that topic again and ask for support. I have a more or less similar usecase, i.e. I want to parse a .txt file that contains weather data. I want to “extract” single values only (e.g. for Temperature, Pressure etc.) and map these values to items. I tried the approach described above in this thread and it worked so far, but I still have the following limitations and questions.

  1. In my openhab.log the following is shown when I run the rule:
2020-04-06 21:02:01.528 [INFO ] [ipse.smarthome.model.script.RAW DATA] - 12345 0.0 0.0 90 12.5 42 1022.9 0.00 0.00 146.30 0.00 0.00 23.9 46 - 4 - - 0 0.00 - - - - - - - - - 21 01 01 Bayreuth,_Stolzingstrasse-21:01 0 - 06 04 - - - - - - - 23.5 10.3 20.2 2.0 4 - 0 0.0 0.0 0.1 1.0 1.1 1.6 1.4 2.0 2.1 2.2 0.9 0.2 0.1 0.0 0.0 0.0 0.0 0.1 0.3 0.9 6.8 -0.1 5040 06/04/2020 18.8 -1.1 23.8 22.9 - 0.0 0.0 0.1 1.0 1.1 1.6 1.4 2.0 2.1 2.2 17.2 19.3 20.0 19.8 19.1 18.3 16.9 15.3 13.8 12.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 20.2 2.0 12.5 3.3 0 _ _ 90.0 _ 0 - - - - - - - 0 26.8 22.6 - 1022.9 1019.7 0.0 20:00 16:09 - - 3.3 -4.6 0.0 2020 _ -1 0 -1 90 90 90 180 135 225 68 135 113 23 - - 0.0 - 49.966667 -11.59 - 66.0 31.0 - 09:39 !!C10.37Of!!

2020-04-06 21:02:01.532 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Get weater station data': The name 'Pressure' cannot be resolved to an item or type; line 13, column 5, length 8

What does the above Error-Message mean? Maybe that I have to create an item “Pressure” first? If yes, how do I have to define it in that specific case?

  1. As said I want to extract specific fields/values of the .txt file only. How do I “jump” to the right position in the file? Is that done by the command Pressure.postUpdate(weatherArray.get(10)) in the above example, i.e. does the value “10” in brackets represent the 10th field in the array somehow? If that is not the right track, how can I solve it?

Thanks a lot for your help.

Well, yes. If the Item does not exist yet, then your rule cannot post updates to it.
You create the Item however you usually create Items, either using a file like xxx.items or using PaperUI.
You’ll need to decide what type of Item you would like.

weatherArray.get(10)
selects the 11th result (counting starts at zero) from the array produced by the split earlier.

Thanks a lot for your swift reply. I’m a bit confused how to correctly define the item in that specific case. The first part is clear for me, but how to define the rest? I think I have to add something in curly brackets, but what and how?

Number Pressure "Luftdruck [%.2f hPa]" <pressure> { ??? ="[???]" }