you can achieve the same thing on the Thing configuration itself. You can specify:
how often to ping
how long to wait for a reply
how many times a ping will time out before making it as offline
So you can check every 15 seconds, wait one second for a reply, and have the ping timeout 4 times in a row before it’s marked offline. There is no need to use a proxy item and rule.
The online/offline status of a Thing is whether the binding is working or not. To see if a device is online/offline you need to look at the Item linked to the status channel. It’s perfectly normal for The Thing to be ONLINE and the device be offline. The binding is working.
Unless you set an Item to UNDEF yourself in a rule, Items will only go to UNDEF if a binding determines it cannot connect to the device or something else like that has gone wrong. So you can rely on the UNDEF state to indicate a device has gone offline. Assuming the binding supports that.
Thanks but for example the Hue Binding thing goes Offline if the connection is lost to the Hue Bridge.
The color item does still have the same old states.
So this is why I am want to get the thing status for example.
I relooked at my approach to this and I decided I don’t need a notification for every thing because Spotify things go offline all the time when you are not using them. So I going to trigger a rule for every thing I want to monitor.
I guess the hue binding doesn’t use the standard approach. You will have to create a rule to trigger on the Thing going offline. There is no way to group things (they are not items after all) so you need to trigger the rule with each Thing you care about. There will be a receivedEvent variable I think you can use to figure out which thing went offline.
To put it on your sitemap you’ll have to create an Item and set it’s state from the rules.
import org.eclipse.smarthome.model.script.ScriptServiceUtil
val telegramAction = getActions("telegram","telegram:telegramBot:Telegram_Bot")
rule "Thing Detected off line list question"
when
Thing 'wled:wled:Anabel_NL' changed from ONLINE to OFFLINE or
Item PING_Device changed to OFF
then
telegramAction.sendTelegramQuery("One of your things went OFFLINE in openHAB.\nItem " + triggeringItem + "\n\nDo you want a list of OFFLINE Things?", "Offline_List", "Yes", "No")
end
rule "Get status of thing and if OFFLINE send message to telegram"
when
Item telegramReplyId received update Offline_List
then
val StringBuilder Message = new StringBuilder
val things = ScriptServiceUtil.getInstance.thingRegistry.getAll()
if (telegramMessage.state.toString == "Yes")
{
Message.append( " Your OFFLINE things are; " + "\n" + "\n" )
things.forEach[ thing |
val statusInfo = thing.getStatusInfo()
if (statusInfo.getStatus().toString() != 'ONLINE') {
Message.append( "Thing: " + thing.getLabel() + "\n" )
Message.append( "state: " + statusInfo.getStatus() + "\n" )
Message.append( "detail: " + statusInfo.getStatusDetail() + "\n" )
Message.append( "description: " + statusInfo.getDescription() + "\n" + "\n" )
}
]
telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, Message.toString)
} else {
telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok")
}
end
The result in telegram please forgive typo
Item null is because it was triggered by thing if item triggers it shows up.
Select Yes it checks when you select yes so it is a live list not from when it went offline.
I had the same problem, and I noticed that the rule “changed from ONLINE to OFFLINE” did not work reliably. So I created the following script, to be executed regularly. It gets the online/offline data directly from the bridge and sets +"_Online" to the values “ONLINE” or “OFFLINE”.
Replace <huepw> with your apikey for the Hue Bridge.
HueSetup includes all lamps with the number (according to Bridge), name, type and item prefix.
load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/jsr223/jslib/JSRule.js');
var ThingUID = Java.type("org.eclipse.smarthome.core.thing.ThingUID");
logDebug("HueWorker.js: being loaded");
var HueSetup = [
[1, "k3", 220, "hueKeller3"]
// insert here other lamp descriptions
]
var getHueFromNo=function(id) {
for (var i = 0; i < HueSetup.length; i++) {
if(HueSetup[i][0]==id) return HueSetup[i];
}
return -1;
}
var getHueReachable=function() {
logTrace("HueWorker.js: Getting Hue data");
var requestURL = 'http://philips-hue.local/api/<huepw>/lights';
var HttpUtilNativ = Java.type("org.eclipse.smarthome.io.net.http.HttpUtil");
var results = HttpUtilNativ.executeUrl("GET", requestURL, 5000);
var inJson = JSON.parse(results);
logTrace("HueWorker.js: JSON received from bridge: "+inJson);
var outJson=""
for (x in inJson) {
var hue=getHueFromNo(x);
if(hue[0]!=2) {
var itState=getItem(hue[3]+"_Toggle").state;
var onlineState=getItem(hue[3]+"_Online").state;
var thState=things.get(new ThingUID("hue:0"+hue[2]+":1:"+hue[1])).status;
var jsOnOff=inJson[x]['state']['on'].toString();
var jsOnline=inJson[x]['state']['reachable'].toString();
var thingMatch;
if(jsOnOff=="true") jsOnOff="ON";
else if(jsOnOff=="false") jsOnOff="OFF";
else jsOnOff="UNDEFINED";
if(jsOnline=="true") jsOnline="ONLINE";
else if(jsOnline=="false") jsOnline="OFFLINE";
else jsOnline="UNDEFINED";
// We test if the online/offline status is the same for the thing and the JSON string
if (thState==jsOnline) thingMatch="match ";
else {
if(onlineState==jsOnline) thingMatch="mism(cor)"
else {
thingMatch="mismatch ";
events.postUpdate(hue[3]+"_Online", jsOnline);
logInfo("HueWorker.js: Thing status (ONLINE/OFFLINE) mismatch for item "+hue[3]+". jsOnline="+jsOnline+" with _Online="+getItem(hue[3]+"_Online").state+". Corrected.");
}
}
if(onlineState!=jsOnline) events.postUpdate(hue[3]+"_Online", jsOnline);
var itemMatch;
// We test for different light state, e.g. if the item shows on and the JSON-string shows off
if (itState==jsOnOff)
itemMatch="match ";
else {
if(jsOnline=="ONLINE") {
itemMatch="mismatch";
events.postUpdate(hue[3]+"_Toggle", jsOnOff);
logInfo("HueWorker.js: Item state (ON/OFF) mismatch for item "+hue[3]+". jsOnOff="+jsOnOff+". Corrected.");
}
else itemMatch="mism(th)";
}
if(itState!=jsOnOff) events.postUpdate(hue[3]+"_Toggle", jsOnOff);
logDebug("HueWorker.js: "+String(" "+x).slice(-2)+":"+String(hue[3]+" ").slice(0,18)+
", testing "+"hue:0"+hue[2]+":1:"+String(hue[1]+" ").slice(0,5)+
": ITEM-STATUS:"+String(itState+" ").slice(0,4)+
"; THING-STATUS: "+String(thState+" ").slice(0,7)+
"; JSON: on="+String(jsOnOff+" ").slice(0,3)+
", reachable="+String(jsOnline+" ").slice(0,7)+
"; Thing: "+thingMatch+
", Item: "+itemMatch+
"==>"+onlineState);
outJson+="| ";
outJson+=x+"-"+inJson[x]['state']['on']+"-"+inJson[x]['state']['reachable']+"-"+hue[1];
}
outJson+=" ";
}
outJson+="|";
// outJson shows all Json on/off and on-/offline in one line
logTrace("HueWorker.js: "+outJson);
return;
}
JSRule({
name: "Hue Synchronization",
description: "This rules is a simple example for getting started with JSR223 Javascript",
triggers: [
TimerTrigger("0/15 * * * * ?")//Enable/Disable Rule
],
execute: function( module, input){
getHueReachable();
}
});
logDebug("HueWorker.js: finished loading");
Each Tasmota and Zigbee (via zigbee2mqtt) has “Reachability” status. With Tasmota it’s the LWT topic, and for zigbee2mqtt it’s the “availability” topic.
I have assigned an item for each thing into gAvailability group, so in my sitemap I can just add
Group item=gAvailability label="Availability Status"
I appreciate that you have searched for your answer before asking a question.
The link above is to working code that you can use however I would use @rlkoshak method.
Please let old threads die. Start a new one and you can post links to old threads so people can use them as research to what you are trying to achieve.
If you want good answers please ask a question that will make us understand what it is you are trying to do.
I not saying you haven’t researched it or not to ask the question. Ask the long question like.
I am trying to get a list of all my offline things that will send at the end of each day on telegram? Then provide as much information as you can about everything relevant so you can get a good answer so the answer may include something helpful.
Don’t be afraid of opening a new thread if you are going to ask a question that is three paragraphs or longer.
Hi, looks like your script doesn’t work in OH 3. The method ScriptServiceUtil.getInstance.thingRegistry doesn’t seem exist anymore. Do you find a workaround? Thanks