Read/parse text file

I’m hoping someone can point me in the right direction. I’m looking for a way to read and parse a csv file that is sitting on a Windows share on my network. The file is generated by Weather Display software connected to my Davis Vantage Pro2. It’s a two-line file - the first line is a header while the second line contains the actual values I’m looking to grab and put into items.

If I did it right, there should be a sample screenshot of the file below:

I do see the Davis binding is available, but I need to keep the weather station on the Windows machine as the Weather Display software also feeds a website.

In my previous setup, I used the local weather data for decision making on HVAC (inside vs outside temperature), irrigation, lighting (thanks to the solar unit), and more. It was one of those pieces that added high WAF to the setup.

Any help would be greatly appreciated.

Many thanks for your time,

Brian

1 Like

I’ll preface this post by saying my knowledge of Linux is very shallow. I can follow directions and hack together some things that work, but my terminology is suspect at best. Having said that, I am wondering if I can parse the .txt file noted in my previous post by using a bash script (If I have that term right?) and the exec binding.

I found the snippet (below) for parsing a csv file here and tested it - it does indeed work at parsing out the values. It would be quite a lengthy script with all the variables, but it’s doable.

So my new question: How can I pass these variables into OpenHab? I understand the echo command simply dumps these values out to the command line. Is the exec binding the right tool for the job? If so, any tips on pulling data from a script like this?

Thanks for any ideas,

Brian

#!/bin/bash
IFS=","
while read f1 f2 f3
do
    echo "Year        : $f1"
    echo "Month  : $f2"
    echo "Day   : $f3"
done < /mnt/wxlogs/scriptlog.txt

The exec binding will put what ever the shell script prints (i.e. Echos) into the item it is bound to. So your three options are

  1. write the script that prints out all the values and use a transform in your binding to extract specific values for different items.

  2. write your script to accept an argument to only print out tge chosen value and each exec bound item passes a different argument

  3. bind the exec to a String Item and parse out and update items from a rule.

Personally, if i were going through the trouble of writing the script in the first place I woukd choose 2.

1 Like

Thanks, @rlkoshak - it looks like you’ve given me lots to chew on. I’ll get to work on this.

I appreciate your time!

Brian

I’m making very good progress here. I went with your 2nd suggestion and wrote a script to which I can pass an argument asking for a specific field:

#!/bin/bash
intWx=$1
while  IFS= read -r line;do
	fields=($(printf "%s" "$line"|cut -d"," --output-delimiter=' '  -f1-))
        echo ${fields[intWx]}
done < /mnt/wxlogs/scriptlog.txt

This outputs the following when I run "./wxFunction.sh 12 (the 12 asks for the 12th field in this case)

temp
27.4

In a perfect world, I would rather not have “temp” be returned, but one step at a time.

What I’m lost now on is how to bind this in my items file and how it actually gets updated. Am I close with the following format:

Number Weather_Temperature 	"Outside Temperature [%.1f °F]"    {exec="<[/bin/wxFunction.sh@@12]"}

I wasn’t clear from any of the examples from the wiki page.

Finally, how does this get updated? The scriptlog file gets updated once a minute. I’m thinking this doesn’t auto-update into OpenHAB, so should I write a rule to run the script or do it via cron, or is there a better way?

Sorry for asking so many questions, but I think I’m getting close! :+1:

Brian

Okay, I’m switching things up a bit after reading another post on the board about the exec binding. I have since cleaned up my bash script.

I can call this script from a rule (and hopefully eventually from the exec binding), pass it an argument (i.e. I want to retrieve the 12th field) and populate an item in OpenHAB. This script reads my two-line txt file (which is really a CSV file), skips the first line and only reads the second line. I used this page as a reference for writing this script:

linesToSkip=1
intWx=$1
{
for ((i=$linesToSkip;i--;)) ;do
    read
    done
while read line ;do
   # echo $line
   fields=($(printf "%s" "$line"|cut -d"," --output-delimiter=" " -f1-))
   echo ${fields[intWx]}
done
} < /mnt/wxlogs/scriptlog.txt

This is properly reporting values now when running it from a rule as suggested in the aforementioned post.

I’m debating now whether to stick with running the script and updating my Davis weather items via a rule or using the exec binding to update the item. I will have 10 - 15 items to define. In your experience, which is the better way to go? I’m still not getting how to handle the formatting of the exec binding in my items file.

Thanks for putting up with me!

Brian

1 Like

I prefer to use a rule. It is easier to detect and debug problems.