Problem checking network item state in a rule

Hello friends,

I’ve been digging into openhab2 for a few days now, and I’m enjoying the puzzle of it all, but I’ve run into an issue I’m just not sure how to solve.

I’m trying to set up a switch in my sitemap that, when pushed, will tell me whether my phone is on my network or not.

Relevant items:

Switch iPhone_Max_Phone "Max's iPhone" <network> { channel="network:pingdevice:192_168_1_108:online" }

Switch iPhone_Presence_Check {autoupdate="false"}

Relevant rules:

rule "iPhone present?"
when
	Item iPhone_Presence_Check received command
then
	switch(iPhone_Max_Phone.state)
	{
		case ON:
		sendBroadcastNotification("iPhone ON")
		case OFF:
		sendBroadcastNotification("iPhone OFF")
	}
end

Relevant sitemap:

sitemap actions label="Actions"
{
Switch item=iPhone_Presence_Check label="Check if iPhone is online" mappings=[ON="Test"]
}

The problem I’m experiencing is that when I push the button on the sitemap, no notification appears. Interestingly, if I remove the switch statement (or any check on iPhone_Max_Phone.state such as an if statement), I receive the notification instantly. This makes me think that I’m checking the state incorrectly, but I can’t see how.

Any help would be appreciated.

Android and iOS devices

Because mobile devices put themselves in a deep sleep mode after some inactivity, they do not react to normal ICMP pings. Configure ARP ping to realize presence detection for those devices. This only works if the devices have WIFI enabled, have been configured to use the WIFI network, and have the option “Disable wifi in standby” disabled (default). Use DHCP listen for an almost immediate presence detection for phones and tablets when they (re)join the home Wifi network.

iPhones, iPads

Apple iOS devices are usually in a deep sleep mode and do not respond to ARP pings under all conditions, but to Bonjour service discovery messages (UDP port 5353). Therefore first a Bonjour message is sent, before the ARP presence detection is performed. The binding automatically figures out if the target device is an iOS device. To check if the binding has correctly recognised a device, have a look at the uses_ios_wakeup property of the THING.

Yes, I suppose I could have been more clear, I already have the Network Binding installed and working. In fact, I already have working presence detection which wakes my computer when I join the network.

Read the the Mobile Devices section :wink:

Unlock the iOS Device and than Pull the Switch … I think than you will get iPhone ON

Even when unlocked, it still doesn’t send the notification. But I think my presence detection might be suddenly broken, as I didn’t get the notification that should trigger when my Wake on Lan item is sent a command either… Interesting.

For me Presence Decetion via iOS Devices never work perfektly …
With the iCloud Binding it worked “Okay” for me.
https://www.openhab.org/addons/bindings/icloud/
Least I switch to a Dectection via Bluetooth Tags at my key ring.

Here my Script with the RestAPI:


Here my Script with MQTT:

#!/bin/bash

########################################
# MQTT presence detection using G-Tags #
#                          version 0.2 #
#              copyright by eiGelbGeek #
########################################

#configuration
mqtt_broker_ip="191.168.0.5"
mqtt_broker_port="1883"
mqtt_topic="/mqtt/topic/"
mqtt_items=("GTag_1" "GTag_2" "GTag_3")
gtag_ids=("99:99:99:99:99:99" "88:88:88:88:88:88" "77:77:77:77:77:77")

#From here changes can lead to loss of function!
filename=/tmp/bluetooth_devices.$$
hcitool lescan > $filename & sleep 10
pkill --signal SIGINT hcitool
sleep 1

for ((i=0;i<${#gtag_ids[@]};++i)); do
  searchresult=$(grep -c ${gtag_ids[i]} $filename)
  if [ $searchresult -gt 0 ]; then
      mosquitto_pub -h $mqtt_broker_ip -p $mqtt_broker_port -t $mqtt_topic${mqtt_items[i]}/status -m ON
  else
      mosquitto_pub -h $mqtt_broker_ip -p $mqtt_broker_port -t $mqtt_topic${mqtt_items[i]}/status -m OFF
  fi
done
rm $filename

So this is really strange. Now my iPhone shows online even when turned all the way off. And even if the presence detection is behaving wrongly, shouldn’t either the if or the else trigger?

EDIT: Added a default case to the switch statement to broadcast “Broken”. Still no dice. What the heck is going on? O.o

This works well for detection with iPhone in my home.

Thing:

Thing network:pingdevice:SiPhone [ hostname="10.0.1.22", uses_ios_wakeup=1, uses_arp_pings=1, retry=30, timeout=15000, refreshInterval=60000 ]

Item:

Group:Switch:AND(OFF,ON) gPresent <present>
Switch Present "Phone is home" <present>
Switch Present_Timer { expire="20m, command=OFF" }
Switch MyDevice <network> (gPresent) { channel="network:pingdevice:SiPhone:online" }

Network.cfg:

binding.network:allowSystemPings=true
binding.network:allowDHCPlisten=false
binding.network:arpPingToolPath=arping
binding,network:cacheDeviceStateTimeInMS=2000
uses_arp_pings=yes

Rule:

rule "start gPresent on system start"
when
    System started
then
    Present.sendCommand(OFF) // assume no one is home
end

rule "gPresent updated, at least one change of state"
when
    Item gPresent received update
then
    // someone came home
    if(gPresent.state == ON && Present.state != ON) { 
        Present_Timer.postUpdate(OFF) // cancel the timer if necessary
        Present.sendCommand(ON)
    }

    // no one is home and timer is not yet ticking (otherwise endless loop)
    else if(gPresent.state == OFF && Present.state != OFF && Present_Timer.state != ON) {
        Present_Timer.sendCommand(ON) // start the timer
    }
end

rule "Present_Timer expired"
when
	Item Present_Timer received command OFF
then
	Present.sendCommand(OFF)
end

You can adjust the expire time to suit your needs, or remove it if you like, it’s there for when I walk to neighbors home for a few minutes and do not want another rule to turn something off right away.

I ended up taking the nuclear option: flashed a fresh install of openhabian to the pi, and now everything is working as expected. Thank you both for your ideas, I’ll be bookmarking them for later use!