Script for daily fetching fee data from tibber (ie clock hour of daily max and min)

Hi,

Asked ChatGPT; came up with something which didn’t work. Anybody around here who has a working script?

Regards,

Harry

This script will get the data from Tibber.

The first 3 lines refer to the variables that hold the data, apikey and url.

const tibberVarId = “138521c4-1eb9-4027-a224-cd9f861fbb3f”; // TibberPriceData

const apiKeyVarId = “25349750-2371-40b4-a635-ce60be3f40be”; // Tibber_api_key

const apiUrlVarId = “dd2ff110-db1e-4126-a960-99770614f905”; // tibber_API_URL

// Get API key and URL from Homey variables

const apiKeyVar = await Homey.logic.getVariable({ id: apiKeyVarId });

const apiUrlVar = await Homey.logic.getVariable({ id: apiUrlVarId });

const tibberApiKey = apiKeyVar.value;

const tibberApiUrl = apiUrlVar.value;

if (!tibberApiKey || !tibberApiUrl) {

throw new Error(“Missing Tibber API key or URL in Homey variables.”);

}

// Tibber GraphQL query

const query = {

query: `{

viewer {

  homes {

    currentSubscription {

      priceInfo (resolution: QUARTER_HOURLY) {

        today {

          total

          startsAt

        }

      }

    }

  }

}

}`

};

// Fetch data from Tibber

const response = await fetch(tibberApiUrl, {

method: ‘POST’,

headers: {

'Authorization': \`Bearer ${tibberApiKey}\`,

'Content-Type': 'application/json'

},

body: JSON.stringify(query)

});

if (!response.ok) {

throw new Error(`HTTP error! status: ${response.status}`);

}

const data = await response.json();

// Convert the full result to a string for storage

const tibberPriceData = JSON.stringify(data);

// Update the Homey variable

await Homey.logic.updateVariable({

id: tibberVarId,

variable: { value: tibberPriceData }

});

console.log(“TibberPriceData variable updated successfully.”);

Hi Ton,

Thanks! Will give it a try by coming Monday.

So no specific Tibber account or contract info required?

Can I fetch hourly data by changing `QUARTER_HOURLYinto`HOURLY`

Regards,

Harry Westerbeek

You DO need a Tibber account/contract because you will need to get an API key. (https://developer.tibber.com)

But if you do not have an account why do you then want the Tibber prices?

Use the Homey app PowerByTheHour instead.

1 Like

Hi,
I do have a Tibber account, was not aware of this app. It seems to fulfill my need for price data. With this data I can feed my thermostat flow which is to switch off heatpump (within certain T limits) or on on price peaks and valleys.
Keep you informed.
Regards,
Harry

Hi Ton,

The app is exacty what I needed.

Thanks for providing this solution.

Regards,

Harry