Skip to content
  • Home
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • 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

  • Default (No Skin)
  • No Skin
Collapse
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. Objekte unterhalb eines Odners lesen

NEWS

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

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

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

Objekte unterhalb eines Odners lesen

Scheduled Pinned Locked Moved JavaScript
3 Posts 2 Posters 170 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Ben1983B Offline
    Ben1983B Offline
    Ben1983
    wrote on last edited by
    #1

    Hallo,

    bezüglich einer MQQT Schnittstelle zu einem Hersteller liefert dieser "NUR" in den Topics einen Wert in Form eine JSONs aus.
    Im MQTT State steht dann bspw.

    {value: 2.5}
    

    Ich würde gerne ein Skript erstellen, das mir mit Eingabe des Startverzeichnisses alle unterlagerten Objekte in einem angegeben Zielverzeichnis erstellt und deren Wert anlegt.

    Gibt es eine Möglichkeit im Javascript Adapter Die Verzeichnisse zu durchlaufen, sodass ich auch wirklich abfragen kann ist es ein folder, oder channel, state etc.?
    Oder wäre ein gängiger Weg mit einer Regex einfach den Hauptpfad zu subscriben und dann auf Änderungen zu reagieren?
    Dann hätte man allerdings die States erst nach einer änderung. und die channels wären grundsätzlich alle folders... ps. sind sie aber glaube im falle vom mqtt adapter sowieso alle, da dies ja einfach die topic darstellt glaube ich.

    Thomas BraunT 1 Reply Last reply
    0
    • Ben1983B Ben1983

      Hallo,

      bezüglich einer MQQT Schnittstelle zu einem Hersteller liefert dieser "NUR" in den Topics einen Wert in Form eine JSONs aus.
      Im MQTT State steht dann bspw.

      {value: 2.5}
      

      Ich würde gerne ein Skript erstellen, das mir mit Eingabe des Startverzeichnisses alle unterlagerten Objekte in einem angegeben Zielverzeichnis erstellt und deren Wert anlegt.

      Gibt es eine Möglichkeit im Javascript Adapter Die Verzeichnisse zu durchlaufen, sodass ich auch wirklich abfragen kann ist es ein folder, oder channel, state etc.?
      Oder wäre ein gängiger Weg mit einer Regex einfach den Hauptpfad zu subscriben und dann auf Änderungen zu reagieren?
      Dann hätte man allerdings die States erst nach einer änderung. und die channels wären grundsätzlich alle folders... ps. sind sie aber glaube im falle vom mqtt adapter sowieso alle, da dies ja einfach die topic darstellt glaube ich.

      Thomas BraunT Online
      Thomas BraunT Online
      Thomas Braun
      Most Active
      wrote on last edited by
      #2

      @ben1983

      So ganz verstehe ich dein Anliegen zwar nicht, aber wenn es um das Zerlegen einer mqtt-Nachricht in Datenpunkte gehen sollte:

      // where the mqtt messages arrive
      
      const mqttDatenpunktObjectId = 'mqtt.0.solix.site.device.scenInfo';
      
      // where the states should appear
      
      const userDataFolder = '0_userdata.0.device';
      
       
      
      // ############## end user config
      
       
      
      // receive
      
      on(mqttDatenpunktObjectId, (obj) => {
        // log(JSON.stringify(obj.state.val))
        const jsonString = obj.state.val;
      
        const jsonData = JSON.parse(jsonString);
      
       
      
        function createObjectsRecursively(parent, data) {
      
          for (const key in data) {
      
            const obj = data[key];
      
            const stateName = `${parent}.${key}`;
      
           
      
            if (typeof obj === 'object') {
      
              createObjectsRecursively(stateName, obj);
      
            } else {
      
             let value = obj;  
      
       
      
              // Check if the state already exists. If it does, just update the value.
      
              // If it does not exist create the states.
      
              if (!existsState(stateName)) {
      
                // If the state does not exist, create it and set the value
      
                createState(stateName, value, {
      
                  name: key,
      
                  type: typeof value,
      
                  role: "value",
      
                  read: true,
      
                  write: true
      
                });
      
                log(`Created state ${stateName}`);
      
                
      
              } else {
                // log(stateName + ': ' + value)
                setState(stateName, value, true);
      
              }
      
            }
      
          }
      
        }
      
         createObjectsRecursively(userDataFolder, jsonData);
      
      });
      

      Linux-Werkzeugkasten:
      https://forum.iobroker.net/topic/42952/der-kleine-iobroker-linux-werkzeugkasten
      NodeJS Fixer Skript:
      https://forum.iobroker.net/topic/68035/iob-node-fix-skript
      iob_diag: curl -sLf -o diag.sh https://iobroker.net/diag.sh && bash diag.sh

      Ben1983B 1 Reply Last reply
      0
      • Thomas BraunT Thomas Braun

        @ben1983

        So ganz verstehe ich dein Anliegen zwar nicht, aber wenn es um das Zerlegen einer mqtt-Nachricht in Datenpunkte gehen sollte:

        // where the mqtt messages arrive
        
        const mqttDatenpunktObjectId = 'mqtt.0.solix.site.device.scenInfo';
        
        // where the states should appear
        
        const userDataFolder = '0_userdata.0.device';
        
         
        
        // ############## end user config
        
         
        
        // receive
        
        on(mqttDatenpunktObjectId, (obj) => {
          // log(JSON.stringify(obj.state.val))
          const jsonString = obj.state.val;
        
          const jsonData = JSON.parse(jsonString);
        
         
        
          function createObjectsRecursively(parent, data) {
        
            for (const key in data) {
        
              const obj = data[key];
        
              const stateName = `${parent}.${key}`;
        
             
        
              if (typeof obj === 'object') {
        
                createObjectsRecursively(stateName, obj);
        
              } else {
        
               let value = obj;  
        
         
        
                // Check if the state already exists. If it does, just update the value.
        
                // If it does not exist create the states.
        
                if (!existsState(stateName)) {
        
                  // If the state does not exist, create it and set the value
        
                  createState(stateName, value, {
        
                    name: key,
        
                    type: typeof value,
        
                    role: "value",
        
                    read: true,
        
                    write: true
        
                  });
        
                  log(`Created state ${stateName}`);
        
                  
        
                } else {
                  // log(stateName + ': ' + value)
                  setState(stateName, value, true);
        
                }
        
              }
        
            }
        
          }
        
           createObjectsRecursively(userDataFolder, jsonData);
        
        });
        
        Ben1983B Offline
        Ben1983B Offline
        Ben1983
        wrote on last edited by
        #3

        @thomas-braun Danke, hatte so gelöst:

        
        const quelle = "mqtt.0.N.c0619ab24727";
        const ziel = "0_userdata.0.Tests.Victron";
        
        const quelleRegEx = new RegExp(quelle);
        on({id:quelleRegEx,change:"any"},async (dp)=>{
            const zielId = ziel + dp.id.substring(quelle.length,dp.id.length);
            const stateJson = JSON.parse(dp.state.val);
            if(typeof stateJson === "object"){
                for(const att in stateJson){
                    const attId = `${zielId}.${att}`;
                    await createStateAsync(attId);
                    await setStateAsync(attId,stateJson[att],true);
                }
            }
            else{
                await createStateAsync(zielId);
                await setStateAsync(zielId,dp.state.val,true);
            }
        });
        

        Allerdings kommt wohl sehr viel über den MQTT Adapter rein.... Ich bekam eine Meldung, dass setState mehr als 1000 mal ausgeführt wurde pro Minute :-D

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        Support us

        ioBroker
        Community Adapters
        Donate

        520

        Online

        32.4k

        Users

        81.5k

        Topics

        1.3m

        Posts
        Community
        Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
        ioBroker Community 2014-2025
        logo
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Home
        • Recent
        • Tags
        • Unread 0
        • Categories
        • Unreplied
        • Popular
        • GitHub
        • Docu
        • Hilfe