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.
    • R
      reutli @Schnup89 last edited by reutli

      @Schnup89

      Danke für Deine Antwort, aber das ist es leider auch nicht...

      Code:

      	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 = this.getState('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`);
      		}
      	}
      

      Fehler:

      husqvarna_mowers.0	2020-04-20 08:52:54.205	error	at processImmediate (internal/timers.js:456:21)
      husqvarna_mowers.0	2020-04-20 08:52:54.205	error	at Immediate.<anonymous> (/usr/local/iobroker/node_modules/iobroker.js-controller/lib/adapter.js:4855:34)
      husqvarna_mowers.0	2020-04-20 08:52:54.205	error	at HusqvarnaMowers.emit (events.js:311:20)
      husqvarna_mowers.0	2020-04-20 08:52:54.205	error	at HusqvarnaMowers.onStateChange (/usr/local/iobroker/node_modules/iobroker.husqvarna_mowers/main.js:521:107)
      husqvarna_mowers.0	2020-04-20 08:52:54.205	error	(71636) TypeError: Cannot read property 'val' of undefined
      husqvarna_mowers.0	2020-04-20 08:52:54.205	error	(71636) uncaught exception: Cannot read property 'val' of undefined
      

      und der State ist m.E. auch da und korrekter String:

      {
        "type": "state",
        "common": {
          "name": "Duration of Command",
          "def": "24",
          "type": "number",
          "role": "indicator",
          "read": true,
          "write": true
        },
        "native": {},
        "from": "system.adapter.husqvarna_mowers.0",
        "user": "system.user.admin",
        "ts": 1587365605661,
        "_id": "husqvarna_mowers.0.6dcad25a-39f7-4baf-9f44-877302c35c1f.Control.duration",
        "acl": {
          "object": 1638,
          "state": 1638
        }
      }
      
      Schnup89 J.A.R.V.I.S. 2 Replies Last reply Reply Quote 0
      • Schnup89
        Schnup89 @reutli last edited by

        @reutli sagte in State auslesen in Adapterentwicklung:

        husqvarna_mowers.0 2020-04-20 08:52:54.205 error (71636) TypeError: Cannot read property 'val' of undefined

        husqvarna_mowers.0	2020-04-20 08:52:54.205	error	(71636) TypeError: Cannot read property 'val' of undefined
        

        .val entfernen 🙂

        const time = this.getState('husqvarna_mowers.0.6dcad25a-39f7-4baf-9f44-877302c35c1f.Control.duration').val;
        // zu ->>>>
        const time = this.getState('husqvarna_mowers.0.6dcad25a-39f7-4baf-9f44-877302c35c1f.Control.duration');						
        
        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 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

                                  1.1k
                                  Online

                                  31.7k
                                  Users

                                  79.7k
                                  Topics

                                  1.3m
                                  Posts

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