Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JSON in taugliches E-Charts umwandeln

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    JSON in taugliches E-Charts umwandeln

    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Maas-Meister last edited by

      Kann mir jemand erklären, oder zeigen, wie ich dieses JSON-Format:

      [
        {
          "from": "2023-11-01T00:00:00.000+01:00",
          "to": "2023-12-01T00:00:00.000+01:00",
          "cost": 165.1500357416,
          "unitPrice": 0.24995,
          "unitPriceVAT": 0.039908,
          "consumption": 660.731,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2023-12-01T00:00:00.000+01:00",
          "to": "2024-01-01T00:00:00.000+01:00",
          "cost": 218.4211066317,
          "unitPrice": 0.21144,
          "unitPriceVAT": 0.033759,
          "consumption": 1033.019,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-01-01T00:00:00.000+01:00",
          "to": "2024-02-01T00:00:00.000+01:00",
          "cost": 253.5435962045,
          "unitPrice": 0.239839,
          "unitPriceVAT": 0.038294,
          "consumption": 1057.139,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-02-01T00:00:00.000+01:00",
          "to": "2024-03-01T00:00:00.000+01:00",
          "cost": 152.2786025299,
          "unitPrice": 0.218416,
          "unitPriceVAT": 0.034873,
          "consumption": 697.194,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-03-01T00:00:00.000+01:00",
          "to": "2024-04-01T00:00:00.000+02:00",
          "cost": 129.0548423584,
          "unitPrice": 0.231925,
          "unitPriceVAT": 0.03703,
          "consumption": 556.45,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-04-01T00:00:00.000+02:00",
          "to": "2024-05-01T00:00:00.000+02:00",
          "cost": 103.0426810056,
          "unitPrice": 0.234781,
          "unitPriceVAT": 0.037486,
          "consumption": 438.888,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-05-01T00:00:00.000+02:00",
          "to": "2024-06-01T00:00:00.000+02:00",
          "cost": 56.86217579,
          "unitPrice": 0.238555,
          "unitPriceVAT": 0.038089,
          "consumption": 238.361,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-06-01T00:00:00.000+02:00",
          "to": "2024-07-01T00:00:00.000+02:00",
          "cost": 65.8974361801,
          "unitPrice": 0.24466,
          "unitPriceVAT": 0.039063,
          "consumption": 269.343,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-07-01T00:00:00.000+02:00",
          "to": "2024-08-01T00:00:00.000+02:00",
          "cost": 56.0706386387,
          "unitPrice": 0.235564,
          "unitPriceVAT": 0.037611,
          "consumption": 238.027,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        },
        {
          "from": "2024-08-01T00:00:00.000+02:00",
          "to": "2024-09-01T00:00:00.000+02:00",
          "cost": 61.7315118714,
          "unitPrice": 0.245993,
          "unitPriceVAT": 0.039276,
          "consumption": 250.948,
          "consumptionUnit": "kWh",
          "currency": "EUR"
        }
      ]
      

      in dieses JSON Format:

      [
        {"ts": 1675887847000, "val": 45},
        {"ts": 1675887848000, "val": 77},
        {"ts": 1675887849000, "val": 180}
      ]
      

      umwandeln kann?

      Hintergrund:
      ich möchte meine Tibber/Stromkosten als Balkendiagramm in E-Charts darstellen.

      Im Besten Fall, schaffe ich es die Monate der letzten Jahre nebeneinander darzustellen um einen vergleich zu erhalten.

      Besten Dank im Voraus.

      paul53 1 Reply Last reply Reply Quote 0
      • paul53
        paul53 @Maas-Meister last edited by paul53

        @maas-meister sagte: in dieses JSON Format umwandeln kann?

        Versuche es mal so:

        // IDs eintragen
        const idSrc = '';
        const idDst = '';
        
        on(idSrc, function(dp) {
            const src = JSON.parse(dp.state.val);
            const dst = [];
            for(let i = 0; i < src.length; i++) {
                const obj = {
                    ts: new Date(src[i].from).getTime(),
                    val: Math.round(src[i].cost)
                };
                dst.push(obj);
            };
            setState(idDst, JSON.stringify(dst), true);
        });
        
        1 Reply Last reply Reply Quote 1
        • mickym
          mickym Most Active last edited by mickym

          Sowas macht man viel einfacher mit JSONATA.

          $.{"ts": $toMillis(from),"val":$round(cost)}
          

          Hier zum Probieren: https://try.jsonata.org/jGIQQKIcU

          Hier JSONATA in JS:

          const obj = JSON.parse('[{"from":"2023-11-01T00:00:00.000+01:00","to":"2023-12-01T00:00:00.000+01:00","cost":165.1500357416,"unitPrice":0.24995,"unitPriceVAT":0.039908,"consumption":660.731,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2023-12-01T00:00:00.000+01:00","to":"2024-01-01T00:00:00.000+01:00","cost":218.4211066317,"unitPrice":0.21144,"unitPriceVAT":0.033759,"consumption":1033.019,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-01-01T00:00:00.000+01:00","to":"2024-02-01T00:00:00.000+01:00","cost":253.5435962045,"unitPrice":0.239839,"unitPriceVAT":0.038294,"consumption":1057.139,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-02-01T00:00:00.000+01:00","to":"2024-03-01T00:00:00.000+01:00","cost":152.2786025299,"unitPrice":0.218416,"unitPriceVAT":0.034873,"consumption":697.194,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-03-01T00:00:00.000+01:00","to":"2024-04-01T00:00:00.000+02:00","cost":129.0548423584,"unitPrice":0.231925,"unitPriceVAT":0.03703,"consumption":556.45,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-04-01T00:00:00.000+02:00","to":"2024-05-01T00:00:00.000+02:00","cost":103.0426810056,"unitPrice":0.234781,"unitPriceVAT":0.037486,"consumption":438.888,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-05-01T00:00:00.000+02:00","to":"2024-06-01T00:00:00.000+02:00","cost":56.86217579,"unitPrice":0.238555,"unitPriceVAT":0.038089,"consumption":238.361,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-06-01T00:00:00.000+02:00","to":"2024-07-01T00:00:00.000+02:00","cost":65.8974361801,"unitPrice":0.24466,"unitPriceVAT":0.039063,"consumption":269.343,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-07-01T00:00:00.000+02:00","to":"2024-08-01T00:00:00.000+02:00","cost":56.0706386387,"unitPrice":0.235564,"unitPriceVAT":0.037611,"consumption":238.027,"consumptionUnit":"kWh","currency":"EUR"},{"from":"2024-08-01T00:00:00.000+02:00","to":"2024-09-01T00:00:00.000+02:00","cost":61.7315118714,"unitPrice":0.245993,"unitPriceVAT":0.039276,"consumption":250.948,"consumptionUnit":"kWh","currency":"EUR"}]')
          console.log (await jsonataExpression(obj,'$.{"ts": $toMillis(from),"val":$round(cost)}'))
          

          fbd0822d-f068-451a-a423-baa8986c16f4-image.png

          oder wenn Du lieber Puzzeln willst:

          891367ff-eb53-4fd5-8289-9757e23ca5db-image.png

          Hier der Import:

          <xml xmlns="https://developers.google.com/blockly/xml">
           <variables>
             <variable id="~TL:j*B5D{UqS]m;)Pfs">obj</variable>
             <variable id=")v4vTlP~N(P44:KLT0Gw">Liste</variable>
           </variables>
           <block type="variables_set" id="peg06W|I?2Mow@Y9q*}D" x="13" y="38">
             <field name="VAR" id="~TL:j*B5D{UqS]m;)Pfs">obj</field>
             <value name="VALUE">
               <block type="convert_json2object" id="f`%/!3_Ok}UC87=2QF=K">
                 <value name="VALUE">
                   <block type="text" id="MzaBvgD+q.%XTHe#vAW0">
                     <field name="TEXT">[   {     "from": "2023-11-01T00:00:00.000+01:00",     "to": "2023-12-01T00:00:00.000+01:00",     "cost": 165.1500357416,     "unitPrice": 0.24995,     "unitPriceVAT": 0.039908,     "consumption": 660.731,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2023-12-01T00:00:00.000+01:00",     "to": "2024-01-01T00:00:00.000+01:00",     "cost": 218.4211066317,     "unitPrice": 0.21144,     "unitPriceVAT": 0.033759,     "consumption": 1033.019,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-01-01T00:00:00.000+01:00",     "to": "2024-02-01T00:00:00.000+01:00",     "cost": 253.5435962045,     "unitPrice": 0.239839,     "unitPriceVAT": 0.038294,     "consumption": 1057.139,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-02-01T00:00:00.000+01:00",     "to": "2024-03-01T00:00:00.000+01:00",     "cost": 152.2786025299,     "unitPrice": 0.218416,     "unitPriceVAT": 0.034873,     "consumption": 697.194,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-03-01T00:00:00.000+01:00",     "to": "2024-04-01T00:00:00.000+02:00",     "cost": 129.0548423584,     "unitPrice": 0.231925,     "unitPriceVAT": 0.03703,     "consumption": 556.45,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-04-01T00:00:00.000+02:00",     "to": "2024-05-01T00:00:00.000+02:00",     "cost": 103.0426810056,     "unitPrice": 0.234781,     "unitPriceVAT": 0.037486,     "consumption": 438.888,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-05-01T00:00:00.000+02:00",     "to": "2024-06-01T00:00:00.000+02:00",     "cost": 56.86217579,     "unitPrice": 0.238555,     "unitPriceVAT": 0.038089,     "consumption": 238.361,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-06-01T00:00:00.000+02:00",     "to": "2024-07-01T00:00:00.000+02:00",     "cost": 65.8974361801,     "unitPrice": 0.24466,     "unitPriceVAT": 0.039063,     "consumption": 269.343,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-07-01T00:00:00.000+02:00",     "to": "2024-08-01T00:00:00.000+02:00",     "cost": 56.0706386387,     "unitPrice": 0.235564,     "unitPriceVAT": 0.037611,     "consumption": 238.027,     "consumptionUnit": "kWh",     "currency": "EUR"   },   {     "from": "2024-08-01T00:00:00.000+02:00",     "to": "2024-09-01T00:00:00.000+02:00",     "cost": 61.7315118714,     "unitPrice": 0.245993,     "unitPriceVAT": 0.039276,     "consumption": 250.948,     "consumptionUnit": "kWh",     "currency": "EUR"   } ]</field>
                   </block>
                 </value>
               </block>
             </value>
             <next>
               <block type="variables_set" id="(#+PZ`G~@:p/g895pkS:">
                 <field name="VAR" id=")v4vTlP~N(P44:KLT0Gw">Liste</field>
                 <value name="VALUE">
                   <block type="convert_jsonata" id=".7npPoUg+:|J8CXQLR3J">
                     <value name="EXPRESSION">
                       <shadow type="text" id="5p4h%I}[kMrO^W`20C+.">
                         <field name="TEXT">$.{"ts": $toMillis(from),"val":$round(cost)}</field>
                       </shadow>
                     </value>
                     <value name="TARGET">
                       <block type="variables_get" id="hu+@oa{EPw!fRWRP)`cC">
                         <field name="VAR" id="~TL:j*B5D{UqS]m;)Pfs">obj</field>
                       </block>
                     </value>
                   </block>
                 </value>
                 <next>
                   <block type="debug" id="j%Xo;V#{NUNB,*0dNXq]">
                     <field name="Severity">info</field>
                     <value name="TEXT">
                       <shadow type="text" id="LjboyTJk|p`Dib_.5P%`">
                         <field name="TEXT">test</field>
                       </shadow>
                       <block type="variables_get" id="rt4DH+XG-Z#5%xD{hn6P">
                         <field name="VAR" id=")v4vTlP~N(P44:KLT0Gw">Liste</field>
                       </block>
                     </value>
                     <next>
                       <block type="comment" id="q=Zd:~(G5(K%R%in#pOt">
                         <field name="COMMENT">Keine Ahnung woher hier das sequence true kommt&amp;#10;also kürzen</field>
                         <next>
                           <block type="variables_set" id="@?,[/v,QR(b:DBVvK!4W">
                             <field name="VAR" id=")v4vTlP~N(P44:KLT0Gw">Liste</field>
                             <value name="VALUE">
                               <block type="lists_getSublist" id=".uvwd^hn=MhuVc[oF1@8">
                                 <mutation at1="true" at2="true"></mutation>
                                 <field name="WHERE1">FROM_START</field>
                                 <field name="WHERE2">FROM_START</field>
                                 <value name="LIST">
                                   <block type="variables_get" id="bpfkc?~6`WMR~Q*G5*(x">
                                     <field name="VAR" id=")v4vTlP~N(P44:KLT0Gw">Liste</field>
                                   </block>
                                 </value>
                                 <value name="AT1">
                                   <block type="math_number" id="UF)%uOJ%L,7[yju:sRp5">
                                     <field name="NUM">1</field>
                                   </block>
                                 </value>
                                 <value name="AT2">
                                   <block type="lists_length" id="WK1dUUjo7CH_-^hI46Ua">
                                     <value name="VALUE">
                                       <block type="variables_get" id="m_%=ErM^*z[-W4hHdQL2">
                                         <field name="VAR" id=")v4vTlP~N(P44:KLT0Gw">Liste</field>
                                       </block>
                                     </value>
                                   </block>
                                 </value>
                               </block>
                             </value>
                             <next>
                               <block type="debug" id="w}=/VtW(}PyUa0h_3r-|">
                                 <field name="Severity">info</field>
                                 <value name="TEXT">
                                   <shadow type="text" id="AeAwnrzILuja!W]vO?6*">
                                     <field name="TEXT">test</field>
                                   </shadow>
                                   <block type="variables_get" id="L,nT^}/Y!i34#3sS=!Kz">
                                     <field name="VAR" id=")v4vTlP~N(P44:KLT0Gw">Liste</field>
                                   </block>
                                 </value>
                               </block>
                             </next>
                           </block>
                         </next>
                       </block>
                     </next>
                   </block>
                 </next>
               </block>
             </next>
           </block>
          </xml>
          

          M 1 Reply Last reply Reply Quote 1
          • M
            Maas-Meister @mickym last edited by

            @mickym

            Mega, genau das habe ich gesucht.
            Funktionierte auf Anhieb - baue ich mir jetzt nach meinen wünschen zusammen.

            Besten Dank 🙂

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

            Support us

            ioBroker
            Community Adapters
            Donate

            1.0k
            Online

            31.7k
            Users

            79.7k
            Topics

            1.3m
            Posts

            3
            4
            191
            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