NEWS
Weiterverarbeiten der Stati des Ping Adapters
-
Hi,
ich möchte verschiedene Ip-Adressen bzw. deren Stati via Script auswerten und in Abhängigkeit des Status eine Variable setzen. Das ganze wollte ich mit folgendem Script machen, doch unabhängig vom hinterlegten Status (true/false) des Ping-Adapters im Objekte Baum wird die Variable immer auf false gesetzt.
Vielleicht kann mir einer sagen wo mein Fehler ist.
Danke Peoples
createState('Netzwerk.GesamtStatus', false, { type: 'boolean', name: 'Netzwerkstatus', desc: 'Gesamtstatus Netzwerk' }); var idState = 'javascript.0.Netzwerk.GesamtStatus'; // auf richtige Instanz achten schedule("* * * * *", function () { if(("ping.0.ioBroker-RasPi.192_168_1_1" === true) && ("ping.0.ioBroker-RasPi.192_168_1_2" === true) && ("ping.0.ioBroker-RasPi.192_168_1_3" === true) ){ setState(idState, true); log ("a"); } else{ setState(idState, false); log ("b"); } }); -
if(("ping.0.ioBroker-RasPi.192_168_1_1" === true) && ("ping.0.ioBroker-RasPi.192_168_1_2" === true) && ("ping.0.ioBroker-RasPi.192_168_1_3" === true) ) {–>
if(getState("ping.0.ioBroker-RasPi.192_168_1_1").val === true && getState("ping.0.ioBroker-RasPi.192_168_1_2").val === true && getState("ping.0.ioBroker-RasPi.192_168_1_3").val === true) { -
Da der Ping-Adapter ohnehin zyklisch arbeitet, sollte man nicht mit einem weiteren Zyklus (schedule) abfragen, sondern besser auf Änderung triggern:
var idState = 'javascript.0.Netzwerk.GesamtStatus'; // auf richtige Instanz achten var ping1 = getState("ping.0.ioBroker-RasPi.192_168_1_1").val; var ping2 = getState("ping.0.ioBroker-RasPi.192_168_1_2").val; var ping3 = getState("ping.0.ioBroker-RasPi.192_168_1_3").val; function check() { if(ping1 && ping2 && ping3) { setState(idState, true); log ("a"); } else { setState(idState, false); log ("b"); } } check(); // script start on("ping.0.ioBroker-RasPi.192_168_1_1", function(dp) { ping1 = dp.state.val; check(); }); on("ping.0.ioBroker-RasPi.192_168_1_2", function(dp) { ping2 = dp.state.val; check(); }); on("ping.0.ioBroker-RasPi.192_168_1_3", function(dp) { ping3 = dp.state.val; check(); }); -
Perfekt! Danke!
Eine weitere Frage habe ich jedoch dazu noch:
Kann man auch die "Inhalte / Unterpunkte" eines "Channels" sprich die einzelnen Einträge von "ping.0.ioBroker-RasPi" auslesen?
So könnte ich das dann in einer foreach - Schleife automatisieren und bräuchte nicht bei jedem neuen Eintrag im Ping-Adapter das Script anpassen.
-
Das müsste mit einem Selektor gehen:
var idState = 'javascript.0.Netzwerk.GesamtStatus'; // auf richtige Instanz achten schedule("* * * * *", function () { var allpings = true; $('channel[id=ping.0.ioBroker-RasPi*]').each(function(id, i) { if(!getState(id).val) allpings = false; log(id + ': ' + allpings); }); setState(idState, allpings); });
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login