[SOLVED] Parse file and make items

I’m a beginner in openhab and I have a Weather station which uses CumulusMX weather software. CumulusMX makes a realtime.txt file and I would like to parse the file in openhab and make some items.

Is it possible and how.

Start from here: https://community.openhab.org/search?q=parse%20txt%20file

try out some things… when stuck, post configs and ask for help.

1 Like

I have tried that but it can be difficult to know where to start when you are a beginner.

check the recommendations that Vincent gives here:

post the content of realtime.txt file (using Code Fences) How to use code fences

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