Pass a sensor value to a sh script?

Hi

I'm currently using a bespoke windows app to harvest values from Velbus networks and append a CSV file (one file per sensor).

These files are then uploaded once an hour to a webserver, where a JSCanvas script creates interactive graphs.

http://www.cuedup.biz/VGraphing/demoui/

I'd like to migrate everything to a slim Linux server and host the JSCanvas scripts locally. Assuming the OR webserver supports the JSCanvas scripts.
(Which will also save the FTP transfer every hour, resulting in more up to the minute graphs)

So my question today is...

If an Echo command like :-

Echo $date,$sensor_value >> energy1.csv

Was used in a SH file, could a rule in OR be used to pass a sensor value to the SH script (whenever the sensor changed?)

I might take it a bit further and use a regex query so that the name of the sensor is used to determine which SH script is used.

Examples :-

Sensor name :- electric_current

SH script :- electric_current.sh

CSV file :- electric_current.csv

Sensor name :- electric_total

SH script :- electric_total.sh

CSV file :- electric_total.csv

Sensor name :- water_current

SH script :- water_current.sh

CSV file :- water_current.csv

Sensor name :- GP01

SH script :- GP01.sh

CSV file :- GP01.csv

Just for reference, I've tried getting the RRD4J graphs to work, but I have the following issues

The sensors aren't being consistently recognised. (Each reboot works with different sensors. Of 4 I'm testing with, it never works with all 4 at the same time)

The graphs that are created are static PNG files, rather than interactive graphs that JSCanvas creates.

Any ideas?

Hi

I've read this tutorial and I think I can follow it... With one minor detail..

http://openremote.org/display/docs/OpenRemote+2.0+How+To+-+Execute+Shell+commands

There doesn't appear to be an example of a SH file which accepts the param values.

If you search OpenRemote Smappee how to then there are many examples of passing parameters to shell scripts. Smappee uses oath2 authentication API, which is not implemented in OpenRemote. By passing parameters to the curl command scripts, I was able to make usage of Smappee API.

Thanks Michal,

That really helped :slight_smile:

I've created this script and it works, right up until the last line, where I'm trying to get the output appended into a file as defined by the --file variable.

while [ "$#" -gt 0 ]; do
  case "$1" in
    -v) value="$2"; shift 2;;
    -f) file="$2"; shift 2;;
    -t) text="$2"; shift 2;;
    --value=*) value="${1#*=}"; shift 1;;
    --file=*) file="${1#*=}"; shift 1;;
    --text=*) text="${1#*=}"; shift 1;;
    --value|--file|--text) echo "$1 requires an argument"

&2; exit 1;;

    -*) echo "unknown option: $1" >&2; exit 1;;
    *) handle_argument "$1"; shift 1;;
  esac
#echo $value
#echo $file
#echo $text
echo $(date +%y%m%d%H%M),$value
#echo $(date +%y%m%d%H%M),$value >> $file

done

Command line might be

./script --value=12344 --file=test.csv

My idea is to create an OR command with the $param feature and then a rule that pushes a sensor's value and name in.

So an energy monitoring sensor might be called electric.csv with a value of 661.65

The resulting command line from the rule would become

script --value=661.65 --file=energy.csv

If you cannot append to a file the first thing which should be checked is if you have correct write permissions. For the file and directory.

Hi

It turns out that it's much easier than that..

$file

Didn't work...(ambiguous output)

However..

${file}.csv

Does :slight_smile:

Update....

I've got a heap of JSCanvas graphs working with the OR webserver, using old data from the windows app.

So next...

Is to create the elements in OR to get the data pushed into some csv files....

Okay…

I’m going to try this rule to harvest the data sources from any sensor that matches the pattern

*.graph

The idea is to create command lines like this :-

csvmaker.sh --value=123445 --file=electric_instant.graph

The OR command has this line

–value=${param}

Oh dear....

There's an error in that rules :frowning:

What does.... "Source can not be resolved to a variable" mean ?

Error is here: -

execute.command("csvmaker", Status + " --file=" + source)

Should be: -

execute.command("csvmaker", Status + " --file=" + $source)

Thanks :slight_smile:

By creating a regex rule I now have all the csv files I need to use with JSCanvas graphing code.

Or I can pull the csv files into a spreadsheet if I want to analyse them.

Hi Stuart.

I have tried using your method of writing to a file using your csvmaker.sh script and a execution shell command that passes a float value to the csv file.

The OR execution shell command directs to the csvmaker.sh file properly. The parameter field in the command is like yours - --value=$param.

The outcome is that the file contains the time and literally $param instead of the value.

The rule has execute.command(“bash file name”, some fact value).

I have no idea why I get the text instead of the fact value.

Can you help on this one?

בתאריך יום שלישי, 15 בנובמבר 2016 בשעה 18:05:10 UTC+2, מאת Stuart Hanlon:

Hi

Sorry, my apologies.

There is a tiny tweak that I haven’t amended in these posts.

The correct format for $param is to enclose it.

So it should read --value=${param}

That applies to any use of ${param} in OpenRemote

For reference, this topic was expanded in another thread…

https://groups.google.com/forum/?nomobile=true#!topic/openremotecommunity/OoHpROaS8As

``

Hi Stuart.

I am aware to all of this.

The issue it that when the OR write command that runs the csvmake.sh is executed in my rule as

execute.comma(OR command name, passed parameter),

the file is filled with the time and the text ${param} instead of the value that is there each time it is being run. The csvmake.sh is based on your post with a little change and the script file runs fine in a terminal.

The read command works fine inside a rule.

I do not call the sensor name Dummy but the name of the sensor that exists in this device which handles the logging issue.

The idea was to log a parameter I accumulate along each day and if I boot the server or it is rebooted due to power failure I do not loose the data.

BTW, I have used --value=${param} and $param as well but it remains the same.

The rule looks like this (see last line):

ule “Get WT Factors”
no-loop true
timer(cron: 5 0/30 8-18 * * ?)
// duration 990
when
$wt_factors: Water_Tank_Factors()
CustomState( source == “Roof_Temp_Sensor”, $val_T: value)
CustomState( source == “Roof_Brightness_Sensor”, $val_L: value)
then
$wt_factors.setAcc_temp ($wt_factors.getAcc_temp() + Float.valueOf($val_T.toString()));
update ( $wt_factors );
$wt_factors.setAcc_light ($wt_factors.getAcc_light() + Float.valueOf($val_L.toString()));
update ( $wt_factors );
$wt_factors.setAcc_sigma ($wt_factors.getAcc_sigma() + (Float.valueOf($val_T.toString() * 1400 )+ Float.valueOf($val_L.toString());
update ( $wt_factors );
System.out.println ( _TimeStamp()+ ": Daily Temp accomulation: " + $wt_factors.getAcc_temp());
System.out.println ( _TimeStamp()+ “: Daily Light accomulation: " + $wt_factors.getAcc_light());
System.out.println ( _TimeStamp()+ “: Daily Light + Temp accomulation: " + $wt_factors.getAcc_sigma());
// execute.command(“Write Light and Temp Coefficient”, String.format(java.util.Locale.US,”%.1f”, $wt_factors.getAcc_sigma()));
execute.command(“Write Light and Temp Coefficient”, String.valueOf($wt_factors.getAcc_sigma()));
end

Thanks.

בתאריך יום רביעי, 14 במרץ 2018 בשעה 23:09:15 UTC+2, מאת Stuart Hanlon:

Hi

It's great to see that you want to use this.

Unfortunately I'm not a coder, so what you're doing with the rules is beyond me.

Using my electrical head, I'd ask if you've halved the setup to confirm that the ${param} part is working?

If you just link the command to a slider it should pass the slider value to the script.

One thing to be aware of is that there is a bizarre bug that requires the whole machine to be rebooted before any changes to command line things get activated.

I hope this helps.

Hi Stuart.

From some strange reason after replacing the {} back, it is working. I have no idea besides a bug. It was like this from the beginning and than I have tried to remove the {} looking at your old post.

Anyway, now it is working. BWT I am an electrical engineer as well.

Now I have a plan to use Sonoff metered relay over WiFi and measure the sockets. My KNX lighting switches is an easy task with your help here.

I may have to re-flash them to get an open device but I did not get theme yet.

I also have a Modbus Power meter which is located on the main utility line so eventually I hope it will al work fine.

בתאריך יום שבת, 17 במרץ 2018 בשעה 09:55:20 UTC+2, מאת Stuart Hanlon:

Not the whole machine Stuart. Killing & starting the controller is enough :slight_smile:

@Michal, As always, I’m sure you are correct.
I just reboot the machine as it’s only ever running OpenRemote and a Velbus server :wink:

@Nir,

I’m glad it’s all working for you.

Maybe it was something to do with restarting the controller between edits of the shell execution command.

Good luck with adding your other components.