I modified this device handler > https://github.com/KristopherKubicki/device-type.http-ping
I needed to change line 136 to make it work on SmartThings Hub V2.
I also changed it from contact sensor to switch. And modified it look more modern (as SmartThings developer docs recommend).
This is what I’m using:
[code]/**
*/
preferences {
input(“dest_ip”, “text”, title: “IP”, description: “The device IP you wish to ping”)
input(“dest_port”, “number”, title: “Port”, description: “The port you wish to connect to to emulate a ping”)
}
metadata {
definition (name: “Ping Switch”, namespace: “poisike”, author: “poisike”) {
capability "Polling"
capability "switch"
capability “Refresh”
attribute "ttl", "string"
attribute "last_request", "number"
attribute "last_live", "number"
}
simulator {
// TODO: define status and reply messages here
}
tiles (scale: 2) {
standardTile("switch", "device.switch", width: 6, height: 4, canChangeIcon: true) {
state "on", label: 'ON', backgroundColor: "#79b821", icon:"st.Electronics.electronics15"
state "off", label: 'OFF', backgroundColor: "#ffffff", icon:"st.Electronics.electronics15"
}
standardTile("refresh", "device.ttl", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "default", action:"polling.poll", icon:"st.secondary.refresh"
}
standardTile("ttl", "device.ttl", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
state "ttl", label:'${currentValue}'
}
main "switch"
details(["switch", "refresh", "ttl"])
}
// TODO: define your main and details tiles here
}
def parse(String description) {
log.debug "Parsing ‘${description}’"
def map = stringToMap(description)
// def headerString = new String(map.headers.decodeBase64())
// def bodyString = new String(map.body.decodeBase64())
// log.debug “Parsing ‘${headerString}’”
// log.debug “Parsing ‘${bodyString}’”
def c = new GregorianCalendar()
sendEvent(name: 'switch', value: "on")
sendEvent(name: 'last_live', value: c.time.time)
def ping = ttl()
sendEvent(name: 'ttl', value: ping)
log.debug “Pinging ${device.deviceNetworkId}: ${ping}”
}
private ttl() {
def last_request = device.latestValue(“last_request”)
if(!last_request) {
last_request = 0
}
def last_alive = device.latestValue(“last_live”)
if(!last_alive) {
last_alive = 0
}
def last_status = device.latestValue(“status”)
def c = new GregorianCalendar()
def ttl = c.time.time - last_request
if(ttl > 10000 || last_status == "Down") {
ttl = c.time.time - last_alive
}
def units = "ms"
if(ttl > 10*52*7*24*60*60*1000) {
return "Never"
}
else if(ttl > 52*7*24*60*60*1000) {
ttl = ttl / (52*7*24*60*60*1000)
units = "y"
}
else if(ttl > 7*24*60*60*1000) {
ttl = ttl / (7*24*60*60*1000)
units = "w"
}
else if(ttl > 24*60*60*1000) {
ttl = ttl / (24*60*60*1000)
units = "d"
}
else if(ttl > 60*60*1000) {
ttl = ttl / (60*60*1000)
units = "h"
}
else if(ttl > 60*1000) {
ttl = ttl / (60*1000)
units = "m"
}
else if(ttl > 1000) {
ttl = ttl / 1000
units = "s"
}
def ttl_int = ttl.intValue()
"${ttl_int} ${units}"
}
// handle commands
def poll() {
def hosthex = convertIPToHex(dest_ip)
def porthex = Long.toHexString(dest_port)
if (porthex.length() < 4) { porthex = "00" + porthex }
device.deviceNetworkId = "$hosthex:$porthex"
// log.debug “The DNI configured is $device.deviceNetworkId”
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: "/"
)
def last_request = device.latestValue("last_request")
def last_live = device.latestValue("last_live")
if(!last_request) {
last_request = 0
}
if(!last_live) {
last_live = 0
}
def c = new GregorianCalendar()
if(last_live < last_request) {
sendEvent(name: 'switch', value: "off")
sendEvent(name: 'ttl', value: ttl())
}
sendEvent(name: 'last_request', value: c.time.time)
// log.debug hubAction
hubAction
}
private Long convertIntToLong(ipAddress) {
long result = 0
def parts = ipAddress.split("\.")
for (int i = 3; i >= 0; i–) {
result |= (Long.parseLong(parts[3 - i]) << (i * 8));
}
return result & 0xFFFFFFFF;
}
private String convertIPToHex(ipAddress) {
return Long.toHexString(convertIntToLong(ipAddress));
}[/code]
In Ping switch settings enter Your TV IP and for J series TV, enter port 8000. You can also edit the device name to Samsung TV or what ever You like.
You also need to install Pollster Smartapp, set it to poll the Ping switch device and set the desired poll time there. "Pollster" - A SmartThings Polling Daemon
This works for me, but like I already wrote, I’m new to SmartThings and Groovy, so I don’t take any responsibility if the device handler causes any problems. All credit goes to the original developer.
Hope this helps.