Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Daten jede Stunde in JSON schreiben

    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

    Daten jede Stunde in JSON schreiben

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

      Hi,

      möchte bestimmte Daten zur jeden volle Stunde in ein JSON schreiben um sie später darstellen zukönnen, ähnlich wie auf Awekas.

      ist das so okay, oder gibt es da eine besser Lösung?

      // Reference the state objects for temperature, air pressure, and wind speed
      var tempState = "javascript.0.Wetterstation.Aussentemperatur";
      var pressureState = "javascript.0.Wetterstation.Druck_absolut";
      var windState = "javascript.0.Wetterstation.Wind";
      var weatherStatState = "0_userdata.0.JSON.WetterStatistikJedeStunde";
      
      // Define a function to collect data and write to the weather statistics state
      function collectData() {
          // Get the current date and time
          var date = new Date();
          var dateString = date.toLocaleDateString();
          var timeString = date.toLocaleTimeString();
          
          // Get the values of temperature, air pressure, and wind speed
          var temp = getState(tempState).val;
          var pressure = getState(pressureState).val;
          var wind = getState(windState).val;
          
          // Get the current value of the weather statistics state
          var currentData = getState(weatherStatState).val || '[]';
          
          // Convert the current data to an array
          currentData = JSON.parse(currentData);
          
          // Add the new data to the existing data
          currentData.push({
              date: dateString,
              time: timeString,
              temperature: temp,
              airPressure: pressure,
              windSpeed: wind
          });
          
          // Write the updated data to the weather statistics state
          setState(weatherStatState, JSON.stringify(currentData));
      }
      
      // Schedule the collectData function to run every hour
      schedule("0 * * * *", collectData);
      
      
      

      JSON

      {
          "date": "11.2.2023",
          "time": "06:00:00",
          "temperature": 5.72,
          "airPressure": 1028.82,
          "windSpeed": 1.44
        },
        {
          "date": "11.2.2023",
          "time": "07:00:00",
          "temperature": 5.61,
          "airPressure": 1028.61,
          "windSpeed": 0
        },
        {
          "date": "11.2.2023",
          "time": "08:00:00",
          "temperature": 5.61,
          "airPressure": 1028.72,
          "windSpeed": 1.44
        },
        {
          "date": "11.2.2023",
          "time": "09:00:00",
          "temperature": 5.88,
          "airPressure": 1029.33,
          "windSpeed": 0
        },
        {
          "date": "11.2.2023",
          "time": "10:00:00",
          "temperature": 6.22,
          "airPressure": 1029.73,
          "windSpeed": 0.32
        },
        {
          "date": "11.2.2023",
          "time": "11:00:00",
          "temperature": 6.5,
          "airPressure": 1030.24,
          "windSpeed": 1.12
        }
      ]
      

      Awekas

      Screenshot 2023-02-11 111949.png

      A 1 Reply Last reply Reply Quote 0
      • A
        Ahnungsbefreit @Chris76e last edited by

        @chris76e schau mal hier, da habe ich eine Vorlage für solche Aufgaben gepostet. Dabei wird nicht per CRON per getriggert, sondern auf jede Aktualisierung des JSON.

        https://forum.iobroker.net/topic/62779/vorlage-script-json-datenpunkte-mit-sync?_=1676114744352

        Chris76e 1 Reply Last reply Reply Quote 0
        • Chris76e
          Chris76e @Ahnungsbefreit last edited by

          @ahnungsbefreit

          wenn ich dein script richtig verstehe, ist es doch was anderes. Ich möchte aus verschiedene DP stündlich ein JSON erstellen damit ich in meiner VIS eine Tabelle darstellen kann und diese nach Datum filtern.

          A 1 Reply Last reply Reply Quote 0
          • A
            Ahnungsbefreit @Chris76e last edited by

            @chris76e sorry, falsch verstanden. Das kommt davon, wenn man schnell am Handy liest und dabei mit der Enkelin spielt 🙄

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

            Support us

            ioBroker
            Community Adapters
            Donate

            677
            Online

            31.8k
            Users

            80.0k
            Topics

            1.3m
            Posts

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