Skip to content

Instantly share code, notes, and snippets.

@wwerther
Created June 23, 2023 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwerther/301794b25f11e737b72e48afff946186 to your computer and use it in GitHub Desktop.
Save wwerther/301794b25f11e737b72e48afff946186 to your computer and use it in GitHub Desktop.
Iobroker - Skript Awattar History 2 Influx DB
const axios = require('axios').default;
const influxDbInstance = 'influxdb.0';
const token = 'xxxxxxxxxxxxxxxxxxxx';
const measurement = 'energy-stats';
const influxDbInstanceConfig = await getObjectAsync(`system.adapter.${influxDbInstance}`);
const protocol = influxDbInstanceConfig.native.protocol;
const host = influxDbInstanceConfig.native.host;
const port = influxDbInstanceConfig.native.port;
const org = influxDbInstanceConfig.native.organization;
const bucket = influxDbInstanceConfig.native.dbname;
// Define the URL to the Awattar endpoint
const query = "https://api.awattar.de/v1/marketdata";
// Send the HTTP Get Request to the endpoint and get the response
//const response = await axios.get(query, { params: { start:1685570400000, end:1687384800000} });
const timeStampStart = new Date("2020-01-01 00:00");
const timeStampEnd = new Date("2021-01-01 00:00");
const st=timeStampStart.getTime()
const et=timeStampEnd.getTime()
const response = await axios.get(query, { params: { start:st, end:et} });
// If response was successfull
if(response != null){
// Parse response into JSON object
const json = response.data
// console.log( json)
// The code to access the data base will go here
// console.log(json)
// console.log(json)
// This will be the string containing the data we will write to the data base
let influxData = "";
// Iterate through the forecast data
for(let i = 0; i < json.data.length; i++){
// Parse the start time field into a date
const timeStamp = json.data[i].start_timestamp+"000000";
// Get the market price field and parse it into a float
let marketprice = parseFloat(json.data[i].marketprice);
// Now add the result to the data base string
influxData += "energie,quelle=awattar priceEPEXSpot="
+ marketprice
+ " "
+ timeStamp + "\n";
}
if (influxData) {
console.log(`Saving "${influxData}" to InfluxDB @ ${protocol}://${host}:${port}/`);
const resp=await axios.post(`${protocol}://${host}:${port}/api/v2/write?bucket=${bucket}&org=${org}`, influxData, {
headers: {
'Content-Type': 'text/plain',
'Authorization': `Token ${token}`
}
}).catch(err => {
console.error(err);
});
console.log(resp.data)
}
}else{
// Create an error in the console if the request failed
console.error("Request to Awattar service failed.", response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment