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]: Wie kann man Tibber Durchschnittspreise errechnen?

    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

    [gelöst]: Wie kann man Tibber Durchschnittspreise errechnen?

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

      In den Tibberlink-Objekten gibt es ja für jede Stunde eine Preisangabe:
      Bildschirmfoto 2024-02-11 um 14.39.30.jpg
      Wenn man nun die billigsten x Stunden nutzen will, dann wäre der Durchschnittspreis für diese x Stunden interessant (X ist dann eine Zahl von 1-5).
      Die Startzeit für diese billigen x Stunden ist bekannt und kann jeden Tag ausgelesen werden.

      Wie geht man das in Javascript an, damit man klug immer die richtigen Objekte ausliest und daraus den Durchschnittspreis ausrechnet?

      Danke

      P 1 Reply Last reply Reply Quote 0
      • P
        pingo @pingo last edited by pingo

        Mein Sohn hat es gelöst:

        let start = 21 // values 0-23
        let timePeriod = 4 // values 1-5
        
        const allPrices = {
            0: 0.1,
            1: 0.26,
            2: 0.32,
            3: 0.38,
            4: 0.31,
            5: 0.122,
            6: 0.25,
            7: 0.35,
            8: 0.177,
            9: 0.366,
            10: 0.14,
            11: 0.288,
            12: 0.1,
            13: 0.344,
            14: 0.1,
            15: 0.322,
            16: 0.1,
            17: 0.377,
            18: 0.1,
            19: 0.266,
            20: 0.1,
            21: 0.37,
            22: 0.13,
            23: 0.25,
        }
        
        const averagePrice = startCalc(start, timePeriod)
        console.log(averagePrice)
        
        function startCalc(start, timePeriod) {
            const prices = Object.values(allPrices)
            const earlyTimePeriod = -24 + (start + timePeriod)
            const lateTimePeriod = 24 - (start)
            let hourPrices
        
            if(earlyTimePeriod <= 0) { // for hours 0-19
                hourPrices = calcPrices(prices, 'default', timePeriod, start)
            } else { // for hours 20-23
                let earlyHourPrices = calcPrices(prices, 'early', earlyTimePeriod)
                let lateHourPrices = calcPrices(prices, 'late', lateTimePeriod)
                hourPrices = earlyHourPrices + lateHourPrices
            }
            return calcAveragePrice(hourPrices, timePeriod)
        }
        
        function calcPrices(pricesArr, checkHours, timePeriod, start) {
            const prices = checkHours === 'late' ? pricesArr.reverse() : pricesArr 
            const key = start || 0
            let sum
        
            if(timePeriod === 1) {
                sum = prices[key]
            } else if (timePeriod === 2) {
                sum = prices[key] + prices[key + 1]
            } else if (timePeriod === 3) {
                sum = prices[key] + prices[key + 1] + prices[key + 2]
            } else if (timePeriod === 4) {
                sum = prices[key] + prices[key + 1] + prices[key + 2] + prices[key + 3]
            } else if (timePeriod === 5) {
                sum = prices[key] + prices[key + 1] + prices[key + 2] + prices[key + 3] + prices[key + 4]
            }
            return sum
        }
        
        function calcAveragePrice(hourPrices, timePeriod) {
            const averagePrice = hourPrices / timePeriod
            return averagePrice
        }
        

        natürlich sollte man oben die Tibber Preise aus den Objekten auslesen und dort einfügen. Die Zahlen sind reine Fantasiezahlen zum Testen

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

        Support us

        ioBroker
        Community Adapters
        Donate

        958
        Online

        31.9k
        Users

        80.2k
        Topics

        1.3m
        Posts

        1
        2
        164
        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