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] Datenpunkte in Variablen zusammenfassen

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    [gelöst] Datenpunkte in Variablen zusammenfassen

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

      Hallo,
      ich habe hier ein Script, welches die Timestamps der Zigbee-Sensoren in eigenen Datenpunkten ablegt.

      const HexAddress = '00158d00032400af';
      
      const MijiaSwitch = [ 'zigbee.0.00158d00032400af.click' , 'zigbee.0.00158d00032400af.double_click' , 'zigbee.0.00158d00032400af.triple_click' , 
      'zigbee.0.00158d00032400af.quad_click' , 'zigbee.0.00158d00032400af.many_click' ];
      
      const Path = '0_userdata.0.zigbee.';
      
      on({id: MijiaSwitch, val:true}, function (dp) {
      
        var time = formatDate(Number(dp.state.ts), 'TT-MM-JJ hh:mm:ss');
        var change = formatDate(Number(dp.state.lc), 'hh:mm:ss');
        
        setState(Path + HexAddress + '.' + 'ts', time);
        setState(Path + HexAddress + '.' + 'lc', change);
      });
      
      

      Wie könnte ich die Zeile "const MijiaSwitch = [ 'zigbee ..." vereinfachen, so daß ich nicht jeden Datenpunkt für jede Funktion des Schalters eintragen muss? Also, vom Prinzip her, so etwas wie - 'zigbee.0.' + HexAddress + '.*'
      Könnt Ihr mir einen Tipp geben?

      PS: Allen ein gesundes neues Jahr!

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

        @Heidi02 sagte:

        etwas wie - 'zigbee.0.' + HexAddress + '.*'

        Verwendung von $Selector.

        1 Reply Last reply Reply Quote 0
        • H
          Heidi02 last edited by

          Danke, Paul!
          Der $Selector hilft tatsächlich 😊 . Es funktioniert soweit wie gewünscht.
          Nur ein kleines Problem habe ich noch. Ist es möglich Adresse des Sensors als Variable in die Selector - Funktion einzufügen. Daran strauchele ich im Moment.
          Diese Variante funktioniert -

          $('state[id=zigbee.0.00158d00032400af.*]').each(function(id, i) {
          ...
          });
          

          und diese nicht -

          const SensorHexAddress = '00158d00032400af';
          var SensorPath = ('zigbee.0.' + SensorHexAddress + '.*');
          $('state[id = (SensorPath)]').each(function(id, i) {
          ...
          });
          

          Wo liegt mein Fehler?

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

            @Heidi02 sagte:

            und diese nicht -

            Ich habe es zwar noch nicht getestet, aber meiner Meinung nach sollte eine normale String-Verkettung funktionieren. Versuche es mal so:

            const SensorHexAddress = '00158d00032400af';
            const SensorPath = 'zigbee.0.' + SensorHexAddress + '.*';
            $('state[id=' + SensorPath + ']').each(function(id, i) {
            ...
            });
            

            wobei das auch funktionieren sollte:

            const SensorHexAddress = '00158d00032400af';
            const SensorPath = 'zigbee.0.' + SensorHexAddress + '.*';
            $(SensorPath).each(function(id, i) {
            ...
            });
            
            1 Reply Last reply Reply Quote 1
            • H
              Heidi02 last edited by Heidi02

              @paul53
              Vielen Dank für Deine Hilfe!

              Beide Varianten Deiner Lösung funktionieren tadellos. Ich sehe schon, daß ich in Sachen JS / TS noch viel zu lernen habe.

              So sieht das ganze "Machwerk" aus. Eventuell kann es ja jemand gebrauchen. Verbesserungsvorschläge oder Tipps sind gern gesehen.

              export {};
              const SensorHexAddress = '00158d00032400af';
              const UserdataPath = '0_userdata.0.zigbee.';
              
              var SensorPath = ('zigbee.0.' + SensorHexAddress + '.*');
              var SensorStat = [];
              
              $(SensorPath).each(function(id, i) {        
                  SensorStat.push(id);
              });
              
              on({id: SensorStat , val:true}, function (dp) {
              
                var timestamp = formatDate(Number(dp.state.ts), 'TT-MM-JJ hh:mm:ss');
                var lastchange = formatDate(Number(dp.state.lc), 'hh:mm:ss');
                
                setState(UserdataPath + SensorHexAddress + '.' + 'ts', timestamp);
                setState(UserdataPath + SensorHexAddress + '.' + 'lc', lastchange);
              });   
              
              1 Reply Last reply Reply Quote 0
              • H
                Heidi02 last edited by

                Achso:
                Ich hatte zum Testen des Scripts einen Taster verwendet da der viele verschiedene states zurückliefert und natürlich auch schnell gedrückt ist.
                Bei Verwendung anderer Sensoren, welche Werte zurückliefern anstatt logischer Zustände, müsste die Zeile

                on({id: SensorStat , val:true}, function (dp) {
                

                geändert werden in

                on({id: SensorStat , change: 'any'}, function (dp) {
                

                .

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

                Support us

                ioBroker
                Community Adapters
                Donate

                417
                Online

                31.8k
                Users

                80.0k
                Topics

                1.3m
                Posts

                2
                6
                373
                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