Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Blockly
    5. JSON mit Blockly erstellen

    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

    JSON mit Blockly erstellen

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

      Hallo zusammen,

      ich möchte mit einer Abfrage ein JSON erstellen und in einen Datenpunkt schreiben.

      Die Änderung der Datenpunkte bekomme ich auch schon hin.
      îob3.png

      Aber wie kann ich den Text sauber umschreiben? Bei mir kommen die Daten nicht sauber rein?!?

      ["Monat: 2021.12.Anzahl,Anzahl: 0","Monat: 2021.11.Anzahl,Anzahl: 0","Monat: 2021.10.Anzahl,Anzahl: 0","Monat: 2021.09.Anzahl,Anzahl: 0","Monat: 2021.08.Anzahl,Anzahl: 0","Monat: 2021.07.Anzahl,Anzahl: 0","Monat: 2021.06.Anzahl,Anzahl: 0","Monat: 2021.05.Anzahl,Anzahl: 0","Monat: 2021.04.Anzahl,Anzahl: 12","Monat: 2021.03.Anzahl,Anzahl: 12","Monat: 2021.02.Anzahl,Anzahl: 17","Monat: 2021.01.Anzahl,Anzahl: 22"]
      
      J 1 Reply Last reply Reply Quote 0
      • J
        jerzimeik @jerzimeik last edited by

        Bin das Problem anders angegangen, habe es aber noch nicht ganz lösen können.

        iob5.png

        Ich bekomme jetzt Werte, aber leider "kreuz und quer". Wie kann ich die Werte nach dem Namen der Objekt ID sortieren?

        liv-in-sky 1 Reply Last reply Reply Quote 0
        • liv-in-sky
          liv-in-sky @jerzimeik last edited by

          @jerzimeik

          ich mache das so - mit hilfe einer kleinen js-function - weiß nicht, ob es eine reine blockly lösung gibt

          Image 19.png

          Image 20.png

          das ergebnis:

          Image 21.png

          import:

          <xml xmlns="https://developers.google.com/blockly/xml">
           <variables>
             <variable id="V2M7szHj0grpg_E~L6Y:">Liste</variable>
             <variable id="uraW(G+9^E9Lr!q;.Md?">i</variable>
           </variables>
           <block type="schedule" id="Cqz)^1L5B-*_#wudd;?i" x="-1287" y="-1734">
             <field name="SCHEDULE">*/20 * * * * *</field>
             <statement name="STATEMENT">
               <block type="variables_set" id="sWqJxa.MyY?*y-S}mHPz">
                 <field name="VAR" id="V2M7szHj0grpg_E~L6Y:">Liste</field>
                 <value name="VALUE">
                   <block type="lists_create_with" id="s1KG8o;:gtQ6DjJt@w9I">
                     <mutation items="0"></mutation>
                   </block>
                 </value>
                 <next>
                   <block type="controls_forEach" id="~PaFX`X@5n{TM9K^{Jw6">
                     <field name="VAR" id="uraW(G+9^E9Lr!q;.Md?">i</field>
                     <value name="LIST">
                       <block type="selector" id="xQ^Q5{h5V@5PLI+YO@i.">
                         <field name="TEXT">sonoff.0.*.POWER</field>
                       </block>
                     </value>
                     <statement name="DO">
                       <block type="procedures_callcustomnoreturn" id="7(!wl+gy1z~l4.6UX*QE">
                         <mutation name="etwas tun"></mutation>
                       </block>
                     </statement>
                     <next>
                       <block type="control" id="?B@5=?PqH6v*6zKv6fcp">
                         <mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
                         <field name="OID">0_userdata.0.quorleTesr</field>
                         <field name="WITH_DELAY">FALSE</field>
                         <value name="VALUE">
                           <block type="convert_object2json" id="qbFE#Dr-U2bJkWET{;3/">
                             <field name="PRETTIFY">FALSE</field>
                             <value name="VALUE">
                               <block type="variables_get" id="f6)^4$,h523yANg6}Xp7">
                                 <field name="VAR" id="V2M7szHj0grpg_E~L6Y:">Liste</field>
                               </block>
                             </value>
                           </block>
                         </value>
                         <next>
                           <block type="debug" id="Xar6oL7+k!M:2n0atyZt">
                             <field name="Severity">log</field>
                             <value name="TEXT">
                               <shadow type="text" id="E(2nqnZZxAhV95m0d7U|">
                                 <field name="TEXT">test</field>
                               </shadow>
                               <block type="variables_get" id="JVA=ISb(J?rC5aZV)=i?">
                                 <field name="VAR" id="V2M7szHj0grpg_E~L6Y:">Liste</field>
                               </block>
                             </value>
                           </block>
                         </next>
                       </block>
                     </next>
                   </block>
                 </next>
               </block>
             </statement>
           </block>
           <block type="procedures_defcustomnoreturn" id="i*1!AZb|niUFaXh*t{,x" x="-1012" y="-1738">
             <mutation statements="false"></mutation>
             <field name="NAME">etwas tun</field>
             <field name="SCRIPT">Z2V0U3RhdGUoaSkudmFsID8gbXlTdGF0ZT0i4pqqIiA6IG15U3RhdGU9IuKaqyINCg0KTGlzdGUucHVzaCh7DQogICAgDQogICJuYW1lIiAgOmdldE9iamVjdChpKS5jb21tb24ubmFtZSAsDQogICAiaWQiIDogIG15U3RhdGUNCiAgICAgDQogICAgDQp9KQ0KDQog</field>
             <comment pinned="false" h="80" w="160">Beschreibe diese Funktion …</comment>
           </block>
          </xml>
          

          J 1 Reply Last reply Reply Quote 0
          • J
            jerzimeik @liv-in-sky last edited by

            @liv-in-sky
            und wie bekomme ich da meine Werte in eine Liste?
            Stehe gerade auf dem Schlauch bzw bin da nicht ganz so firm drin 😉

            liv-in-sky 1 Reply Last reply Reply Quote 0
            • liv-in-sky
              liv-in-sky @jerzimeik last edited by liv-in-sky

              @jerzimeik

              es wird eine leere list erzeugt (mit blockly) - das ist ein array. dann wird in der schleife jeweils ein json in das array "gepusht" (Liste.push(...) ) -( im function-teil)

              das ganze ist dann ein sog. json-array - zb. zur anzeige mit inventwo-json widget, ...

              die Liste, Array ist [ ]
              ein json ist dann: {"name":"KingArtTouchT2 POWER","id":"⚫"}

              am ende hast du dann ein object (json-array) mit lauter json's drin

              Homoran paul53 2 Replies Last reply Reply Quote 0
              • Homoran
                Homoran Global Moderator Administrators @liv-in-sky last edited by

                @liv-in-sky und wo gibt es diesen push befehl in Blockly?

                liv-in-sky 1 Reply Last reply Reply Quote 0
                • liv-in-sky
                  liv-in-sky @Homoran last edited by

                  @homoran

                  Image 26.png

                  dann müssen aber umständich die { und die anführungszeichen eingearbeitetet werden im text bereich (grün)

                  Homoran 1 Reply Last reply Reply Quote 0
                  • Homoran
                    Homoran Global Moderator Administrators @liv-in-sky last edited by

                    @liv-in-sky sagte in JSON mit Blockly erstellen:

                    müssen aber umständich die { und die anführungszeichen eingearbeitetet werden im text bereich (grün)

                    ist kein Problem, wenn es klappt.

                    Hatte ich auch schon versucht, nur was kommt da in das leere "Loch"

                    liv-in-sky 1 Reply Last reply Reply Quote 0
                    • paul53
                      paul53 @liv-in-sky last edited by

                      @liv-in-sky sagte: in der schleife jeweils ein json in das array "gepusht"

                      Das ist kein Json, sondern ein Objekt.
                      Erst JSON.stringify() macht aus dem Array ein Json.

                      @homoran sagte in JSON mit Blockly erstellen:

                      wo gibt es diesen push befehl in Blockly?

                      Bild_2021-03-11_181334.png

                      Homoran 2 Replies Last reply Reply Quote 0
                      • liv-in-sky
                        liv-in-sky @Homoran last edited by liv-in-sky

                        @homoran siehe hier

                        https://forum.iobroker.net/post/588743

                        endlich ist @paul53 mit dabei - der kennt sich da besser aus

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

                          @paul53 sagte in JSON mit Blockly erstellen:

                          Erst JSON.stringify() macht aus dem Array ein Json.

                          damit Quäle ich mich schon seit Stunden, kommt alles andere als ein echtes JSON bei raus.
                          Das muss aber noch warten, bis die Struktur steht

                          1 Reply Last reply Reply Quote 0
                          • paul53
                            paul53 @liv-in-sky last edited by

                            @liv-in-sky sagte: endlich ist @paul53 mit dabei

                            Ich habe mich bisher zurück gehalten, da uns @jerzimeik bisher nicht verraten hat, wozu er das Json braucht und wie es aussehen soll. Auch über die Datenpunkte, die eingelesen werden, lässt er uns im unklaren.

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

                              @paul53 sagte:

                              Erst JSON.stringify() macht aus dem Array ein Json.

                              und wenn in dem Array ein (oder mehrere) JSON sein soll?

                              J paul53 2 Replies Last reply Reply Quote 0
                              • J
                                jerzimeik @Homoran last edited by

                                @homoran
                                Ich löse mal auf 😉

                                Ich habe Datenobjekte für einen Wert Januar bis Dezember. Diese Werte möchte ich bei Änderung auslesen und in ein Array packen, damit ich danach z.B. einen Chart (Materialdesignwidget) "füllen" kann.
                                iob2.png
                                iob1.png

                                Nachher sollen dann tatsächlich noch weitere Werte wie Kilometer und Stunden in den (oder das) array mit rein. Aber ich wollte ja langsam anfangen und die Systematik erstmal verstehen.

                                Homoran paul53 2 Replies Last reply Reply Quote 0
                                • Homoran
                                  Homoran Global Moderator Administrators @jerzimeik last edited by Homoran

                                  @jerzimeik sagte in JSON mit Blockly erstellen:

                                  Ich löse mal auf

                                  so in der Art will ich auch etwas generieren um es anschließend als Tabelle im JSON-Widget darstellen zu können.
                                  Bei mir kommt ohne die manuelle Funktion aber nicht das gewünschte raus:
                                  JSON_Array.png

                                  Weitere Datenreihen sollen noch folgen

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

                                    @homoran sagte: wenn in dem Array ein (oder mehrere) JSON sein soll?

                                    Das macht man nicht, sondern man erstellt ein Array of objects . JSON.stringify(array) macht aus dem ganzen Array ein JSON.

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

                                      @paul53 sagte:

                                      sondern man erstellt ein Array of objects

                                      das muss jetzt erst mal sacken!
                                      aber wenn ich dich schon mal dran hab 😉
                                      JSON_Array02.png

                                      Anführungsstreichen wurden escaped. Wann und ob man einfache Anführungszeichen nehmen darf/muss hab ich auch noch nicht verstanden

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

                                        @jerzimeik sagte: Ich habe Datenobjekte für einen Wert Januar bis Dezember.

                                        Hättest Du in der Datenpunkt-ID eine führende 0 für Januar bis September verwendet, würde der Selektor schon richtig sortieren.

                                        Da man mit Blockly keine Objekte erzeugen kann, benötigt man dafür eine JS-Funktion mit Ergebnis. Inhalt der Funktion monat_anzahl(id):

                                        return {Monat: getObject(id).common.name.substring(0, 7), Anzahl: getState(id).val};
                                        

                                        Blockly_temp.JPG

                                        <xml xmlns="https://developers.google.com/blockly/xml">
                                         <variables>
                                           <variable id="Bo~tGR5:/kUi:%hJI@J|">id</variable>
                                           <variable id="ViRHFqM2[LYzECku2Kxn">Liste</variable>
                                         </variables>
                                         <block type="schedule" id="V-O[P~`9nRu|91{agd=q" x="138" y="212">
                                           <field name="SCHEDULE">*/20 * * * * *</field>
                                           <statement name="STATEMENT">
                                             <block type="variables_set" id="rum8(?e?6L~+z7cct^YY">
                                               <field name="VAR" id="ViRHFqM2[LYzECku2Kxn">Liste</field>
                                               <value name="VALUE">
                                                 <block type="lists_create_with" id=",?eod49;2jOZb(),+BV?">
                                                   <mutation items="0"></mutation>
                                                 </block>
                                               </value>
                                               <next>
                                                 <block type="controls_forEach" id="He@fMA`t532R5BD!+|]y">
                                                   <field name="VAR" id="Bo~tGR5:/kUi:%hJI@J|">id</field>
                                                   <value name="LIST">
                                                     <block type="selector" id="N.v[Iq2Fk7F$5e%S=_7Y">
                                                       <field name="TEXT">*.Anzahl(functions=rennrad_anzahl)</field>
                                                     </block>
                                                   </value>
                                                   <statement name="DO">
                                                     <block type="lists_setIndex" id="(Mh$*_Rdr_Y/c=nnS97/">
                                                       <mutation at="false"></mutation>
                                                       <field name="MODE">INSERT</field>
                                                       <field name="WHERE">LAST</field>
                                                       <value name="LIST">
                                                         <block type="variables_get" id="/+)oXd(`PqT~UCl@,3zH">
                                                           <field name="VAR" id="ViRHFqM2[LYzECku2Kxn">Liste</field>
                                                         </block>
                                                       </value>
                                                       <value name="TO">
                                                         <block type="procedures_callcustomreturn" id="#:t4wK:jUVIL4fEZ=fTV" inline="true">
                                                           <mutation name="monat_anzahl">
                                                             <arg name="id"></arg>
                                                           </mutation>
                                                           <value name="ARG0">
                                                             <block type="variables_get" id="DY,W+iip?A^(fL,7vA((">
                                                               <field name="VAR" id="Bo~tGR5:/kUi:%hJI@J|">id</field>
                                                             </block>
                                                           </value>
                                                         </block>
                                                       </value>
                                                     </block>
                                                   </statement>
                                                   <next>
                                                     <block type="update" id=")kVm,vM]qdBsyITfSZv]">
                                                       <mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
                                                       <field name="OID">Object ID</field>
                                                       <field name="WITH_DELAY">FALSE</field>
                                                       <value name="VALUE">
                                                         <block type="convert_object2json" id="lhe98_uP[tcPS1d`Or42">
                                                           <field name="PRETTIFY">FALSE</field>
                                                           <value name="VALUE">
                                                             <block type="variables_get" id="sA`Fp6(Vo%~EDa:SR/p)">
                                                               <field name="VAR" id="ViRHFqM2[LYzECku2Kxn">Liste</field>
                                                             </block>
                                                           </value>
                                                         </block>
                                                       </value>
                                                     </block>
                                                   </next>
                                                 </block>
                                               </next>
                                             </block>
                                           </statement>
                                         </block>
                                         <block type="procedures_defcustomreturn" id="@,34^k0|W;sA~juA.PPn" x="388" y="213">
                                           <mutation statements="false">
                                             <arg name="id" varid="Bo~tGR5:/kUi:%hJI@J|"></arg>
                                           </mutation>
                                           <field name="NAME">monat_anzahl</field>
                                           <field name="SCRIPT">cmV0dXJuIHtNb25hdDogZ2V0T2JqZWN0KGlkKS5jb21tb24ubmFtZS5zdWJzdHJpbmcoMCwgNyksIEFuemFobDogZ2V0U3RhdGUoaWQpLnZhbH07</field>
                                           <comment pinned="false" h="80" w="160">Beschreibe diese Funktion …</comment>
                                         </block>
                                        </xml>
                                        

                                        Der Datenpunkt ist für die JSON-Tabelle in Vis geeignet (Überschriften: Monat, Anzahl).

                                        Homoran J 2 Replies Last reply Reply Quote 1
                                        • Homoran
                                          Homoran Global Moderator Administrators @paul53 last edited by

                                          @paul53 sagte:

                                          Da man mit Blockly keine Objekte erzeugen kann,

                                          in Verbindung mit

                                          @paul53 sagte in JSON mit Blockly erstellen:

                                          man erstellt ein Array of objects

                                          heisst also dass das auch nur über eine js-Funktion geht?

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

                                            @homoran sagte: heisst also dass das auch nur über eine js-Funktion geht?

                                            Ja.

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

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            388
                                            Online

                                            31.8k
                                            Users

                                            80.0k
                                            Topics

                                            1.3m
                                            Posts

                                            4
                                            24
                                            5921
                                            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