Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. [gelöst] Elegantere Programmierung?

    NEWS

    • Monatsrückblick - April 2025

    • Minor js-controller 7.0.7 Update in latest repo

    • Save The Date: ioBroker@Smart Living Forum Solingen, 14.06.

    [gelöst] Elegantere Programmierung?

    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      skorpil last edited by skorpil

      Ich habe mir ein Script gebastelt, das funktioniert auch. Was die Zustände "true" und "false" angeht gibt es sicher eine elegantere Lösunng als mit zwei "If" abfragen. Bestimmt mit If - else. Aber ich weiß nicht, wie das geht. Danke für Eure Hilfe.

      // ##################################
      // Deklarationen
      // ##################################
      
      var IDAusloeser ='hm-rega.0.39533'/*Anwesend*/;
      
      
      // ##################################
      // Programm
      // ##################################
      
      on({id: IDAusloeser}, function (data) {
      
      if (data.state.val = true) {
      
          sendTo("pushover.0", {
              message:  'ANwesend', // mandatory - your text message
              title:    'ANWESENHEIT', // optional  - your message's title, otherwise your app's name is used
              sound:    'siren',     // optional  - the name of one of the sounds supported by device clients to override the user's default sound choice
                                        //    pushover, bike, bugle, cashregister, classical, cosmic, falling,
                                        //    gamelan, incoming, intermission, magic, mechanical, pianobar, siren,
                                        //    spacealarm, tugboat, alien, climb, persistent, echo, updown, none
              priority: 1,          // optional
                                        //    -1 to always send as a quiet notification,
                                        //    1 to display as high-priority and bypass the user's quiet hours, or
                                        //    2 to also require confirmation from the user
          });        
      }
      
      if (data.state.val = false) {
      
          sendTo("pushover.0", {
              message:  'ABwesend', // mandatory - your text message
              title:    'ANWESENHEIT', // optional  - your message's title, otherwise your app's name is used
              sound:    'siren',     // optional  - the name of one of the sounds supported by device clients to override the user's default sound choice
                                        //    pushover, bike, bugle, cashregister, classical, cosmic, falling,
                                        //    gamelan, incoming, intermission, magic, mechanical, pianobar, siren,
                                        //    spacealarm, tugboat, alien, climb, persistent, echo, updown, none
              priority: 1,          // optional
                                        //    -1 to always send as a quiet notification,
                                        //    1 to display as high-priority and bypass the user's quiet hours, or
                                        //    2 to also require confirmation from the user
          });        
      }
      
      
      });
      

      In dem Zusammenhang: da ich jetzt weitere Scripte anlegen möchte: kann ich in jedem Script die IDAusloeser als Trigger verwenden? Ich weise in jedem Script dieser Variablen einen neuen Wert zu. Oder muss ich diese Variable pro Script modifizieren, also IDAusloeserScript1, IDAusloeserScript2, usw. Mit anderen Worten: ist der Wert der Variablen script-abhängig oder bleibt der Wert der Variablen erhalten?

      Danke für Hilfe und Aufklärung-

      haus-automatisierung paul53 2 Replies Last reply Reply Quote 0
      • haus-automatisierung
        haus-automatisierung Developer Most Active @skorpil last edited by haus-automatisierung

        @skorpil Ja,

        if (data.state.val) {
        
        } else {
        
        }
        

        Generell ist dein Code aber falsch. Du vergleichst nicht, sondern machst eine Zuweisung. Vergleich gegen true wäre if (data.state.val == true) { bzw. typsicher if (data.state.val === true) {

        Aber... das kann man sich schenken, weil true == true ja wieder true gibt 🙂 Daher nur if (data.state.val) {

        1 Reply Last reply Reply Quote 2
        • paul53
          paul53 @skorpil last edited by paul53

          @skorpil

          on(IDAusloeser, function (data) {
              let msg = 'ABwesend';
              if (data.state.val) msg = 'ANwesend';
              sendTo("pushover.0", {
                    message:  msg, // mandatory - your text message
                    // usw.
          
          haus-automatisierung 1 Reply Last reply Reply Quote 1
          • haus-automatisierung
            haus-automatisierung Developer Most Active @paul53 last edited by haus-automatisierung

            @paul53 Dann halt so 🙂

            on({id: 'hm-rega.0.39533'}, (data) => {
                sendTo('pushover.0', {
                    message: data.state.val ? 'ANwesend' : 'ABwesend',
                    title: 'ANWESENHEIT',
                    sound: 'siren',
                    priority: 1
                });        
            });
            
            paul53 S 2 Replies Last reply Reply Quote 3
            • paul53
              paul53 @haus-automatisierung last edited by

              @haus-automatisierung
              Das ist natürlich noch eleganter👍

              1 Reply Last reply Reply Quote 3
              • S
                skorpil last edited by

                Wahnsinn. Ihr seid ja schneller als der Schall. Vielen herzlichen Dank u. eine schönes Wochende.

                paul53 1 Reply Last reply Reply Quote 0
                • paul53
                  paul53 @skorpil last edited by paul53

                  @skorpil
                  Soll eine Nachricht gesendet werden, wenn sich die Zahl der anwesenden Personen ändert? Ich nehme an, dass dann die SV aktualisiert wird.
                  Falls nur bei "jemand anwesend" und "alle abwesend" gesendet wurden soll, ändere den Trigger auf "Wertänderung" wie in meinem Vorschlag gezeigt.

                  S 1 Reply Last reply Reply Quote 1
                  • S
                    skorpil @paul53 last edited by

                    @paul53 Danke für den Hinweis. Ich habe die SV so eingestellt, dass sie auf abwesend gestellt wird, wenn definitiv keine Person mehr im Haus ist. Und ist nur mindestens einer anwesend, dann wird sie auf anwesend gestellt.

                    paul53 1 Reply Last reply Reply Quote 0
                    • paul53
                      paul53 @skorpil last edited by

                      @skorpil sagte: Ich habe die SV so eingestellt

                      Davon gehe ich aus. Die Frage ist, ob die SV auf "anwesend" aktualisiert wird, wenn sich nur die Zahl der Personen ändert.

                      S 1 Reply Last reply Reply Quote 1
                      • S
                        skorpil @paul53 last edited by

                        @paul53 ich glaube nicht. Muss ich prüfen.

                        1 Reply Last reply Reply Quote 0
                        • S
                          skorpil @haus-automatisierung last edited by

                          @haus-automatisierung sagte in [gelöst] Elegantere Programmierung?:

                          on({id: 'hm-rega.0.39533'}, (data) => { sendTo('pushover.0', { message: data.state.val ? 'ANwesend' : 'ABwesend', title: 'ANWESENHEIT', sound: 'siren', priority: 1 }); }

                          Irgendwo ist noch ein Hund. Er meldet einen Fehler in der letzten Zeile

                          haus-automatisierung 1 Reply Last reply Reply Quote 0
                          • haus-automatisierung
                            haus-automatisierung Developer Most Active @skorpil last edited by

                            @skorpil Hab die Klammer zu vergessen - das kommt, wenn man im Forum Code schreibt 🙂 Jetzt ist korrigiert im Beitrag.

                            S 2 Replies Last reply Reply Quote 1
                            • S
                              skorpil @haus-automatisierung last edited by

                              @haus-automatisierung Danke

                              1 Reply Last reply Reply Quote 0
                              • S
                                skorpil @haus-automatisierung last edited by

                                @haus-automatisierung Frage: da ich gerne Deklarationen verwende, der Übersichtlichkeit halber: was müsste ich noch ändern:

                                wenn ich

                                var IDAusloeser ='hm-rega.0.39533'/Anwesend/;

                                nutze, wie müßze dann diese Zeile aussehen

                                on({id: 'hm-rega.0.39533'}, (data) => {

                                Ich frage, um zu lernen

                                haus-automatisierung 1 Reply Last reply Reply Quote 0
                                • haus-automatisierung
                                  haus-automatisierung Developer Most Active @skorpil last edited by

                                  @skorpil

                                  const IDAusloeser = 'hm-rega.0.39533';
                                  
                                  on({id: IDAusloeser}, (data) => {
                                  
                                  });
                                  
                                  1 Reply Last reply Reply Quote 0
                                  • S
                                    skorpil last edited by

                                    So jetzt passiert genau das, was @paul53 angesprochen hat. Offenbar wird die SV jedesmal, wenn sich die Personenzahl ändert, aktualisiert. Und das führt dann im Ergebnis dazu. daß ich jedesmal eine pushover Nachricht erhalte, wenn sich die Personenzahl ändert. Das ist nicht gewollt.

                                    Ich sollte nur dann eine Nachricht erhalten, wenn die SV auf Abwesend oder Anwesend geschaltet wird, also bei VERÄNDERUNG und nicht bei Aktualisierung. Da kommt nun wohl doch Pauls Vorschlag zum Tragen!

                                    paul53 1 Reply Last reply Reply Quote 0
                                    • paul53
                                      paul53 @skorpil last edited by paul53

                                      @skorpil sagte: Da kommt nun wohl doch Pauls Vorschlag zum Tragen!

                                      Nur, was den Trigger betrifft:

                                      on(IDAusloeser, (data)  => { // triggert bei Wertänderung
                                      
                                      S 1 Reply Last reply Reply Quote 0
                                      • S
                                        skorpil @paul53 last edited by

                                        @paul53 was ich jetzt noch nicht begriffen habe:

                                        wann reagiert die on Anweisung auf Aktualisierung und wann auf Wertveränderung?

                                        paul53 1 Reply Last reply Reply Quote 0
                                        • paul53
                                          paul53 @skorpil last edited by paul53

                                          @skorpil sagte: wann reagiert die on Anweisung auf Aktualisierung und wann auf Wertveränderung?

                                          Wenn das Muster ein ID-String ist, reagiert sie auf Wertänderungen. Wenn das Muster ein Objekt ist, reagiert sie auf Aktualisierung, wobei das durch weitere Attribute eingeschränkt werden kann.
                                          Beispiel: Anstelle der String-ID-Version kann man auch schreiben:

                                          on({id: IDAusloeser, change: 'ne'}, (data) => { // triggert bei Wertänderung
                                          
                                          S 1 Reply Last reply Reply Quote 2
                                          • S
                                            skorpil @paul53 last edited by

                                            @paul53 Dankeschön. Letze Frage: wie erkenne ich ein Objekt und wie den String? Es ist verwirrend.. ich bin auf diese Unterscheidung schon x mal reingefallen und verstehe es nicht richtig. Vlt. bin ich zu doof.

                                            paul53 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            997
                                            Online

                                            31.6k
                                            Users

                                            79.4k
                                            Topics

                                            1.3m
                                            Posts

                                            10
                                            325
                                            34145
                                            Loading More Posts
                                            • Oldest to Newest
                                            • Newest to Oldest
                                            • Most Votes
                                            Reply
                                            • Reply as topic
                                            Log in to reply
                                            Community
                                            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                                            The ioBroker Community 2014-2023
                                            logo