Skip to content

Instantly share code, notes, and snippets.

@bthompson00
Last active April 25, 2023 04:05
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 bthompson00/6e4dd82cb935715dfd77b36cf41dc5af to your computer and use it in GitHub Desktop.
Save bthompson00/6e4dd82cb935715dfd77b36cf41dc5af to your computer and use it in GitHub Desktop.
Simple Amber Electric Widget
//Use this code in the Scriptable app to create a widget for Amber Electric
// To get the following parameters, login to https://app.amber.com.au/developers/ and enable the developer panel in Settings. Once in the developer panel, click "Generate a new Token" and type in "Amber Price Widget" when prompted. Copy the generated token, and paste it below. Back in the developer panel, in the section below, click the green "Authorize" button and again paste your token. Click Authorize and then Close. In the /Sites section, click "Try it out" and then "Execute". You must have previously followed the instructions to Authorize for this to work. In the Server Response section, the first parameter shown will be you id. Copy it and paste it below.
// Set this to your API Token. Demo token shown:
let apiToken = "psk_b736264848babbdj8373628ac938e849";
// Change this to your site id. Demo ID shown:
let siteId = "01GWKFHNFQN5XGKSBXVJS30VW2";
/*//////////////////////////////////////////////////////////////////////
Rest of this doesn't need to be changed unless you want to get your hands dirty
//////////////////////////////////////////////////////////////////////*/
// API call to get prices for the provided site
let req = new Request("https://api.amber.com.au/v1/sites/"+siteId+"/prices/current?next=1&resolution=30");
req.method = 'GET';
req.headers = {'accept': 'application/json','Authorization': 'Bearer '+apiToken};
// Initialise variables. These will be set based on Amber API
let data = await req.loadJSON();
var currentPrice = data[0].perKwh;
var currentRenewables = data[0].renewables;
var currentDescriptor = data[0].descriptor;
var nextPrice = data[1].perKwh;
currentPrice = (currentPrice/100).toFixed(2);
currentRenewables = Math.round(currentRenewables);
nextPrice = (nextPrice/100).toFixed(2);
if (config.runsInWidget) {
let widget = createWidget();
Script.setWidget(widget);
Script.complete();
} else {
QuickLook.present(createWidget());
}
function createWidget() {
let w = new ListWidget();
// Set background color to green / yellow / orange / red based on current rate.
let bgColor = "";
switch (currentDescriptor) {
case "negative":
case "extremelyLow":
case "veryLow":
bgColor = "00E3A0";
break;
case "low":
bgColor = "FFC624";
break;
case "neutral":
bgColor = "FB8C0F";
break;
case "high":
case "spike":
bgColor = "DC2D20";
break;
}
w.backgroundColor = new Color(bgColor);
// Add 'AMBER' heading
let header = w.addText('AMBER ELECTRICITY');
header.textColor = Color.white();
header.textOpacity = 0.8;
// Add 'Current price' in 22pt font
let bodyText = w.addText("$" + currentPrice + "/kwh");
bodyText.font = Font.mediumRoundedSystemFont(22);
bodyText.textColor = Color.white();
// Add 'Next price' in smaller 14pt font
let nextText = w.addText("Next $" + nextPrice + "/kwh");
nextText.font = Font.mediumRoundedSystemFont(14);
nextText.textColor = Color.white();
// Add '% Renewables' annotation in 14pt font, lower opacity
let renewableText = w.addText(currentRenewables + "% renewables");
renewableText.font = Font.mediumRoundedSystemFont(14);
renewableText.textColor = Color.white();
renewableText.textOpacity = 0.8;
// Add 'last updated' date to footer
var nowDate = new Date();
var nowFormDate = nowDate.toLocaleTimeString('en-AU', { hour: "2-digit", minute: "2-digit" });
let footer = w.addText("as at " + nowFormDate);
footer.font = Font.footnote();
footer.textColor = Color.white();
footer.textOpacity = 0.8;
return w;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment