Pass a sensor value to a sh script?

131 views
Skip to first unread message

Stuart Hanlon

unread,
Nov 14, 2016, 6:50:44 AM11/14/16
to OpenRemote
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?

Stuart Hanlon

unread,
Nov 14, 2016, 7:10:21 AM11/14/16
to OpenRemote
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.

Michal Rutka

unread,
Nov 14, 2016, 10:10:55 AM11/14/16
to OpenRemote
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.

Stuart Hanlon

unread,
Nov 15, 2016, 4:42:00 AM11/15/16
to OpenRemote
Thanks Michal,


That really helped :-)


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

Michal Rutka

unread,
Nov 15, 2016, 5:06:36 AM11/15/16
to OpenRemote
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.

Stuart Hanlon

unread,
Nov 15, 2016, 5:23:02 AM11/15/16
to OpenRemote
Hi

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


>>$file

Didn't work...(ambiguous output)


However..

>> ${file}.csv


Does :-)

Stuart Hanlon

unread,
Nov 15, 2016, 6:25:44 AM11/15/16
to OpenRemote
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....

Stuart Hanlon

unread,
Nov 15, 2016, 11:05:10 AM11/15/16
to openremot...@googlegroups.com
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}


###################


package org.openremote.controller.protocol;

global org.openremote.controller.statuscache.CommandFacade execute;

import java.util.*;
import java.util.regex.*;


// JSCanvas

// the aim of this rule is that any device can have it's output graphed
// by mapping it to a Custom Sensor, where any numeric value is passed, or a text value is mapped to a given number


rule "any .graph sensor"

when
    $evt:Event(source matches "^.*.graph\\d+$", $source : source)
then


// This next line will extract the value of the sensor and hold it in a string

    String Status = $evt.getValue().toString();

    System.out.println("JSCanvas Graph datasource Status Change '" + $source + "': " + Status);


   try {

     
      execute.command("csvmaker", Status + " --file=" + source);
   
   } catch (Exception e) {
      System.out.println("############## JSCanvas datasource error : " + e.getMessage() + " #################");
      return;
   }

end

Stuart Hanlon

unread,
Nov 15, 2016, 1:23:41 PM11/15/16
to OpenRemote
Oh dear....


There's an error in that rules :-(


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

Richard Turner

unread,
Nov 15, 2016, 3:21:02 PM11/15/16
to OpenRemote
Error is here: -

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

Should be: -

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

Stuart Hanlon

unread,
Nov 18, 2016, 7:51:14 AM11/18/16
to OpenRemote
Thanks :-)


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.

Nir Aviry

unread,
Mar 14, 2018, 3:47:05 PM3/14/18
to OpenRemote
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:
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

Stuart Hanlon

unread,
Mar 14, 2018, 5:09:15 PM3/14/18
to openremot...@googlegroups.com
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...




Nir Aviry

unread,
Mar 16, 2018, 1:02:03 AM3/16/18
to openremot...@googlegroups.com
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:

Stuart Hanlon

unread,
Mar 17, 2018, 3:55:20 AM3/17/18
to openremot...@googlegroups.com
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.

Nir Aviry

unread,
Mar 17, 2018, 5:31:23 AM3/17/18
to OpenRemote
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:

Michal Rutka

unread,
Mar 17, 2018, 7:48:34 AM3/17/18
to OpenRemote
Not the whole machine Stuart. Killing & starting the controller is enough :-)

Stuart Hanlon

unread,
Mar 17, 2018, 9:10:22 AM3/17/18
to openremot...@googlegroups.com
@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 ;-)


@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.

Reply all
Reply to author
Forward
0 new messages