Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Entwicklung
    4. State auslesen in Adapterentwicklung

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    State auslesen in Adapterentwicklung

    This topic has been deleted. Only users with topic management privileges can see it.
    • J.A.R.V.I.S.
      J.A.R.V.I.S. Developer @reutli last edited by J.A.R.V.I.S.

      @reutli

      Versuch eines der folgenden. Das was du oben versuchst funktioniert nicht, das ist vom JS Adapter.

      Await/Async

      const state = await this.getStateAsync('Dein Dp');
      

      oder

      Callback

      this.getState('Dein Dp', (error, state) => {
      
      });
      

      Bin mir beim unteren gerade nicht sicher, welche Werte alle übergeben werden

      R 1 Reply Last reply Reply Quote 0
      • R
        reutli @Schnup89 last edited by

        @Schnup89

        Das habe ich auch bereits versucht.... dann bekomme ich zwar keinen Fehler, aber auch keinen Wert (time ist dann undefined):

        (71874) Variablen für Funktionsaufruf : command: Pause time: undefined mid: 6dcad25a-39f7-4baf-9f44-877302c35c1f
        
        1 Reply Last reply Reply Quote 0
        • R
          reutli @J.A.R.V.I.S. last edited by

          @J-A-R-V-I-S sagte in State auslesen in Adapterentwicklung:

          Versuch eines der folgenden. Das was du oben versuchst funktioniert nicht, das ist vom JS Adapter.
          Await/Async
          const state = await this.getStateAsync('Dein Dp');

          Dann müsste ich eine komplette ASYNC Funktion aufbauen, weil ich ja in der 'OnStateChange' keinen await aufrufen kann?!

          J.A.R.V.I.S. 1 Reply Last reply Reply Quote 0
          • J.A.R.V.I.S.
            J.A.R.V.I.S. Developer @reutli last edited by

            @reutli du kannst die einfach zu einer Async machen, indem du async davor schreibst.

            async onStateChange(id, state)
            
            R 1 Reply Last reply Reply Quote 0
            • R
              reutli @J.A.R.V.I.S. last edited by

              @J-A-R-V-I-S

              Hi,

              nope leider nicht.
              Erhalte mit '.val' einen undefined bei 'time' (und ohne '.val' natürlich ein Object):

              (72098) Variablen für Funktionsaufruf : command: Pause time: undefined mid: 6dcad25a-39f7-4baf-9f44-877302c35c1f
              

              hier der code:

              	async onStateChange(id, state) {
              		if (state) {
              			let mid = id.split('.')[2];
              			let trigger = id.split('.').pop();
              			this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
              
              			if (trigger == 'command') { // wenn Befehl an Mower verändert wurde
              				const command = state.val;
              				const time = await this.getStateAsync('husqvarna_mowers.0.6dcad25a-39f7-4baf-9f44-877302c35c1f.Control.duration').val;
              				this.log.info('Variablen für Funktionsaufruf : command: ' + command + ' time: ' + time + ' mid: ' + mid);
              				//this.await.sendCommand(command, time, mid);
              			}
              		} else {
              			// The state was deleted
              			this.log.info(`state ${id} deleted`);
              		}
              	}
              
              J.A.R.V.I.S. 1 Reply Last reply Reply Quote 0
              • J.A.R.V.I.S.
                J.A.R.V.I.S. Developer @reutli last edited by J.A.R.V.I.S.

                @reutli lass dir mal den ganzen State ausgeben, ohne .val.

                this.log.info(JSON.stringify(time));
                

                Und mach mal einen try/catch Block drum. Damit fängst du die möglichen Fehler ab.

                R 1 Reply Last reply Reply Quote 0
                • R
                  reutli @J.A.R.V.I.S. last edited by

                  @J-A-R-V-I-S sagte in State auslesen in Adapterentwicklung:

                  this.log.info(JSON.stringify(time));

                  jetzt bekomme ich Werte zurück:

                  (72271) {"val":"24","ack":true,"ts":1587366996004,"q":0,"from":"system.adapter.husqvarna_mowers.0","user":"system.user.admin","lc":1587295118488}
                  

                  Warum bekommen ich diesen nicht in die const?

                  J.A.R.V.I.S. 1 Reply Last reply Reply Quote 0
                  • J.A.R.V.I.S.
                    J.A.R.V.I.S. Developer @reutli last edited by

                    @reutli dann schreib es in eine neue Zeile;

                    const state = await this.getStateAsync('husqvarna_mowers.0.6dcad25a-39f7-4baf-9f44-877302c35c1f.Control.duration');
                    const time = state.val;	
                    
                    R 1 Reply Last reply Reply Quote 0
                    • R
                      reutli @J.A.R.V.I.S. last edited by reutli

                      @J-A-R-V-I-S

                      ok, danke.
                      Jetzt bekomme ich einen Wert:

                      (72584) Variablen für Funktionsaufruf : command: Pause time: 24 mid: 6dcad25a-39f7-4baf-9f44-877302c35c1f
                      

                      mit dem Code:

                      async onStateChange(id, state) {
                      		if (state) {
                      			let mid = id.split('.')[2];
                      			let trigger = id.split('.').pop();
                      			this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
                      
                      			if (trigger == 'command') { // wenn Befehl an Mower verändert wurde
                      				const command = state.val;
                      				const state_check = await this.getStateAsync('husqvarna_mowers.0.6dcad25a-39f7-4baf-9f44-877302c35c1f.Control.duration');
                      				this.log.info(JSON.stringify(state_check));
                      				const time = state_check.val;
                      				this.log.info('Variablen für Funktionsaufruf : command: ' + command + ' time: ' + time + ' mid: ' + mid);
                      				//this.await.sendCommand(command, time, mid);
                      			}
                      		} else {
                      			// The state was deleted
                      			this.log.info(`state ${id} deleted`);
                      		}
                      	}
                      

                      Aber: Warum muss ich aus dem ganzen 'onStateChange' eine Async machen? Das habe ich nicht verstanden (und auch noch nirgendwo so gesehen....?

                      Ich frage noch anders: Wie muss ich sonst (außerhalb des 'on StateChange') dann einen State abfragen?

                      Sorry wenn ich so nachfrage, versuche eben zu lernen...

                      Danke!

                      PS: kannst Du mir für mein Beispiel ein sauberes "Try/Catch" zeigen, damit ich das korrekt in andere Bereich übernehmen kann? Habe hier schon einiges an Doku gelesen, aber noch nicht so ganz kapiert...

                      J.A.R.V.I.S. 2 Replies Last reply Reply Quote 0
                      • J.A.R.V.I.S.
                        J.A.R.V.I.S. Developer @reutli last edited by

                        @reutli der Try/Catch sieht wie folgt aus:

                        try {
                        /*
                        Hier all der Code bei dem Fehler abgefangen werden sollen.
                        Sollte ein Fehler geworfen werden, wird der Code, der im Try/Catch steht nicht weiter ausgeführt, Code nach dem Try/Catch wird aber weiter ausgeführt.
                        */
                        } catch(error) {
                            this.log.error(error);
                        }
                        
                        R 1 Reply Last reply Reply Quote 0
                        • J.A.R.V.I.S.
                          J.A.R.V.I.S. Developer @reutli last edited by

                          @reutli sagte in State auslesen in Adapterentwicklung:

                          Ich frage noch anders: Wie muss ich sonst (außerhalb des 'on StateChange') dann einen State abfragen?

                          Entweder du machst es dort auch mit Async Methoden oder du verwendest die Möglichkeit mit Callbacks.

                          R 1 Reply Last reply Reply Quote 0
                          • R
                            reutli @J.A.R.V.I.S. last edited by

                            @J-A-R-V-I-S

                            Dann wäre mein try/catch so richtig, oder:

                            			try {
                            				if (trigger == 'command') { // wenn Befehl an Mower verändert wurde
                            					const command = state.val;
                            					const state_check = await this.getStateAsync('husqvarna_mowers.0.6dcad25a-39f7-4baf-9f44-877302c35c1f.Control.duration');
                            					this.log.info(JSON.stringify(state_check));
                            					const time = state_check.val;
                            					this.log.info('Variablen für Funktionsaufruf : command: ' + command + ' time: ' + time + ' mid: ' + mid);
                            					//this.await.sendCommand(command, time, mid);
                            				}
                            			} catch(e) {
                            				this.log.error(e);
                            			}
                            
                            1 Reply Last reply Reply Quote 0
                            • R
                              reutli @J.A.R.V.I.S. last edited by

                              @J-A-R-V-I-S sagte in State auslesen in Adapterentwicklung:

                              @reutli sagte in State auslesen in Adapterentwicklung:

                              Ich frage noch anders: Wie muss ich sonst (außerhalb des 'on StateChange') dann einen State abfragen?

                              Entweder du machst es dort auch mit Async Methoden oder du verwendest die Möglichkeit mit Callbacks.

                              ok, aber im 'OnStateChange' muss ich beim Async bleiben, oder wie wäre dort das callback-Szenario zu lösen?

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

                              Support us

                              ioBroker
                              Community Adapters
                              Donate

                              827
                              Online

                              31.7k
                              Users

                              79.8k
                              Topics

                              1.3m
                              Posts

                              adapterentwicklung getstate state auslesen
                              3
                              22
                              1300
                              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