Skip to content
  • Home
  • Aktuell
  • Tags
  • 0 Ungelesen 0
  • Kategorien
  • Unreplied
  • Beliebt
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Standard: (Kein Skin)
  • Kein Skin
Einklappen
ioBroker Logo

Community Forum

  1. ioBroker Community Home
  2. English
  3. Scripting / Logic
  4. JSON - Reading a datapoint with a . in the name? - Solved

NEWS

  • UPDATE 31.10.: Amazon Alexa - ioBroker Skill läuft aus ?
    apollon77A
    apollon77
    48
    3
    8.3k

  • Monatsrückblick – September 2025
    BluefoxB
    Bluefox
    13
    1
    1.9k

  • Neues Video "KI im Smart Home" - ioBroker plus n8n
    BluefoxB
    Bluefox
    15
    1
    2.4k

JSON - Reading a datapoint with a . in the name? - Solved

Geplant Angeheftet Gesperrt Verschoben Scripting / Logic
blocklyjavascript
6 Beiträge 2 Kommentatoren 1.6k Aufrufe 1 Watching
  • Älteste zuerst
  • Neuste zuerst
  • Meiste Stimmen
Antworten
  • In einem neuen Thema antworten
Anmelden zum Antworten
Dieses Thema wurde gelöscht. Nur Nutzer mit entsprechenden Rechten können es sehen.
  • E Offline
    E Offline
    erwinberlin
    schrieb am zuletzt editiert von erwinberlin
    #1

    Hi, I am having trouble reading a data point from a JSON message in blockly. The script works, however there is 1 datapoint with a . in it. Is there a trick to get it done? I tried various solutions I found online, however to no avail. See the JSON message below.

    {"Time":"2021-03-28T11:31:08","BMP180":{"Temperature":11.4,"Pressure":1022.7},"SHT3X-0x45":{"Temperature":11.2,"Humidity":45.3,"DewPoint":-0.3},"SDS0X1":{"PM2.5":3.6,"PM10":7.0},"PressureUnit":"hPa","TempUnit":"C"}

    paul53P 1 Antwort Letzte Antwort
    0
    • E erwinberlin

      Hi, I am having trouble reading a data point from a JSON message in blockly. The script works, however there is 1 datapoint with a . in it. Is there a trick to get it done? I tried various solutions I found online, however to no avail. See the JSON message below.

      {"Time":"2021-03-28T11:31:08","BMP180":{"Temperature":11.4,"Pressure":1022.7},"SHT3X-0x45":{"Temperature":11.2,"Humidity":45.3,"DewPoint":-0.3},"SDS0X1":{"PM2.5":3.6,"PM10":7.0},"PressureUnit":"hPa","TempUnit":"C"}

      paul53P Offline
      paul53P Offline
      paul53
      schrieb am zuletzt editiert von paul53
      #2

      @erwinberlin

      obj.SDS0X1['PM2.5']
      

      Blockly:

      Bild_2021-03-28_192744.png

      Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
      Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

      E 1 Antwort Letzte Antwort
      0
      • paul53P paul53

        @erwinberlin

        obj.SDS0X1['PM2.5']
        

        Blockly:

        Bild_2021-03-28_192744.png

        E Offline
        E Offline
        erwinberlin
        schrieb am zuletzt editiert von erwinberlin
        #3

        This is my code, however the PM2.5 still does not work in this script. I used the code in blockly and that works fine! Now I can see the differences, but I still would like to understand why the script does not work?

        Probably obvious to experienced Java programmers, however not for a newbie like me. Any help appreciated!

        var AQ;
        on({id: "mqtt.0.tele.tasmota_sensors_7E466A.SENSOR", change: "any"}, async function (obj) {
          var value = obj.state.val;
          var oldValue = obj.oldState.val;
          AQ = (function () { try {return JSON.parse(getState("mqtt.0.tele.tasmota_sensors_7E466A.SENSOR").val);} catch(e) {return {};}})();
          console.log('PM10 & PM2.5');
          console.log(parseFloat(getAttr(AQ, 'SDS0X1.PM10')));
          console.log(parseFloat(getAttr(AQ, 'SDS0X1['PM2.5']')));
          if (parseFloat(getAttr(AQ, 'SDS0X1.PM10')) != 0) {
            setState("javascript.0.Air-quality.10um", parseFloat(getAttr(AQ, 'SDS0X1.PM10')), true);
          }
          if (parseFloat(getAttr (AQ, 'SDS0X1['PM2.5']') != 0) {
            setState("javascript.0.Air-quality.2_5um", parseFloat(getAttr(AQ, 'SDS0X1['PM2.5']')), true);
          }
        });
        
        paul53P 2 Antworten Letzte Antwort
        0
        • E erwinberlin

          This is my code, however the PM2.5 still does not work in this script. I used the code in blockly and that works fine! Now I can see the differences, but I still would like to understand why the script does not work?

          Probably obvious to experienced Java programmers, however not for a newbie like me. Any help appreciated!

          var AQ;
          on({id: "mqtt.0.tele.tasmota_sensors_7E466A.SENSOR", change: "any"}, async function (obj) {
            var value = obj.state.val;
            var oldValue = obj.oldState.val;
            AQ = (function () { try {return JSON.parse(getState("mqtt.0.tele.tasmota_sensors_7E466A.SENSOR").val);} catch(e) {return {};}})();
            console.log('PM10 & PM2.5');
            console.log(parseFloat(getAttr(AQ, 'SDS0X1.PM10')));
            console.log(parseFloat(getAttr(AQ, 'SDS0X1['PM2.5']')));
            if (parseFloat(getAttr(AQ, 'SDS0X1.PM10')) != 0) {
              setState("javascript.0.Air-quality.10um", parseFloat(getAttr(AQ, 'SDS0X1.PM10')), true);
            }
            if (parseFloat(getAttr (AQ, 'SDS0X1['PM2.5']') != 0) {
              setState("javascript.0.Air-quality.2_5um", parseFloat(getAttr(AQ, 'SDS0X1['PM2.5']')), true);
            }
          });
          
          paul53P Offline
          paul53P Offline
          paul53
          schrieb am zuletzt editiert von paul53
          #4

          @erwinberlin sagte): 'SDS0X1['PM2.5']'

          Try this:

          Bild_2021-03-30_171316.png

          PM10 and PM2.5 values are numbers.

          let x = getAttr(json, 'SDS0X1['PM2.5']'); // designed for Blockly
          // is not
          let x = JSON.parse(json).SDS0X1['PM2.5']; // Javascript
          

          because the 2. parameter of getAttr() must be a string or an array with one string element.

          P.S. Spricht ErwinBerlin kein deutsch?

          Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
          Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

          1 Antwort Letzte Antwort
          0
          • E erwinberlin

            This is my code, however the PM2.5 still does not work in this script. I used the code in blockly and that works fine! Now I can see the differences, but I still would like to understand why the script does not work?

            Probably obvious to experienced Java programmers, however not for a newbie like me. Any help appreciated!

            var AQ;
            on({id: "mqtt.0.tele.tasmota_sensors_7E466A.SENSOR", change: "any"}, async function (obj) {
              var value = obj.state.val;
              var oldValue = obj.oldState.val;
              AQ = (function () { try {return JSON.parse(getState("mqtt.0.tele.tasmota_sensors_7E466A.SENSOR").val);} catch(e) {return {};}})();
              console.log('PM10 & PM2.5');
              console.log(parseFloat(getAttr(AQ, 'SDS0X1.PM10')));
              console.log(parseFloat(getAttr(AQ, 'SDS0X1['PM2.5']')));
              if (parseFloat(getAttr(AQ, 'SDS0X1.PM10')) != 0) {
                setState("javascript.0.Air-quality.10um", parseFloat(getAttr(AQ, 'SDS0X1.PM10')), true);
              }
              if (parseFloat(getAttr (AQ, 'SDS0X1['PM2.5']') != 0) {
                setState("javascript.0.Air-quality.2_5um", parseFloat(getAttr(AQ, 'SDS0X1['PM2.5']')), true);
              }
            });
            
            paul53P Offline
            paul53P Offline
            paul53
            schrieb am zuletzt editiert von paul53
            #5

            @erwinberlin
            Javascript-Version:

            on('mqtt.0.tele.tasmota_sensors_7E466A.SENSOR', function (dp) {
              let sensor = JSON.parse(dp.state.val);
              let pm10 = sensor.SDS0X1.PM10;
              let pm2_5 = sensor.SDS0X1['PM2.5'];
              log('PM10: ' + pm10);
              log('PM2.5: ' + pm2_5);
              if (pm10) setState("javascript.0.Air-quality.10um", pm10, true);
              if (pm2_5) setState("javascript.0.Air-quality.2_5um", pm2_5, true);
            });
            

            Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
            Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

            E 1 Antwort Letzte Antwort
            0
            • paul53P paul53

              @erwinberlin
              Javascript-Version:

              on('mqtt.0.tele.tasmota_sensors_7E466A.SENSOR', function (dp) {
                let sensor = JSON.parse(dp.state.val);
                let pm10 = sensor.SDS0X1.PM10;
                let pm2_5 = sensor.SDS0X1['PM2.5'];
                log('PM10: ' + pm10);
                log('PM2.5: ' + pm2_5);
                if (pm10) setState("javascript.0.Air-quality.10um", pm10, true);
                if (pm2_5) setState("javascript.0.Air-quality.2_5um", pm2_5, true);
              });
              
              E Offline
              E Offline
              erwinberlin
              schrieb am zuletzt editiert von
              #6

              @paul53 Super, vielen Dank! Das hat mich sehr geholfen.

              Übrigens spreche ich Deutsch, aber schreiben ist jedoch etwas ganz anderes...

              1 Antwort Letzte Antwort
              0
              Antworten
              • In einem neuen Thema antworten
              Anmelden zum Antworten
              • Älteste zuerst
              • Neuste zuerst
              • Meiste Stimmen


              Support us

              ioBroker
              Community Adapters
              Donate

              731

              Online

              32.4k

              Benutzer

              81.4k

              Themen

              1.3m

              Beiträge
              Community
              Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
              ioBroker Community 2014-2025
              logo
              • Anmelden

              • Du hast noch kein Konto? Registrieren

              • Anmelden oder registrieren, um zu suchen
              • Erster Beitrag
                Letzter Beitrag
              0
              • Home
              • Aktuell
              • Tags
              • Ungelesen 0
              • Kategorien
              • Unreplied
              • Beliebt
              • GitHub
              • Docu
              • Hilfe