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]Vergleich String mit State.val fehlerhaft

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    [gelöst]Vergleich String mit State.val fehlerhaft

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

      Hallo Community,

      ich habe ein Problem mit einem Vergleich eines State.val mit einem String.
      Hier der Skriptteil, der nicht so funktioniert wie ich mir das vorstelle:

      function checkCommand (ZwayId, ZwayCommand){
          if(!existsState(ZwayId + '.id')){
              log('setDevices: ZwayId nicht vorhanden');
              return false;
          }else{
              let DeviceType = getState(ZwayId + '.deviceType');
              log('setDevices: typeof: ' + typeof(DeviceType.val));
              log('setDevices: Wert: ' + DeviceType.val);
              log('setDevices: Kommando: ' + ZwayCommand);
              
              if(DeviceType.val == 'switchBinary'){
                  log('setDevices: Typ switchBinary erkannt');
                  if (!regexSwitchBinary.test(ZwayCommand)){
                          log('setDevices: ZwayCommand nicht erlaubt');
                          return false;
                      }else{
                          log('setDevices: ZwayCommand erlaubt');
                          return true;
                  }
              }
          }
          return false;
      }
      

      Der if in Zeile 11 wird nicht true, obwohl typeof(DeviceType.val) 'string' im Log ausgibt und der Wert auch 'switchBinary' ist.
      Wo ist mein Denkfehler?
      Bin für jeden Tipp dankbar...

      Gruß
      nordstern

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

        @nordstern19_72
        Sieht alles richtig aus. Log ?

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

          @paul53
          Ja, keine (roten) Fehler.

          Skript aktuell:

          function checkCommand (ZwayId, ZwayCommand){
              if(!existsState(ZwayId + '.id')){
                  log('setDevices: ZwayId nicht vorhanden');
                  return false;
              }else{
                  let DeviceType = getState(ZwayId + '.deviceType');
                  if(getState('ZwayDebug').val==1){
                      log('setDevices: typeof: ' + typeof(DeviceType.val));
                      log('setDevices: Wert: ' + DeviceType.val);
                      log('setDevices: Kommando: ' + ZwayCommand);
                  }
                  
                  //if(DeviceType.val == 'switchBinary'){
                  if(DeviceType.val == DeviceType.val){
                      if(getState('ZwayDebug').val==1){
                          log('setDevices: Typ switchBinary erkannt');
                      }
                      if (!regexSwitchBinary.test(ZwayCommand)){
                              log('setDevices: ZwayCommand nicht erlaubt');
                              return false;
                          }else{
                              if(getState('ZwayDebug').val==1){
                                  log('setDevices: ZwayCommand erlaubt');
                              }
                              return true;
                      }
                  }
              }
              return false;
          }
          

          Log:

          javascript.0	2020-01-22 19:52:35.856	info	(12468) script.js.common.setDevice: setDevices: Command: Ok: 200
          javascript.0	2020-01-22 19:52:35.708	info	(12468) script.js.common.setDevice: setDevices: ZwayCommand erlaubt
          javascript.0	2020-01-22 19:52:35.708	info	(12468) script.js.common.setDevice: setDevices: Typ switchBinary erkannt
          javascript.0	2020-01-22 19:52:35.707	info	(12468) script.js.common.setDevice: setDevices: Kommando: on
          javascript.0	2020-01-22 19:52:35.707	info	(12468) script.js.common.setDevice: setDevices: Wert: switchBinary
          javascript.0	2020-01-22 19:52:35.707	info	(12468) script.js.common.setDevice: setDevices: typeof: string
          javascript.0	2020-01-22 19:52:32.041	info	(12468) script.js.common.setDevice: registered 2 subscriptions and 0 schedules
          javascript.0	2020-01-22 19:52:32.037	info	(12468) Start javascript script.js.common.setDevice
          

          Habe das if mal 'gebrückt' für ein komplettes Log und um zu sehen, ob sonst alles funktioniert.

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

            @nordstern19_72
            Ergänze mal

                        log('setDevices: Länge: ' + DeviceType.val.length);
            

            Vielleicht hängt noch ein unsichtbares Zeichen dran ?

            1 Reply Last reply Reply Quote 0
            • N
              nordstern19_72 last edited by nordstern19_72

              Tatsächlich...

              javascript.0	2020-01-22 20:18:14.246	info	(12468) script.js.common.setDevice: setDevices: Länge: 13
              

              Ich probiere mal .trim()

              Edit:
              Funktioniert...

              Danke!

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

              Support us

              ioBroker
              Community Adapters
              Donate

              795
              Online

              31.7k
              Users

              79.6k
              Topics

              1.3m
              Posts

              2
              5
              580
              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