Scripts that access OS commands

Hello,
I’m at a bit of an impasse. Like so many people I want to perform presence detection and after reading a variety of different solutions, the one I’d like to use is to query my router for the presence of my phone. I don’t have a router that can run scripts like some have, but I can easily write a simple script which uses curl to check for connected devices and see if one of them is my iPhone.

The question now becomes, how do I integrate this with Openhab? I love rules and have several of them that do neat things, is there a way to access data outside Openhab?

Thanks
-Bob

  • Platform information:
    • Hardware: CPUArchitecture/RAM/storage
    • OS: what OS is used and which version
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version:
  • Issue of the topic: please be detailed explaining your issue
  • Please post configurations (if applicable):
    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

You can assign a fixed IP to your phone with your router settings and then install the network binding.

https://docs.openhab.org/addons/bindings/network/readme.html

Could you reformat your post so that the text is not in code fences, please?

Thanks

The exec binding will allow you to make calls to the system console and access the system “outside” openHAB.

https://docs.openhab.org/addons/bindings/exec/readme.html

I used the editor that’s provided with this forum. I have no idea what a code fence is.

-Bob

This normal text

This is text is in a fence (Little icons at the top right of the edit dialog)

Based on what I’ve read, and you can correct me if it’s wrong, but iPhones go to sleep when not being used. I’ve seen many people complain that in order for the phone to respond to ping it must remain awake and the battery dies.

My solution is to run a script that does a simple:

curl http:// | grep iPhone

if ( $? == 0) then
IsHome=true
else
IsHome=false
end

return IsHome

I could use a reserved IP to determine if it’s my phone or a friend’s phone that connected but since I live alone and very few people are allowed onto my private network I don’t think this is necessary.

I looked at exec and it isn’t clear to me how to check a variable. But then I’ve only just started.

Thanks
-Bob

Ok, I still don’t know how fencing happens. I certainly didn’t intend it.

you can check here:

Some tutorial for the exec binding for starters. Maybe it will help you get a fast kickoff.

https://community.openhab.org/t/openhab-exec-binding-explained-in-detail-on-433mhz-radio-transmitter-example

I figured out the problem with “fencing”. I use proper grammar when I write. My paragraphs were properly indented and apparently that’s interpreted as a fence.

-Bob

Thank you to those who responded to my post. I’ve successfully implemented Presence Detection. I’ll post my work in case anyone would like to implement my solution.

I’ve read that many people use the IP address of their iPhone to detect its presence, but others have complained this lead to battery depletion. I’ve also read that some people write scripts they can run on their routers to detect the phone then report that to OpenHab.

My router is provided by my Internet Provider and is one of a variety of Arris routers. I’m not sure of the model and find that immaterial, what is important about it is that the login pages displays up to 15 connected devices. It seems that the most recently connected devices are on the list and those that have been connected longer fall off. This may not be correct but so far it’s the behavior I’ve observed. Since this information is available before login it was obvious to me that I could write a simple curl script to see if the router is aware of my phone. With some pointers from this forum I am able to detect the presence of my phone and act upon it in Openhab2. Here is my configuration:

The Curl Script:

#!/bin/bash
# 
# ishome.sh - Detect presence of iPhone on wireless router
#

curl -s http://172.17.0.1|grep iPhone > /dev/null 2>&1

if [ "$?" = "0" ]; then 
ishome='true'
else
ishome='false'
fi

echo $ishome

The following are the contents of my ishome.things file:

Thing exec:command:ishome [command="/usr/local/bin/ishome.sh", interval=60, timeout=30]

I’ve set the command to run every 60 seconds and to abort if execution is not complete within 30 seconds.

Finally I had to create an item which looks like this:

String IsHome {channel="exec:command:ishome:output"}

This allows that the string IsHome can capture the output of ishome.sh. Since IsHome is a proxy device it can trigger a rule or be queried as part of a rule. To test I created this rule to log when IsHome is changed:

rule "Presence Detection"
when
Item IsHome changed
then
logInfo("ishome.rules", "Presence changed")
end

So far this works very, very well. I’ve performed several tests by disabling wifi on my phone and re-enabling it. So far, it takes about 5 minutes to signal that the phone is no longer present and takes about 1 minute to detect when it is. Since my polling interval is one minute this is a pretty rapid response, I’d have expected it to average 2 minutes for detection and over the long run may be more reasonable, but even still that’s sufficient for my purpose which will be to signal me if my door contacts are triggered when I’m not home.

Cheers
-Bob

I’m not sure why you didn’t just use arpping to detect the iPhone

Why do full network discovery when I can simply query the router?

Because an arpping is lighter than a full curl, but if your solution works, then you’re set. In fact, you dont even need an exec binding, what you’re doing is basically what the HTTP binding already does.