This page last changed on Nov 26, 2014 by pappabjoern.

Hi,
I'm looking for a way to trigger events or do stuff if a certain device is connected to the network. Say that my wife is home and connected to the network, then I don't want a certain lamp turn on. But if she's not connected it should fire. If this is possible, could someone please explain how to get this to work.

Regards
pappabjoern


Capture1.PNG (image/png)
openrem.png (image/png)
dev.png (image/png)
folders.png (image/png)
check IP.png (image/png)
folders1.png (image/png)
check IP.png (image/png)
sensor_android.png (image/png)
sensor_ipad.png (image/png)
sensor_ipod.png (image/png)
sensor_agg.png (image/png)
sensor_all.png (image/png)
crontab.png (image/png)
crontab1.png (image/png)
vi_crontab.PNG (image/png)
owner.png (image/png)
create task.png (image/png)
create task1.png (image/png)
cron editor.png (image/png)
crontab2.png (image/png)
error.png (image/png)
task.png (image/png)
crontab3.png (image/png)
facts sensor_android.png (image/png)
pingsensor.png (image/png)
sensor_android.png (image/png)
sensor_ipad.png (image/png)
sensor_agg.png (image/png)
sensor_all.png (image/png)
sensor_android.png (image/png)
sensor_agg.png (image/png)
pingsensor.png (image/png)
dev off.png (image/png)
dev on.png (image/png)

hey mate.
i think this is possible in the free designer using the "ping " sensor . i have the pro version and we dont have it yet but want do the same thing.

regards jon

Posted by jon.gillioz at Feb 15, 2015 12:38

You can tell more where to look / read? How to configure?

Posted by aleksey_z at Mar 11, 2015 15:38

i've done exactly this, i use it to control entry / exit routines in the house. Turn on lights etc if it is dark, when i am out, simulating occupancy, turning off all of the plugs etc.

The way i did it (and probably not the best but it does work) was to write shell scripts running on linux (raspi) to detect ip addressed (assigned by router by MAC id). these shell scripts write out a value to a file, one for each of us in the house. These files are then reviewed by another script which decides if the house is completely empty or anyone is in. The output of this second script then writes another value to a file which is used (via a sensor input) to tell openremote whether the house is occupied or not.

THere is probably a better way to do this, but it works and is robust. The use of the files and scripts also means that if there is a power cut nothing is lost from memory.

LMK if this sounds like it would help and if you need some examples.

HTH Nick

Posted by nickbrookes76 at Jun 19, 2015 18:55

sorry - forgot to add, initially i had some issues with the ping not responding in time, meaning everything got switched off even when i was in the house. i therefore changed the rules to check if i missed 3 consecutive pings before deciding if i was out or not.

Posted by nickbrookes76 at Jun 19, 2015 18:57

