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] Auf Sonnenstand reagieren mit beschreibbarer Variable

    NEWS

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    [Gelöst] Auf Sonnenstand reagieren mit beschreibbarer Variable

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

      Hallo,

      ich habe Scripte laufen wo ich auf den Sonnenstand Elevation reagiere.
      Durch den Neuaufbau meiner Visu möchte ich einiges variabel ausführen.
      Bei den Zeiten funktioniert alles.
      Aber beim Sonnenstand und dessen Scriptaufbau stehe ich etwas auf dem Schlauch.

      Folgendes Script:

      createState('javascript.0.Zeiten.Treppenlicht.Elevation',"-0.5");
      var idt3 = 'javascript.0.Zeiten.Treppenlicht.Elevation';
      var idElevation = getState(idt3).val;
      
      on({
          id: "javascript.0.Global.Sonnenstand.Elevation"/*Global.Sonnenstand.Elevation*/,
          change: "ne",
          valLe: idElevation, 
          oldValGt: idElevation,  
          logic: "and"
      }, function (obj) {
         Treppenlicht_an_Sonnenuntergang();
      });
      

      Keine Ahnung warum es nicht funktioniert.
      Bin auch nur ein bisschen Bewandert mit Javascript.
      Vielleicht kann ja jemand helfen.

      Danke im voraus

      Sebastian

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

        Die Bezeichnung idElevation ist irreführend, denn es ist keine ID, sondern ein Grenzwert. Checke mal den Datentyp der Werte:

        const idt3 = 'javascript.0.Zeiten.Treppenlicht.Elevation';
        const idElevation = "javascript.0.Global.Sonnenstand.Elevation"/*Global.Sonnenstand.Elevation*/;
        
        createState(idt3, -0.5, {type: 'number'});
        
        var gwElevation = getState(idt3).val;
        log('Typ Grenzwert: ' + typeof gwElevation);
        log('Typ Elevation: ' + typeof getState(idElevation).val);
        
        // ...
        
        W 1 Reply Last reply Reply Quote 0
        • W
          webseb79 @paul53 last edited by

          @paul53
          Hallo,

          Grenzwert ist number
          Elevation ist string

          Gruss Sebastian

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

            @webseb79 sagte:

            Elevation ist string

            Das ist das Problem, und solltest Du im Sonnenstands-Skript ändern.

                setState("javascript.0.Global.Sonnenstand.Elevation", Math.round(10 * h) / 10);
                setState("javascript.0.Global.Sonnenstand.Azimut", Math.round(a));
            
            W Homoran 2 Replies Last reply Reply Quote 0
            • W
              webseb79 @paul53 last edited by

              @paul53 sagte in [Hilfe] Auf Sonnenstand reagieren mit beschreibbarer Variable:
              So...
              mein Sonnenstandsscript habe ich nach deinem Vorschlag geändert.
              Die Funktion schaltet jetzt meine Treppenbeleuchtung ein ... Ziel erreicht.
              Aber im Log steht jetzt:

              Wrong type of javascript.0.Global.Sonnenstand.Elevation: "number". Please fix, while deprecated and will not work in next versions.
              

              Was nun?

              Homoran 1 Reply Last reply Reply Quote 0
              • Homoran
                Homoran Global Moderator Administrators @paul53 last edited by

                @paul53
                Darf ich hier mal bitte reingrätschen?

                Aufgrund deines Hinweises habe ich mir "mein" (geklautes) Sonnenstandsskript angeshen.
                Da steht dann:

                      setState(Elevation,h.toFixed(1),true); /*sonne_elevation*/
                      setState(Azimut,a.toFixed(),true);  /*sonne_azimut*/
                

                und kaum dass ich mir das angesehen hatte wurden diese Zeilen auch schon mit WARN angemeckert.
                Leider keine näheren Hinweise

                paul53 1 Reply Last reply Reply Quote 0
                • Homoran
                  Homoran Global Moderator Administrators @webseb79 last edited by

                  @webseb79 sagte in [Hilfe] Auf Sonnenstand reagieren mit beschreibbarer Variable:

                  Was nun?

                  Welchen Typ hat der Datenpunkt unter Objekte?

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

                    @Homoran sagte :

                    Da steht dann:

                    Ja, das war eines meiner ersten Skripte, als ich noch nicht geschnallt hatte, dass toFixed() eine Wandlung zu einem String macht. Ändere es bitte auch in der Doku (s.o), da sonst sicherlich weitere Neulinge darüber stolpern.

                    @webseb79 sagte:

                    Aber im Log steht jetzt:

                    Dann sind die beiden Datenpunkte sicherlich vom Typ "Zeichenkette" (string), was Du in Typ "Zahl" (number) ändern solltest.

                    Homoran W 2 Replies Last reply Reply Quote 0
                    • Homoran
                      Homoran Global Moderator Administrators @paul53 last edited by Homoran

                      @paul53 sagte in [Hilfe] Auf Sonnenstand reagieren mit beschreibbarer Variable:

                      Ändere es bitte auch in der Doku (s.o), da sonst sicherlich weitere Neulinge darüber stolpern.

                      Gerne.

                      Hast du mal das komplette Skript?
                      Nach der Änderung meckert es jetzt noch mehr.

                      14:16:27.777	warn	javascript.0 at Sonnenstand_berechnen (script.js.Wetter_und_Klima.Sonnenstand:30:7)
                      14:16:27.777	warn	javascript.0 at script.js.Wetter_und_Klima.Sonnenstand:38:2
                      14:16:27.777	warn	javascript.0 at Sonnenstand_berechnen (script.js.Wetter_und_Klima.Sonnenstand:31:7)
                      14:16:27.777	warn	javascript.0 at script.js.Wetter_und_Klima.Sonnenstand:38:2
                      

                      EDIT:
                      Mein Fehler. Habe die Zeilen komplett ohne Prüfung übernommen.
                      Der Pfad stimmte so natürlich nicht

                      Homoran paul53 2 Replies Last reply Reply Quote 0
                      • W
                        webseb79 @paul53 last edited by

                        @paul53
                        Jetzt drehe ich mich im Kreis.

                        Also vom Sonnenstands Script beide Variablen in String und meine beschreibbare Variable in String schaltet das Licht ein.
                        Da meckert aber das Sonnenstands Script wegen "Number".
                        Stelle ich auf Number um ist das Sonnenstandsscript in Ordnung aber mein Licht schaltet nicht.
                        Meine beschreibbare Variable auf "Number" geändert und trotzdem kein einschalten

                        Ich verzweifle gerade.

                        Sebastian

                        paul53 1 Reply Last reply Reply Quote 0
                        • Homoran
                          Homoran Global Moderator Administrators @Homoran last edited by

                          @paul53
                          Du hast das Skript in Global?

                          Soll das so sein?

                          W 1 Reply Last reply Reply Quote 0
                          • W
                            webseb79 @Homoran last edited by

                            @Homoran
                            Nein, das ist nur meine Bezeichnung.

                            Homoran 1 Reply Last reply Reply Quote 0
                            • Homoran
                              Homoran Global Moderator Administrators @webseb79 last edited by

                              @webseb79
                              Danke!
                              ich ändere es gerade in der Doku

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

                                @Homoran sagte:

                                Hast du mal das komplette Skript?

                                const suncalc = require('suncalc');
                                const result = getObject("system.adapter.javascript.0");
                                const lat = result.native.latitude;
                                const long = result.native.longitude;
                                const idEle = 'Sonnenstand.Elevation';
                                const idAzi = 'Sonnenstand.Azimut';
                                 
                                createState(idEle, 0, {type: 'number', unit: '°'});
                                createState(idAzi, 0, {type: 'number', unit: '°'});
                                 
                                function Sonnenstand_berechnen () {
                                    var now = new Date();
                                    var sunpos = suncalc.getPosition(now, lat, long);
                                    var h = sunpos.altitude * 180 / Math.PI;
                                    var a = sunpos.azimuth * 180 / Math.PI + 180;
                                    setState(idEle, Math.round(10 * h) / 10, true);
                                    setState(idAzi, Math.round(a), true);
                                }
                                
                                schedule("* * * * *", Sonnenstand_berechnen); // jede Minute
                                
                                Homoran 1 Reply Last reply Reply Quote 0
                                • Homoran
                                  Homoran Global Moderator Administrators @paul53 last edited by

                                  @paul53
                                  Danke!

                                  Das hast du ja mal wieder massiv verschlankt 😉

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

                                    @Homoran sagte:

                                    Das hast du ja mal wieder massiv verschlankt

                                    Nein, nur die Logs entfernt, dafür aber Konstanten als solche deklariert und den Datenpunkttyp "number" mitgegeben.

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

                                      @webseb79 sagte:

                                      Meine beschreibbare Variable auf "Number" geändert und trotzdem kein einschalten

                                      Stimmt der Datentyp ?

                                      log('Typ Grenzwert: ' + typeof gwElevation);
                                      
                                      W 1 Reply Last reply Reply Quote 0
                                      • W
                                        webseb79 @paul53 last edited by

                                        @paul53

                                        Jetzt ist Grenzwert Typ "Number" und Elevation auch Typ "Number"

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

                                          @webseb79 Dann sollte es funktionieren. Wie sieht das komplette Skript jetzt aus ?

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

                                            @paul53

                                            createState('javascript.0.Zeiten.Treppenlicht.Elevation', -0.5 , {type:"number", role:"javascript"});
                                            
                                            var idt3 = 'javascript.0.Zeiten.Treppenlicht.Elevation';
                                            
                                            var idElevation = getState(idt3).val;
                                            
                                            
                                            
                                            on({
                                                id: "javascript.0.Global.Sonnenstand.Elevation"/*Global.Sonnenstand.Elevation*/,
                                                change: "ne",
                                                valLe: idElevation,  
                                                oldValGt: idElevation,   
                                                logic: "and"
                                            }, function (obj) {
                                               Treppenlicht_an_Sonnenuntergang();
                                            });
                                            
                                            function Treppenlicht_an_Sonnenuntergang() {
                                            
                                                if      (getState("radar.0.G-Tag-Claudia.here"/*G-Tag-Claudia.here*/).val === false 
                                                    &&   getState("radar.0.G-Tag-Sebastian.here"/*G-Tag-Sebastian.here*/).val === false ) 
                                                        
                                                        { return; }
                                                
                                                if      (getState("knx.0.Licht.Licht-Status.OG-Schlafzimmer-Treppenleuchte-Schalten-Status"/*OG-Schlafzimmer-Treppenleuchte-Schalten-Status*/).val === 1) 
                                                        
                                                        { return; }
                                                
                                                        event("Schlafzimmer...Treppenbeleuchtung zum Sonnenuntergang eingeschalten","Allgemein","white");
                                                        setState ("knx.0.Licht.Licht-Schalten.OG-Schlafzimmer-Treppenleuchte-Schalten"/*OG-Schlafzimmer-Treppenleuchte-Schalten*/, 1);
                                            }
                                            

                                            Wie schon erwähnt ... Typ String und alles lief.

                                            Sebastian

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

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            717
                                            Online

                                            31.7k
                                            Users

                                            79.8k
                                            Topics

                                            1.3m
                                            Posts

                                            3
                                            31
                                            1396
                                            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