Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Probleme beim parsen von MQTT string in Datenpunkte

    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

    Probleme beim parsen von MQTT string in Datenpunkte

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

      Hallo,
      ich bin absolut ratlos nach nun mehr Stunden die ich mit dem Thema verbraucht habe. Ich habe meine Ölheizung mit einem Ebus Adapter (Raspberry) per MQTT an IO Broker angeschlossen. Die Daten kommen soweit auch schon mal an. Der Befehl sudo mosquitto_sub -d -v -t /# wirft folgende Werte auf der SSH Konsole am IOBroker Raspi aus:

      Client mosqsub/10071-raspberry received PUBLISH (d0, q0, r0, m0, '/ebusd/broadcast/sollw', ... (206 bytes))
      /ebusd/broadcast/sollw {
           "kesselsolltemp": {"value": 42.000},
           "aussentemp": {"value": 9.301},
           "leistungszwang": {"value": null},
           "sollw_status": {"value": "05"},
           "brauchwassersolltemp": {"value": 55.000}}
      Client mosqsub/10071-raspberry received PUBLISH (d0, q0, r0, m0, '/ebusd/broadcast/betrd', ... (247 bytes))
      /ebusd/broadcast/betrd {
           "betrd_status": {"value": 1},
           "zustand": {"value": "40"},
           "stellgrad": {"value": null},
           "kesseltemp": {"value": 45.5},
           "ruecklauftemp": {"value": null},
           "boilertemp": {"value": 53},
           "aussentemp": {"value": 10}}
      
      


      Nun brauch ich diese ganzen Werte ja als Datenpunkte. Nach vielen stöbern habe ich folgendes Javascript geschrieben:

      on({id: 'mqtt.0.ebusd.broadcast.betrd', change: "any"}, function (obj) { 
         
         //der try ist wichtig das der adapter nicht abschmiert bei einem fehler, hiermit parsen wird die json in ein object
         try {obj = JSON.parse(getState('mqtt.0.ebusd.broadcast.betrd').val);
         } catch (e) {
                 console.error('Cannot parse: ' + getState('mqtt.0.ebusd.broadcast.betrd').val);
                 return;
          }
        console.log(obj.kesseltemp);
        console.log(obj.ruecklauftemp);
        console.log(obj.boilertemp);
        console.log(obj.aussentemp);
      
        setState('javascript.0.kesseltemp', parseFloat(obj.kesseltemp));
        setState('javascript.0.ruecklauftemp', parseFloat(obj.ruecklauftemp));
        setState('javascript.0.boilertemp', parseFloat(obj.boilertemp));
        setState('javascript.0.aussentemp', parseFloat(obj.aussentemp));
          });
      

      Das Ergebnis sieht aber so aus:


      15.3.2019, 22:49:59.565 [info ]: javascript.0 Start javascript script.js.common.EBUS_Broadcast_Datenpunkte_2
      15.3.2019, 22:49:59.566 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: subscribe: {"pattern":{"id":"mqtt.0.ebusd.broadcast.betrd","change":"any","q":0},"name":"script.js.common.EBUS_Broadcast_Datenpunkte_2"}
      15.3.2019, 22:49:59.566 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: registered 1 subscription and 0 schedules
      15.3.2019, 22:50:04.330 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: getState(id=mqtt.0.ebusd.broadcast.betrd, timerId=undefined) => {"val":"{\n "betrd_status": {"value": 1},\n "zustand": {"value": "40"},\n "stellgrad": {"value": null},\n "kesseltemp": {"value": 52.5},\n "ruecklauftemp": {"value": null},\n "boilertemp": {"value": 49},\n "aussentemp": {"value": 10}}","ack":true,"ts":1552686604312,"q":0,"from":"system.adapter.mqtt.0","lc":1552686604312}
      15.3.2019, 22:50:04.331 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: [object Object]
      15.3.2019, 22:50:04.331 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: [object Object]
      15.3.2019, 22:50:04.331 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: [object Object]
      15.3.2019, 22:50:04.331 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: [object Object]
      15.3.2019, 22:50:04.331 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: setForeignState(id=javascript.0.kesseltemp, state=null)
      15.3.2019, 22:50:04.332 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: setForeignState(id=javascript.0.ruecklauftemp, state=null)
      15.3.2019, 22:50:04.332 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: setForeignState(id=javascript.0.boilertemp, state=null)
      15.3.2019, 22:50:04.332 [info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: setForeignState(id=javascript.0.aussentemp, state=null)

      ![alt text](58710bef-4bfe-4af3-bd12-fadc1641953a-image.png image url)

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

        @Theisy Welchen Wert (string) enthält der Datenpunkt 'mqtt.0.ebusd.broadcast.betrd' ?

        Gemäß Log sind LF enthalten, die erst mal entfernt werden sollten.

           var json = obj.state.val.replace('\n ','');
           try {obj = JSON.parse(json);
           } catch (e) {
               console.error('Cannot parse: ' + json);
               return;
           }
           log(obj.kesseltemp.value);
           // usw.
        
        T 1 Reply Last reply Reply Quote 1
        • T
          Theisy @paul53 last edited by Theisy

          @paul53 Vielen Dank für deine Rückmeldung. Nachdem ich deinen Hinweis (hoffentlich richtig) eingabut habe, erhalte ich schon mal einen Datenpunkt "kesseltemp" unter den Objekten mit den korrekten Werten, den ich auch für einen Graph weiterverarbeiten kann. Aber die anderen Werte werde nicht an das "Objekt" übergeben.
          Ich poste hier nochmal mein Skript und den Log Auszug. Eventuell fällt dir noch was auf.
          Vielen Dank schon mal, dein Hilfe ist eigentlich unbezahlbar ;-).
          Script:

          on({id: 'mqtt.0.ebusd.broadcast.betrd', change: "any"}, function (obj) {
                  var kesseltemp;
                  var aussentemp;
                  var boilertemp;
             
             var json = obj.state.val.replace('\n ','');
             try {obj = JSON.parse(getState('mqtt.0.ebusd.broadcast.betrd').val);
             } catch (e) {
                 console.error('Cannot parse: ' + getState('mqtt.0.ebusd.broadcast.betrd').val);
                 return;
             }
            setState('javascript.0.kesseltemp', (obj.kesseltemp.value));
            setState('javascript.0.aussentemp', (aussentemp.value));
            setState('javascript.0.boilertemp', (boilertemp.value));
          });
          

          Log:

          16.3.2019, 21:00:25.306	[info ]: javascript.0 Start javascript script.js.common.EBUS_Broadcast_Datenpunkte_2
          16.3.2019, 21:00:25.306	[info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: subscribe: {"pattern":{"id":"mqtt.0.ebusd.broadcast.betrd","change":"any","q":0},"name":"script.js.common.EBUS_Broadcast_Datenpunkte_2"}
          16.3.2019, 21:00:25.306	[info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: registered 1 subscription and 0 schedules
          16.3.2019, 21:00:50.842	[info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: getState(id=mqtt.0.ebusd.broadcast.betrd, timerId=undefined) => {"val":"{\n     \"betrd_status\": {\"value\": 1},\n     \"zustand\": {\"value\": \"40\"},\n     \"stellgrad\": {\"value\": null},\n     \"kesseltemp\": {\"value\": 50.0},\n     \"ruecklauftemp\": {\"value\": null},\n     \"boilertemp\": {\"value\": 50},\n     \"aussentemp\": {\"value\": 7}}","ack":true,"ts":1552766450825,"q":0,"from":"system.adapter.mqtt.0","lc":1552766450825}
          16.3.2019, 21:00:50.843	[info ]: javascript.0 script.js.common.EBUS_Broadcast_Datenpunkte_2: setForeignState(id=javascript.0.kesseltemp, state=50)
          16.3.2019, 21:00:50.843	[error]: javascript.0     at Object.<anonymous> (script.js.common.EBUS_Broadcast_Datenpunkte_2:13:51)
          16.3.2019, 21:01:11.613	[info ]: javascript.0 Stop script script.js.common.EBUS_Broadcast_Datenpunkte_2
          

          Logging IO Broker:

          javascript.0	2019-03-16 21:01:11.237	info	Stop script script.js.common.EBUS_Broadcast_Datenpunkte_2
          javascript.0	2019-03-16 21:00:50.839	error	at Manager.Emitter.emit (/opt/iobroker/node_modules/iobroker.js-controller/node_modules/component-emitter/index.js:133:20)
          javascript.0	2019-03-16 21:00:50.839	error	at Manager.<anonymous> (/opt/iobroker/node_modules/component-bind/index.js:21:15)
          javascript.0	2019-03-16 21:00:50.838	error	at Socket.onpacket (/opt/iobroker/node_modules/iobroker.js-controller/node_modules/socket.io-client/lib/socket.js:236:12)
          javascript.0	2019-03-16 21:00:50.838	error	at Socket.onevent (/opt/iobroker/node_modules/iobroker.js-controller/node_modules/socket.io-client/lib/socket.js:278:10)
          javascript.0	2019-03-16 21:00:50.838	error	at Socket.Emitter.emit (/opt/iobroker/node_modules/iobroker.js-controller/node_modules/component-emitter/index.js:133:20)
          javascript.0	2019-03-16 21:00:50.837	error	at Socket.<anonymous> (/opt/iobroker/node_modules/iobroker.js-controller/lib/states/statesInMemClient.js:52:30)
          javascript.0	2019-03-16 21:00:50.837	error	at Object.change (/opt/iobroker/node_modules/iobroker.js-controller/lib/adapter.js:3425:37)
          javascript.0	2019-03-16 21:00:50.837	error	at Object.stateChange (/opt/iobroker/node_modules/iobroker.javascript/main.js:364:25)
          javascript.0	2019-03-16 21:00:50.836	error	at Object.callback (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:963:38)
          javascript.0	2019-03-16 21:00:50.836	error	at Object.<anonymous> (script.js.common.EBUS_Broadcast_Datenpunkte_2:13:51)
          javascript.0	2019-03-16 21:00:50.835	error	Error in callback: TypeError: Cannot read property 'value' of undefined
          javascript.0	2019-03-16 21:00:50.832	info	script.js.common.EBUS_Broadcast_Datenpunkte_2: setForeignState(id=javascript.0.kesseltemp, state=50)
          mqtt.0	2019-03-16 21:00:50.837	info	send2Server javascript.0.kesseltemp[javascript/0/kesseltemp]
          javascript.0	2019-03-16 21:00:50.831	info	script.js.common.EBUS_Broadcast_Datenpunkte_2: getState(id=mqtt.0.ebusd.broadcast.betrd, timerId=undefined) => {"val":"{\n \"betrd_status\": {\"value\": 1},\n \"zustand\": {\"value\": \"40\"}
          javascript.0	2019-03-16 21:00:25.305	info	script.js.common.EBUS_Broadcast_Datenpunkte_2: registered 1 subscription and 0 schedules
          javascript.0	2019-03-16 21:00:25.305	info	script.js.common.EBUS_Broadcast_Datenpunkte_2: subscribe: {"pattern":{"id":"mqtt.0.ebusd.broadcast.betrd","change":"any","q":0},"name":"script.js.common.EBUS_Broadcast_Datenpunkte_2"}
          javascript.0	2019-03-16 21:00:25.291	info	Start javascript script.js.common.EBUS_Broadcast_Datenpunkte_2
          ```[/s]
          
          Ansicht Objekte:
          
          ![objekte.jpg](/assets/uploads/files/1552766728656-objekte.jpg)
          1 Reply Last reply Reply Quote 0
          • T
            Theisy last edited by

            @paul53
            Vielen Dank, habe meinen Fehler gefunden. (Zeile 13 und 14 fehlte das "obj").
            Somit kann ich erstmal weiterbasteln.

            Danke für deine HIlfe

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

            Support us

            ioBroker
            Community Adapters
            Donate

            412
            Online

            31.8k
            Users

            80.0k
            Topics

            1.3m
            Posts

            2
            4
            849
            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