It would be interesting to do the same. But I have a little knowledge to write scripts and shell. It is interesting to see examples, screenshots, for simple scenes. (for example: if I came in the office (my phone), then turn on the lights, etc. Thank you.

Posted by aleksey_z at Jun 19, 2015 19:38

all scripts are linux bash shell scripts.

script to check ip address:

#!/bin/bash
#set up new vars;
new_var=0
var1=nick
var2=192.168.1.20
ddd=$(date +"%Y-%m-%d %H:%M:%S,%3N")
echo "datetime = $ddd"

echo "ipaddress to check is $var2"
status=`ping -qc 1 $var2 | grep -wi '[0-9][[:space:]]received'|awk -F, '{print $2}'|awk '{print $1}'`
echo "$var1 status is :$status";

# if status is ge 1 that means one of the ips responded to a ping.
# therefore value in pingsensor table should be set to 0;

   if [[ $status -gt 0 ]]; then
   echo "ping response received resetting $var1 status to 1"
   echo "1" > /opt/openremote/facts/sensor_$var1
   else
    #write out a 0 in the nicksensor file
    echo "0" > /opt/openremote/facts/sensor_$var1
   fi

ddd=$(date +"%Y-%m-%d %H:%M:%S,%3N")
echo "datetime = $ddd"
echo "**********************************************************"

script to aggregate responses (more than one IP address to be checked). I even have one for the baby-sitter's phone!

#!/bin/bash
#set up new vars;
run_tot=0
new_var=0

ddd=$(date +"%Y-%m-%d %H:%M:%S,%3N")
echo "datetime = $ddd"
exec < /opt/openremote/facts/pingsensor
read old_val
echo "old_val is $old_val"
exec < /opt/openremote/facts/sensor_sarah
read sarah_val
echo "sarah-val is $sarah_val"
exec < /opt/openremote/facts/sensor_extra
read extra_val
echo "extra_val is $extra_val"
exec < /opt/openremote/facts/sensor_note
read note_val
echo "note_val is $note_val"

run_tot=$(($sarah_val + $extra_val + $note_val));
echo "run_tot is $run_tot"
if [[ $run_tot -gt 0 ]]; then
 echo "ping response received resetting pingcycle to 0"
 echo "0" > /opt/openremote/facts/pingsensor
 else
 new_val=$(($old_val + 1 ));
 #write out a 1 in the nicksensor file
 echo "$new_val" > /opt/openremote/facts/pingsensor
fi

exec < /opt/openremote/facts/pingsensor
read msp0
echo "intervals since last ping response is : $msp0"

ddd=$(date +"%Y-%m-%d %H:%M:%S,%3N")
echo "datetime = $ddd"

echo "**********************************************************"

script to run above scripts

/opt/openremote/scripts/sensor_agg > /opt/openremote/logs/dev/sensor_agg.log &
/opt/openremote/scripts/sensor_note > /opt/openremote/logs/dev/sensor_note.log &
/opt/openremote/scripts/sensor_sarah > /opt/openremote/logs/dev/sensor_sarah.log &
/opt/openremote/scripts/sensor_extra > /opt/openremote/logs/dev/sensor_extra.log &

sleep 5

/opt/openremote/scripts/sensor_agg >> /opt/openremote/logs/dev/sensor_agg.log &
/opt/openremote/scripts/sensor_note >> /opt/openremote/logs/dev/sensor_note.log &
/opt/openremote/scripts/sensor_sarah >> /opt/openremote/logs/dev/sensor_sarah.log &
/opt/openremote/scripts/sensor_extra >> /opt/openremote/logs/dev/sensor_extra.log &
...

(script repeats every 5 seconds)

cron entry to run scripts

  • * * * * /opt/openremote/scripts/sensor_all >> /opt/openremote/logs/dev/sensor_all.log
Posted by nickbrookes76 at Jun 19, 2015 19:57

you will need to ensure your phone / whatever you are sensing always gets the same ip address or this won't work...

sample of sensor used to read in result of ping scripts

the file pingsensor just contains a 1 or 0 indicating if the house is occupied or not.

sample rule to turn lights on if noone is home.

package org.openremote.controller.model.event
global org.openremote.controller.statuscache.CommandFacade execute;
global org.openremote.controller.statuscache.SwitchFacade switches;
global org.openremote.controller.statuscache.LevelFacade levels;
global org.openremote.controller.statuscache.RangeFacade ranges;
import org.openremote.controller.protocol.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.Random;


//----------------------------------------------------------------------------------
//- holiday section
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//- 20ish minutes until sunset - turn living room light on.
//----------------------------------------------------------------------------------------
rule "h1"
when
Switch( source=="vacation_sensor", value =="on" )
Switch(source =="bypass2_sensor", value =="off" )
 CustomState( source=="sensor_minutesUntilSunset", value == "30") then
 execute.command("a1_on");
end

note bypass2_sensor is used to override this functionality


package org.openremote.controller.model.event
global org.openremote.controller.statuscache.CommandFacade execute;
global org.openremote.controller.statuscache.SwitchFacade switches;
global org.openremote.controller.statuscache.LevelFacade levels;
global org.openremote.controller.statuscache.RangeFacade ranges;
import org.openremote.controller.protocol.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.Random;

// going out
rule "go"
when
   Range( source=="person_sensor2", value > 3 )
   Switch( source=="bypass2_sensor", value=="off" )
   Switch( source=="vacation_sensor", value =="off" )
then
 execute.command("arm");
 execute.command("vacation_on");
 execute.command ("put_slider2", 5);
 execute.command("a1_off");
 execute.command("a2_off");
 execute.command("tv_plug_off");
 execute.command("a14_off");
 execute.command("a15_off");
 execute.command("a16_off");
 execute.command("c1_off");
 execute.command("d1_off");
 execute.command("d16_off");
 execute.command("e1_off");
 execute.command("g16_off");
 execute.command("g15_off");
 execute.command("h15_off");
 execute.command("h16_off");
 execute.command("f1_off");
 execute.command("g1_off");
end;
Posted by nickbrookes76 at Jun 19, 2015 20:05

Thank you for examples. It looks pretty hard for me. I'm just starting to learn the logic and operation of the "Rules". http://www.openremote.org/pages/viewpage.action?pageId=23597123
And yet not quite figured out the logic of the virtual switch. But gradually I learn.

I use: Synology DS3615; OpenRemote 2.1.1 + AeonLabs Z-Wave Stick 2.
How and where to write, and how to call the scripts? In which folders are saved scripts? How can I use your example to make a simple scene (to my understanding):

When:
I attend (person_sensor (ping?) ON))
Time (9-18) every day (except sundays)
Then:
set dimmer level 60

