Totally agree with you that this is not a Layer 2 issue but it does seem to me that the DNI has persisted somewhere in the database even though the device has been removed and this is supported by the fact that if I search by the DNI no device shows up.
Anyhow perhaps you right and I am over complicating things so heres my device driver code
metadata {
definition (name: "Tony's Generic Heartbeat V2", namespace: "gashd1889", author: "Tony Leach") {
capability "Configuration"
capability "Switch"
command "Recheck"
attribute "Time", "number"
attribute "Last Checked", "number"
attribute "Server Heartbeat Recieved", "number"
attribute "Server Difference", "number"
}
preferences {
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
input("recheck", "number", title:"Time to Recheck", description:"Time in mins to run the check function", defaultValue:5)
input("alertTime", "number", title:"Alert Time", description:"If no heartbeat is recieved within this time. Time in mins", defaultValue:5)
input("ip", "text", title:"IP Address", description:"IP Address of system sending Heartbeat Signal", required: true)
input("Server", "text", title:"ID In Request Body", description:"ID of system sending Heartbeat Signal", required: true)
}
}
void logsOff(){
log.warn "debug logging disabled..."
device.updateSetting("logEnable",[value:"false",type:"bool"])
}
void updated(){
if (logEnable) runIn(1800,logsOff)
def currentTimeMillis = now()
def iphex = convertIPtoHex(ip)
device.setDeviceNetworkId("${iphex}") // Set the device DNI based on IP set in preferences page
sendEvent(name: "Server Heartbeat Recieved", value: "${currentTimeMillis}", isStateChange: true)
sendEvent(name: "Last Checked", value: "${currentTimeMillis}", isStateChange: true)
sendEvent(name: "Server Difference", value: 0 , isStateChange: true)
}
void parse(String description) {
log.info ("we hit the parse")
log.debug "Received data: ${description}"
def result = parseLanMessage(description)
respData = parseJson(result.body)
def currentTimeMillis = now()
switch (respData.id) { // Only one case at this moment but will expand so I can have multiple heartbearts from the same IP using a parent/Child structure
case Server:
sendEvent(name: "Server Heartbeat Recieved", value: "${currentTimeMillis}", isStateChange: true)
log.debug "We have recieved data from ${Server}"
break
default :
log.warn "unable to set initial values for type:${type}"
break
}
Recheck()
}
List configure() {
log.warn "configure..."
runIn(1800,logsOff)
}
def Recheck () {
def delay = settings.recheck * 60
def currentTimeMillis = now()
sendEvent(name: "Last Checked", value: "${currentTimeMillis}", isStateChange: true)
def lastHeartbeat = device.currentValue("Server Heartbeat Recieved") as Long
def difference = (currentTimeMillis - lastHeartbeat) / 1000
//def difference = (currentTimeMillis - (device.currentValue("Server Heartbeat Recieved")))/1000 // difference in seconds
sendEvent(name: "Server Difference", value: "${difference}", isStateChange: true)
def alert = alertTime*60 // alertTime in mins so convert to secs
if (difference < alert) {
log.debug ("Switch on condition met")
on()
}
else {
log.debug ("Switch off condition met")
off()
}
runIn(delay,Recheck)
}
def installed(){
initialize()
}
def initialize(){
Recheck ()
}
private String convertIPtoHex(String ipAddress) {
return ipAddress.tokenize('.').collect { String.format('%02x', it.toInteger()) }.join().toUpperCase()
}
def on (){
sendEvent(name: "switch", value: "on" , isStateChange: true)
}
def off (){
sendEvent(name: "switch", value: "off" , isStateChange: true)
}
def convertor (string){
double dtimestamp = (Double.parseDouble(string)) // need to convert to double to get it into milliseconds
def long milliseconds = (dtimestamp * 1000) // finally convert into milliseconds
def date = new Date( milliseconds ).toString() // convert milliseconds into a date strin
return date
}
(1) using the wired IP adaptor IP address (192.168.55.130) - doesnt respond when I ping it from the PI
(2) using the wireless adaptor ip Address (192.168.55.140) - Works as expected when the PI sends a heartbeat request