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.
    • Homoran
      Homoran Global Moderator Administrators @hendrik87 last edited by

      @hendrik87 ganz grob als Denkanstoß
      Screenshot_20250419-211349_Firefox.jpg

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

        @hendrik87

        Die Werte könntest du z.B. so in entsprechende Datenpunkte schreibrn
        Screenshot_20250419-214546_Firefox.jpg
        Datenpunkte müssten angelegt werden und Dem Skript muss ein Trigger hinzugefügt werden. z.B.Änderung des Datenpunktes mit den Rohdaten

        F 1 Reply Last reply Reply Quote 0
        • F
          fastfoot @Homoran last edited by

          Blockly macht mich mittlerweile ganz kirre 🙂 Alternative:

          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 txt = decodeURIComponent(txtorg);
          const json = '{"' + txt.replaceAll('&', '","').replaceAll('=', '":"') + '"}';
          const obj = JSON.parse(json);
          log(obj.tempf);
          log(obj.dateutc);
          log(obj.humidity);
          
          Homoran H T 3 Replies Last reply Reply Quote 1
          • Homoran
            Homoran Global Moderator Administrators @fastfoot last edited by

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

            Blockly macht mich mittlerweile ganz kirre

            SORRY!
            Hab die Unterkategorie nicht gesehen 😞

            F 1 Reply Last reply Reply Quote 0
            • H
              hendrik87 @fastfoot last edited by

              @fastfoot Vielen Dank, das sieht gut aus 😉

              1 Reply Last reply Reply Quote 0
              • F
                fastfoot @Homoran last edited by

                @homoran Och, ich seh das nicht so eng, Deine Lösung ist sicher für die Blockly-Fan Gemeinde interessant. Meine ersten Gehversuche mit Javascript waren hier im Forum, mit Blockly 🙂

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

                  @fastfoot ich hatte beim letzten Versuch anscheinend genau das versucht, was du da als js hast.
                  bekam aber dauernd ' und " gemischt heraus, so dass ich kein valides json hinbekommen hatte.
                  jetzt hatte uch es über Listen versucht.
                  Hat mir aber keine Ruhe gelassen, mit Trick klappte s auch mit JSON Erstellung via Blockly.

                  ich bin so frei
                  Screenshot_20250419-223116_Firefox.jpg
                  Ich musste in zwei Schritten ersetzen

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

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

                    Blockly macht mich mittlerweile ganz kirre 🙂 Alternative:

                    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 txt = decodeURIComponent(txtorg);
                    const json = '{"' + txt.replaceAll('&', '","').replaceAll('=', '":"') + '"}';
                    const obj = JSON.parse(json);
                    log(obj.tempf);
                    log(obj.dateutc);
                    log(obj.humidity);

                    Gute Idee 🙂

                    Da sollte ein try catch drum sein. Zur Sicherheit und das hier in Zeile 2:

                    // EDIT - wegen Forumsdarstellungfehlern es wird nur 1 \ dargestellt in manchen Fällen
                    // const txt = decodeURIComponent(txtorg).replaceAll(/[^
                    // da müssen jetzt 2 \ eingefügt werden und dann nachfolgendes angehangen
                    // ]"/g, '\\"');
                    
                    const txt = decodeURIComponent(txtorg).replaceAll(/[^\]"/g, '\\"');
                    

                    Wenn da ein " im Text steht gibt es sonst nen Fehler.

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

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

                      Da sollte...

                      vor Allem solltest Du deine Ratschläge testen, bevor du die veröffentlichst 🙂

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

                        @fastfoot
                        Verstehe ich nicht

                        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 txt = decodeURIComponent(txtorg).replaceAll(/[^\]"/g, '\\"');
                        try {
                            const json = '{"' + txt.replaceAll('&', '","').replaceAll('=', '":"') + '"}';
                        const obj = JSON.parse(json);
                        log(obj.tempf);
                        log(obj.dateutc);
                        log(obj.humidity);
                        } catch (e) {
                            log(e)
                        }
                        

                        so sieht das in meinen Skript aus - oben hab ich kein " ein "und ein \" eingefügt.

                        F 1 Reply Last reply Reply Quote 1
                        • 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
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            843
                                            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