Posted by aleksey_z at Jun 20, 2015 09:52

sorry i can't help as i use a raspberry pi running rasbian, and i've no experience with your OS. Someone will be along shortly to help.

Best of luck

Posted by nickbrookes76 at Jun 20, 2015 10:22

I want to try to understand your way.
Following your example, I have to create a folder /scripts and /facts in a folder /volume1/@appstore/OpenRemote.
Then, in a folder /volume1/@appstore/OpenRemote/scripts I have to put the scripts, mentioned above (you examples), in the folder /volume1/@appstore/OpenRemote/logs/dev I have to create log-files (as in the you example above). What should be in the folder /volume1/@appstore/OpenRemote/facts? What permissions should have created folders and scripts? What purpose are sensor_note, sensor_sarah, sensor_extra?
If you can, please help me understand.

Posted by aleksey_z at Jun 29, 2015 15:35

Aleksey,

The /scripts folder should contain the scripts, and the /facts folder should contain all of the files that are used to store the values written out by scripts (apart from the logs). In each script, you should change the file path for the file being written to, to your /volume1/@appstore/OpenRemote/facts folder. i think from my example these paths were originally /opt/openremote/facts/
the scripts should have executable bit set (chmod +x name_of_script or chmod 774 name_of_script). The logs folder and the facts folder should have permission for the scripts to write to. so if you run the scripts as nick, nick should have permission to write to these folders...

HTH, let me know if you have any questions.

Nick

Posted by nickbrookes76 at Jun 29, 2015 15:47

Thanks for the answer, but I have little knowledge.
I start trying.
With WinSCP I created a folder in the /volume1/@appstore/OpenRemote/.

In the Putty I must do?
> chown -R openremote.users /volume1/@appstore/OpenRemote/scripts
> chown -R openremote.users /volume1/@appstore/OpenRemote/facts

After > chown

In the test folder on the my PC I copy and changed your script, put my data.
I called him "checkmyphoneip".

As it was called correctly in order to avoid confusion in the future?
I understand that this script will record values in the folder /volume1/@appstore/OpenRemote/facts will have to appear sensor_$var1, Ie sensor_alex

How to do next, what scripts? What to do with the second script sensor_agg? I can not understand the purpose of sensor_note, sensor_sarah, sensor_extra?

Posted by aleksey_z at Jun 29, 2015 16:14

Hi.
Help me to understand.
I wrote three scripts (sensor_android, sensor_ipad, sensor_ipod) - for android, Ipad, iPod.

I wrote the script - sensor_agg
I wrote the script - sensor_all

I put these scripts into a folder /volume1/@appstore/OpenRemote/scripts
and
chmod +x name_of_script
for them.

How and where to write this entry?

cron entry to run scripts

* * * * /volume1/@appstore/OpenRemote/scripts/sensor_all >> /volume1/@appstore/OpenRemote/logs/dev/sensor_all.log
Posted by aleksey_z at Jul 07, 2015 08:21

Ok if you're only monitoring one device (ip) then you don't need to run sensor aggressive as this just aggregates multiple sensors. The other sensor_ scripts are for checking others devices. You need to run sensor_all to continually kick off the sensor_all script at x second intervals. Remove the other scripts from sensor_all that you don't need. The reason for sensor_all rather than con is that con cannot run scripts several times per minute without messing around. You will need to add a con job to kick off the sensor_all script every x minutes. The permissions for the scripts should be chmod 774

Posted by nickbrookes76 at Jul 07, 2015 08:23

I want to try to monitor the three devices.
What I wrote above, is that correct?

The reason for sensor_all rather than con is that con cannot run scripts several
times per minute without messing around.
You will need to add a con job to kick off the sensor_all script every x minutes.
The permissions for the scripts should be chmod 774

Where is it you need to add?

* * * * /volume1/@appstore/OpenRemote/scripts/sensor_all >> /volume1/@appstore/OpenRemote/logs/dev/sensor_all.log
Posted by aleksey_z at Jul 07, 2015 08:43

The last entry must be added to this table?

or this?

Posted by aleksey_z at Jul 07, 2015 09:15

side note @Aleksey

If you refer tot text, you can use {quote}..{quote} to get layout like this

Posted by pz1 at Jul 07, 2015 09:37

Insert replica understood, thanks.
I read your posts in the topic http://forum.synology.com/enu/viewtopic.php?t=68438#p265203

