Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. [Gelöst] SetState funktioniert nicht

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    [Gelöst] SetState funktioniert nicht

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

      Edit: Das Skript funktioniert auf einmal korrekt.

      Hallo zusammen

      Ich bin relativ neu mit JS unterwegs, daher bitte nicht böse sein, wenn die Antwort offensichtlich ist.

      Ich habe ein Skript geschrieben, welches per API ein paar Daten abruft und diese in Objekte schreiben soll.
      Soweit scheint das auch zu funktionieren und in den Logs sehe ich, dass die Werte "lastDownloads[0]" bis *lastDownloads[4]" korrekt gefüllt sind. Die Objekte erhalten aber keine neuen Werte und ich erhalte auch keine Fehlermeldung.

      Die Datenpunkte vom Typ state existieren ebenfalls.

      Hat hier jemand eine Idee, wo das Problem liegen könnte?

      const axios = require('axios');
      
      const sonarrIP = '192.168.1.214:8093'; // IP und Port
      const apiKey = 'XYZ'; // API Key
      
      const options = {
        url: `http://${sonarrIP}/api/v3/history`,
        headers: {
          'X-Api-Key': apiKey
        }
      };
      
      axios(options)
        .then(response => {
          const records = response.data.records;
          const lastDownloads = [];
      
          for (let i = 0; i < records.length && i < 5; i++) {
            lastDownloads.push(records[i].sourceTitle);
          }
      
          //console.log(lastDownloads);
          console.log(lastDownloads[0]);
      	console.log(lastDownloads[1]);
      	console.log(lastDownloads[2]);
      	console.log(lastDownloads[3]);
      	console.log(lastDownloads[4]);
      
      
          // Schreibe die letzten 5 Downloads in die gewünschten Datenpunkte
      	setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.1', lastDownloads[0]);
          setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.2', lastDownloads[1]);
          setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.3', lastDownloads[2]);
          setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.4', lastDownloads[3]);
          setState('0_userdata.0.Media.Sonarr.Letzte_Downloads.5', lastDownloads[4]);
        })
      
      M 1 Reply Last reply Reply Quote 0
      • M
        Moebi @wizzardking last edited by

        Auch wenn es bereits auf gelöst steht.
        So mancher kennt den Stolperstein nicht.😉

        Screenshot_2023-03-07-09-42-20-472_com.whatsapp.jpg

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

        Support us

        ioBroker
        Community Adapters
        Donate

        860
        Online

        31.7k
        Users

        79.6k
        Topics

        1.3m
        Posts

        2
        2
        153
        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