Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Textvariablen mit & - wie trennen?

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Textvariablen mit & - wie trennen?

    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      fastfoot @ticaki last edited by

      @ticaki das kompiliert doch so gar nicht! Der Grund ist in Zeile 2

      T 1 Reply Last reply Reply Quote 0
      • T
        ticaki Developer @fastfoot last edited by ticaki

        @fastfoot
        Da stehe ich jetzt auf dem Schlauch - der Code geht bei mir in typescript und javascript.

        Bildschirmfoto 2025-04-21 um 13.42.45.png

        JS Adapter v8.9.1 / nodejs 20.x.x

        F 1 Reply Last reply Reply Quote 0
        • haus-automatisierung
          haus-automatisierung Developer Most Active last edited by haus-automatisierung

          Warum alles selbst zerlegen? URLSearchParams ist genau dafür gedacht.

          const url = require('node:url');
          
          const urlParams = 'PASSKEY=xxx&stationtype=GW2000A_V3.2.2&runtime=86732&heap=82460&dateutc=2025-04-19%2018%3A57%3A39&tempinf=69.08&humidityin=43&baromrelin=29.740&baromabsin=29.740&tempf=46.40&humidity=76&vpd=0.076&winddir=304&windspeedmph=3.80&windgustmph=7.38&maxdailygust=13.65&solarradiation=0.16&uv=0&rrain_piezo=0.000&erain_piezo=0.000&hrain_piezo=0.000&drain_piezo=0.000&wrain_piezo=0.409&mrain_piezo=0.409&yrain_piezo=0.409&srain_piezo=0&ws90cap_volt=5.2&ws90_ver=133&wh90batt=3.26&freq=868M&model=GW2000A&interval=60';
          const params = new url.URLSearchParams(urlParams);
          const obj = {};
          
          for (const key of params.keys()) {
              obj[key] = params.get(key);
          }
          
          console.log(JSON.stringify(obj));
          

          @hendrik87 sagte in Textvariablen mit & - wie trennen?:

          Mit welchem Befehl kann ich hier einzelne Werte auslesen (z.B. "tempf")?

          Das ist dann sogar noch einfacher:

          const url = require('node:url');
          
          const urlParams = 'PASSKEY=xxx&stationtype=GW2000A_V3.2.2&runtime=86732&heap=82460&dateutc=2025-04-19%2018%3A57%3A39&tempinf=69.08&humidityin=43&baromrelin=29.740&baromabsin=29.740&tempf=46.40&humidity=76&vpd=0.076&winddir=304&windspeedmph=3.80&windgustmph=7.38&maxdailygust=13.65&solarradiation=0.16&uv=0&rrain_piezo=0.000&erain_piezo=0.000&hrain_piezo=0.000&drain_piezo=0.000&wrain_piezo=0.409&mrain_piezo=0.409&yrain_piezo=0.409&srain_piezo=0&ws90cap_volt=5.2&ws90_ver=133&wh90batt=3.26&freq=868M&model=GW2000A&interval=60';
          const params = new url.URLSearchParams(urlParams);
          
          const tempf = params.get('tempf');
          
          console.log(tempf);
          
          1 Reply Last reply Reply Quote 2
          • T
            ticaki Developer last edited by

            Mein Beispiel oben um einen Kommentar ergänzt, der die richtige Schreibweise erklärt, ohne die nachfolgende Diskussion aus dem Kontext zu reissen.

            1 Reply Last reply Reply Quote 0
            • mickym
              mickym Most Active @hendrik87 last edited by mickym

              @hendrik87 sagte in Textvariablen mit & - wie trennen?:

              PASSKEY=xxx&stationtype=GW2000A_V3.2.2&runtime=86732&heap=82460&dateutc=2025-04-19%2018%3A57%3A39&tempinf=69.08&humidityin=43&baromrelin=29.740&baromabsin=29.740&tempf=46.40&humidity=76&vpd=0.076&winddir=304&windspeedmph=3.80&windgustmph=7.38&maxdailygust=13.65&solarradiation=0.16&uv=0&rrain_piezo=0.000&erain_piezo=0.000&hrain_piezo=0.000&drain_piezo=0.000&wrain_piezo=0.409&mrain_piezo=0.409&yrain_piezo=0.409&srain_piezo=0&ws90cap_volt=5.2&ws90_ver=133&wh90batt=3.26&freq=868M&model=GW2000A&interval=60

              Ich würde so was natürlich mit JSONATA in ein Objekt überführen, dann kann einzelne Werte ganz bequem selektieren:
              9bf5b934-2697-48c1-bfc6-323ccb3732f3-image.png

              Hier der JSONATA Ausdruck

              $split($,"&"){$substringBefore("="):$substringAfter("=")}
              

              <xml xmlns="https://developers.google.com/blockly/xml">
               <variables>
                 <variable id="hu+F[F+=/+n6(w#b!bkk">Text</variable>
                 <variable id="8+=Mk?BD}YCcGl~4E)j|">Objekt</variable>
               </variables>
               <block type="variables_set" id="^~Sv#fxrrEl3v%:$_j(K" x="-137" y="63">
                 <field name="VAR" id="hu+F[F+=/+n6(w#b!bkk">Text</field>
                 <value name="VALUE">
                   <block type="text" id="}KJz}$Rs!%XxMZl%j4_w">
                     <field name="TEXT">PASSKEY=xxx&amp;stationtype=GW2000A_V3.2.2&amp;runtime=86732&amp;heap=82460&amp;dateutc=2025-04-19%2018%3A57%3A39&amp;tempinf=69.08&amp;humidityin=43&amp;baromrelin=29.740&amp;baromabsin=29.740&amp;tempf=46.40&amp;humidity=76&amp;vpd=0.076&amp;winddir=304&amp;windspeedmph=3.80&amp;windgustmph=7.38&amp;maxdailygust=13.65&amp;solarradiation=0.16&amp;uv=0&amp;rrain_piezo=0.000&amp;erain_piezo=0.000&amp;hrain_piezo=0.000&amp;drain_piezo=0.000&amp;wrain_piezo=0.409&amp;mrain_piezo=0.409&amp;yrain_piezo=0.409&amp;srain_piezo=0&amp;ws90cap_volt=5.2&amp;ws90_ver=133&amp;wh90batt=3.26&amp;freq=868M&amp;model=GW2000A&amp;interval=60</field>
                   </block>
                 </value>
                 <next>
                   <block type="variables_set" id="_D5~rAL,~%b==9Nf3*L`">
                     <field name="VAR" id="8+=Mk?BD}YCcGl~4E)j|">Objekt</field>
                     <value name="VALUE">
                       <block type="convert_jsonata" id="u}[_Z`KWHAeHkdE@@V?=">
                         <value name="EXPRESSION">
                           <shadow type="text" id="eg-6.rV=oGTM`RArquVg">
                             <field name="TEXT">$split($,"&amp;"){$substringBefore("="):$substringAfter("=")}</field>
                           </shadow>
                         </value>
                         <value name="TARGET">
                           <block type="variables_get" id="n+aI}jp_T]nxZ439rGaK">
                             <field name="VAR" id="hu+F[F+=/+n6(w#b!bkk">Text</field>
                           </block>
                         </value>
                       </block>
                     </value>
                     <next>
                       <block type="debug" id="k08ZC410l/7`T|+g_+w@">
                         <field name="Severity">info</field>
                         <value name="TEXT">
                           <shadow type="text" id=",T!NCf4e|u/ADp:g1bn[">
                             <field name="TEXT">test</field>
                           </shadow>
                           <block type="variables_get" id="xX[*0A$F`nN{~,hNEQSb">
                             <field name="VAR" id="8+=Mk?BD}YCcGl~4E)j|">Objekt</field>
                           </block>
                         </value>
                         <next>
                           <block type="debug" id="$ZA{KAj::[0NO^}@r2f?">
                             <field name="Severity">info</field>
                             <value name="TEXT">
                               <shadow type="text" id="v3S]^xo$QRpmTi_m:MiD">
                                 <field name="TEXT">test</field>
                               </shadow>
                               <block type="get_attr" id="sch|Vdte#46P|$E0N){b">
                                 <value name="PATH">
                                   <shadow type="text" id="S6Fmb#3I5odaK!_L3/Xx">
                                     <field name="TEXT">tempf</field>
                                   </shadow>
                                 </value>
                                 <value name="OBJECT">
                                   <shadow type="get_object" id="Z[Q*J-EZZlJA:5Q36$$z">
                                     <field name="OID">Object ID</field>
                                   </shadow>
                                   <block type="variables_get" id="}pfM{4G@w.%+V=RxDl|%">
                                     <field name="VAR" id="8+=Mk?BD}YCcGl~4E)j|">Objekt</field>
                                   </block>
                                 </value>
                               </block>
                             </value>
                           </block>
                         </next>
                       </block>
                     </next>
                   </block>
                 </next>
               </block>
               <block type="convert_json2object" id="1-ME3y(y.YrZvl:v}Pb`" x="388" y="438"></block>
              </xml>
              

              EDIT: Geht sogar noch einfacher - nochmals korrigiert.

              https://try.jsonata.org/sn8GeyHWc

              oder auf den Code von @fastfoot angewandt:

              const txtorg = "PASSKEY=xxx&stationtype=GW2000A_V3.2.2&runtime=86732&heap=82460&dateutc=2025-04-19%2018%3A57%3A39&tempinf=69.08&humidityin=43&baromrelin=29.740&baromabsin=29.740&tempf=46.40&humidity=76&vpd=0.076&winddir=304&windspeedmph=3.80&windgustmph=7.38&maxdailygust=13.65&solarradiation=0.16&uv=0&rrain_piezo=0.000&erain_piezo=0.000&hrain_piezo=0.000&drain_piezo=0.000&wrain_piezo=0.409&mrain_piezo=0.409&yrain_piezo=0.409&srain_piezo=0&ws90cap_volt=5.2&ws90_ver=133&wh90batt=3.26&freq=868M&model=GW2000A&interval=60";
              const obj = (await jsonataExpression(txtorg, '$split($,"&"){$substringBefore("="):$substringAfter("=")}'));
              log(obj.tempf);
              log(obj.dateutc);
              log(obj.humidity);
              

              b1ce9d44-913d-4bd6-955d-31b8da5eaac8-image.png

              haus-automatisierung 1 Reply Last reply Reply Quote 2
              • haus-automatisierung
                haus-automatisierung Developer Most Active @mickym last edited by

                Geht es in diesem Thread darum, wie man es möglichst kompliziert löst? Dann entschuldigt meinen simplen Ansatz 🙂

                mickym 1 Reply Last reply Reply Quote 3
                • mickym
                  mickym Most Active @haus-automatisierung last edited by

                  @haus-automatisierung sagte in Textvariablen mit & - wie trennen?:

                  Geht es in diesem Thread darum, wie man es möglichst kompliziert löst? Dann entschuldigt meinen simplen Ansatz 🙂

                  Nein - aber man muss halt wieder eine Bibliothek installieren, während JSONATA halt schon an Board ist.

                  T 1 Reply Last reply Reply Quote 0
                  • T
                    ticaki Developer @mickym last edited by

                    @mickym

                    Alles was mit node: anfängt ist in nodejs integriert.

                    mickym 1 Reply Last reply Reply Quote 1
                    • mickym
                      mickym Most Active @ticaki last edited by

                      @ticaki sagte in Textvariablen mit & - wie trennen?:

                      @mickym

                      Alles was mit node: anfängt ist in nodejs integriert.

                      Ok - wusste ich nicht

                      • aber mein Code ist auch nicht länger. Aber egal - könnt ja meine Lösung wieder löschen.
                      T W 2 Replies Last reply Reply Quote 0
                      • T
                        ticaki Developer @mickym last edited by

                        @mickym
                        Ich habs dir nur mitgeteilt ohne wertung 🙂

                        bahnuhr 1 Reply Last reply Reply Quote 2
                        • bahnuhr
                          bahnuhr Forum Testing Most Active @ticaki last edited by

                          @ticaki
                          @haus-automatisierung

                          Gibts da irgendwo ne Beschreibung wie man dies anwendet und was damit möglich ist ?

                          T 1 Reply Last reply Reply Quote 0
                          • T
                            ticaki Developer @bahnuhr last edited by

                            @bahnuhr

                            Jo auf der nodejs/api/url seite 😄

                            https://nodejs.org/api/url.html

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

                              @ticaki node v23??

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

                                Ich habe den Exkurs wegen der Darstellungsprobleme in code-tags bei eckigen Klammern ausgegliedert nach
                                https://forum.iobroker.net/topic/80809/code-in-code-tags-unvollständig-bei-eckigen-klammern/1

                                ich hoffe ich habe nicht zuviel Kontext zerstört

                                1 Reply Last reply Reply Quote 2
                                • T
                                  ticaki Developer @Homoran last edited by ticaki

                                  @homoran
                                  Bildschirmfoto 2025-04-21 um 18.24.27.png

                                  1 Reply Last reply Reply Quote 2
                                  • W
                                    Wildbill @mickym last edited by

                                    @mickym sagte in Textvariablen mit & - wie trennen?:

                                    @ticaki sagte in Textvariablen mit & - wie trennen?:

                                    @mickym

                                    Alles was mit node: anfängt ist in nodejs integriert.

                                    Ok - wusste ich nicht

                                    • aber mein Code ist auch nicht länger. Aber egal - könnt ja meine Lösung wieder löschen.

                                    Bloß nix löschen. Auch wenn viele Wege nach Rom führen, ist doch jeder davon schön und der eine bevorzugt den, der andere einen ganz Anderen. Das ist doch das Schöne an iobroker, dass es nicht stumpf nur den einen Weg gibt.
                                    Interessant ist, wie jetzt hier drei komplett unterschiedliche Lösungen stehen, die alle zum Ziel führen. Finde ich genial, so direkt beieinander den Vergleich. 😃

                                    Gruss, Jürgen

                                    F 1 Reply Last reply Reply Quote 3
                                    • F
                                      fastfoot @Wildbill last edited by

                                      @wildbill na eigentlich sind es ja vier Lösungen, wir wollen die Blockly Lösung ja nicht unterschlagen. Aber hast Recht, nur drei sind richtig. Da @ticaki sich beharrlich weigert seine Lösung auch mal zu testen ist ihm trotz meiner Hinweise nicht aufgefallen dass sein Regex schlicht falsch ist. Da werden alle Zeichen unmittelbar vor einem Hochkomma gelöscht, die kein \ sind. aus x"xx wird somit "xx

                                      Mein persönlicher Favorit ist die Lösung von @haus-automatisierung , auch wenn das natürlich nur ein inoffizielles Statement ist. Die Funktion kannte ich leider gar nicht. Die jsonata Lösung ist deshalb klasse weil sie zeigt was damit alles möglich ist

                                      T 1 Reply Last reply Reply Quote 0
                                      • T
                                        ticaki Developer @fastfoot last edited by ticaki

                                        @fastfoot
                                        Schön dass das Ratespielchen mal ein Ende hat. Du willst also sagen mein Vorschlag ein Zeichen unterschlägt, wo deine Lösung sich einfach aufhängt.
                                        Muss dir sagen, dass mich dein bla bla ärgert - thema und du erledigt.

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

                                        Support us

                                        ioBroker
                                        Community Adapters
                                        Donate

                                        875
                                        Online

                                        31.7k
                                        Users

                                        79.8k
                                        Topics

                                        1.3m
                                        Posts

                                        8
                                        29
                                        677
                                        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