Tell me, how do I add an entry in cron?

Posted by aleksey_z at Jul 07, 2015 09:55

I has been a while. I did use a terminal connection to the Synobox using Putty, and edited crontab with editor vi. So after login as admin:

cd /etc
vi crontab

Not a nice editor, but afaik the only one standard in Synobox. What I do remember is that you have to type all the commands. Don't copy&paste things

So here my toplines (click to enlarge)

Hope this works for you

Posted by pz1 at Jul 07, 2015 10:12

So after login as admin:

As admin or root?

I have to add an entry

*/1 * * * /volume1/@appstore/OpenRemote/scripts/sensor_all >> /volume1/@appstore/OpenRemote/logs/dev/sensor_all.log

which will be executed every minute?

Posted by aleksey_z at Jul 07, 2015 10:25

I think I did it as admin

*/1 Indeed means once every minute

Posted by pz1 at Jul 07, 2015 10:34

NICK BROOKES SAYS:

The /scripts folder should contain the scripts, and the /facts folder should contain all of the files that are used to store the values written out by scripts (apart from the logs). In each script, you should change the file path for the file being written to, to your /volume1/@appstore/OpenRemote/facts folder. i think from my example these paths were originally /opt/openremote/facts/
the scripts should have executable bit set (chmod +x name_of_script or chmod 774 name_of_script). The logs folder and the facts folder should have permission for the scripts to write to. so if you run the scripts as nick, nick should have permission to write to these folders...

I'm confused with the owners and rights.
I have a folders owner openremote
What the owner should be in allocated scripts, owner root
and folder /facts owner openremote

Posted by aleksey_z at Jul 07, 2015 11:00

I found a way to edit crontab. Will work? http://forum.synology.com/enu/viewtopic.php?f=27&t=91830

Posted by aleksey_z at Jul 07, 2015 11:34

It script "sensor_all.sh" does not work of the cron.
I installed a utility for editing cron.

From WinSCP script "sensor_all.sh" starts, but with this error,
but the rest of the scripts are triggered and write new values to the relevant files.

P.S. Ups.. If I use the built-in scheduler,

when you click on the Run, the script triggered and writes in the sensor_all.log: (earlier in the sensor_all.log did not write anything at all.)

P.S.S. Ups

I was able to help VI and hands, I enter the desired entry.

*/1 * * * /volume1/@appstore/OpenRemote/scripts/sensor_all >> /volume1/@appstore/OpenRemote/logs/dev/sensor_all.log 2>@1

The script triggered once a minute.
But these messages are written sensor_all.log:
of how to deal with it?

sh: 0: unknown operand
sh: 0: unknown operand
grep: invalid option -- 'w'
BusyBox v1.16.1 (2015-01-07 14:56:36 CST) multi-call binary.

