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

Hi,

Do I need to change the first three ID’s`?
I do have a Tibber account but get the following when I run the script:

⚠️ Error: Not Found: VariableLocal with ID 25349750-2371-40b4-a635-ce60be3f40be
    at ManagerDatabase.findEntry (file:///node_modules/@athombv/homey-local/dist/lib/ManagerDatabaseLocal.mjs:15:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
    at async VariableLocal.findEntry (file:///node_modules/@athombv/homey-core/dist/lib/DatabaseEntry.mjs:214:23)

If I need to change, where/how can I find them? Web API Playground?

Regards,
Andreas

1 Like

Sorry, I realized that this is Variables that I need to create. I have now created them but now I get:
:warning: Error: HTTP error! status: 400

I guess something is wrong with my Api-Key.
My URL is set to: https://app.tibber.com/v4/gql

As the script is not formatted as code, it is full of fancy quotes and -double quotes. These are illegal characters in Homeyscript.
You’ll need them to replace them by using a text editor and use search and replace.
If you use windos, use the most basic tool like notepad.

Hi Ton,

Would you be so kind to format the script the right way?

I have reformatted the script as Peter suggested ( was already afraid I did things wrong in the past when I read the announcement of Dirk yesterday :see_no_evil_monkey:)

However i also get an error 400 when I use your API-url.
My URL is set to https://api.tibber.com/v1-beta/gql
See Tibber Developer

1 Like

Thank you very much. It works for me using the “v1-beta” API-url.

Unfortunately it seems that I need to use the v4 api for what I am trying to achieve.

I am trying to update the State of Charge (SoC) of my electric car using the API as this Tibber SOC Updater for Home Assistant:

I have successfully updated my SoC on https://app.tibber.com/playground/, however it seems that I need to use v4 and that seems to need OAuth 2.0 for authentication.

You can use 2 different URL’s.
Pricedata is something you typically only fetch once a day. My script stores it in a homey variable.
You can use that data in your other script.