Usage: grep [-HhnlLoqvsriFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...

Search for PATTERN in FILEs (or stdin)

Options:
	-H	Add 'filename:' prefix
	-h	Do not add 'filename:' prefix
	-n	Add 'line_no:' prefix
	-l	Show only names of files that match
	-L	Show only names of files that don't match
	-c	Show only count of matching lines
	-o	Show only the matching part of line
	-q	Quiet. Return 0 if PATTERN is found, 1 otherwise
	-v	Select non-matching lines
	-s	Suppress open and read errors
	-r	Recurse
	-i	Ignore case
	-F	PATTERN is a literal (not regexp)
	-E	PATTERN is an extended regexp
	-m N	Match up to N times per file
	-A N	Print N lines of trailing context
	-B N	Print N lines of leading context
	-C N	Same as '-A N -B N'
	-e PTRN	Pattern to match
	-f FILE	Read pattern from file

grep: invalid option -- 'w'
BusyBox v1.16.1 (2015-01-07 14:56:36 CST) multi-call binary.

Usage: grep [-HhnlLoqvsriFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...

Search for PATTERN in FILEs (or stdin)

Options:
	-H	Add 'filename:' prefix
	-h	Do not add 'filename:' prefix
	-n	Add 'line_no:' prefix
	-l	Show only names of files that match
	-L	Show only names of files that don't match
	-c	Show only count of matching lines
	-o	Show only the matching part of line
	-q	Quiet. Return 0 if PATTERN is found, 1 otherwise
	-v	Select non-matching lines
	-s	Suppress open and read errors
	-r	Recurse
	-i	Ignore case
	-F	PATTERN is a literal (not regexp)
	-E	PATTERN is an extended regexp
	-m N	Match up to N times per file
	-A N	Print N lines of trailing context
	-B N	Print N lines of leading context
	-C N	Same as '-A N -B N'
	-e PTRN	Pattern to match
	-f FILE	Read pattern from file

grep: invalid option -- 'w'
BusyBox v1.16.1 (2015-01-07 14:56:36 CST) multi-call binary.

Usage: grep [-HhnlLoqvsriFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...

Search for PATTERN in FILEs (or stdin)

Options:
	-H	Add 'filename:' prefix
	-h	Do not add 'filename:' prefix
	-n	Add 'line_no:' prefix
	-l	Show only names of files that match
	-L	Show only names of files that don't match
	-c	Show only count of matching lines
	-o	Show only the matching part of line
	-q	Quiet. Return 0 if PATTERN is found, 1 otherwise
	-v	Select non-matching lines
	-s	Suppress open and read errors
	-r	Recurse
	-i	Ignore case
	-F	PATTERN is a literal (not regexp)
	-E	PATTERN is an extended regexp
	-m N	Match up to N times per file
	-A N	Print N lines of trailing context
	-B N	Print N lines of leading context
	-C N	Same as '-A N -B N'
	-e PTRN	Pattern to match
	-f FILE	Read pattern from file

sh: 0: unknown operand

If I remove the record

2>@1

in sensor_all.log 0B

Now, questions about the files they look like this: it seems that something is wrong.

in /facts: sensor_android, ipad, ipod pingsensor and increases.

in logs: sensor_android, ipad, ipod

sensor_agg and increases.

NICK BROOKES SAYS:

you will need to ensure your phone / whatever you are sensing always gets the same ip address or this won't work...

sample of sensor used to read in result of ping scripts

the file pingsensor just contains a 1 or 0 indicating if the house is occupied or not.

sample rule to turn lights on if noone is home.

Posted by aleksey_z at Jul 08, 2015 11:20

Hi.
I got work to all the scripts.
But I had to be replaced in the script, which checks the ip address in the "status =" letter "w", on the letter "v."

#!/bin/bash
#set up new vars;
new_var=0
var1=nick
var2=192.168.1.20
ddd=$(date +"%Y-%m-%d %H:%M:%S,%3N")
echo "datetime = $ddd"

echo "ipaddress to check is $var2"
status=`ping -qc 1 $var2 | grep -wi '[0-9][[:space:]]received'|awk -F, '{print $2}'|awk '{print $1}'`
echo "$var1 status is :$status";

# if status is ge 1 that means one of the ips responded to a ping.
# therefore value in pingsensor table should be set to 0;

   if [[ $status -gt 0 ]]; then
   echo "ping response received resetting $var1 status to 1"
   echo "1" > /opt/openremote/facts/sensor_$var1
   else
    #write out a 0 in the nicksensor file
    echo "0" > /opt/openremote/facts/sensor_$var1
   fi

ddd=$(date +"%Y-%m-%d %H:%M:%S,%3N")
echo "datetime = $ddd"
echo "**********************************************************"
status=`ping -qc 1 $var2 | grep -wi '[0-9][[:space:]]received'|awk -F, '{print $2}'|awk '{print $1}'`

in my script

status=`ping -qc 1 $var2 | grep -vi '[0-9][[:space:]]received'|awk -F, '{print $2}'|awk '{print $1}'`

In crontab:

*/1 * * * /volume1/@appstore/OpenRemote/scripts/sensor_all >> /volume1/@appstore/OpenRemote/logs/dev/sensor_all.log 2>@1

in sensor_all.log = 0B

Script sensor_all (Correct if wrong)

Now I see in the log such records:

sensor_android, ipad, ipod or "0", if WiFi connection on device - off

sensor_agg

in /facts: sensor_android, ipad, ipod:

0 or 1

pingsensor

0 or 1

But, if no ping from devices, the value in the file "pingsensor" increases.

Range( source=="person_sensor2", value > 3 )

If I create RangeSensor, which i need indicate the range: 0-500000, if I'm not in the office for a long time? Correct if wrong.

P.S. I noticed when the devices Apple (Ipad and Ipod Touch) goes into sleep mode (screen goes blank), then they stop responding to pings. But Android smart phone Sony - no.

P.S.S.

NICK BROOKES SAYS:
let me know if you have any questions.

I am trying to understand further.
I added Shell commands and sensors in OpenRemote. Deduced the to the screen, here's what happened:

If all devices are not present (I disabled wi-fi on devices), it looks like

if at least one (or several) device is present (I included wi-fi on devices), it looks like

This is normal, everything is working properly?

Posted by aleksey_z at Jul 09, 2015 11:30
Document generated by Confluence on Jun 05, 2016 09:35