I manage to get the data value I need to work. The code sucks because I don’t know how to code, so I basically erase what I don’t needed and changed the sensor information.
This is the code: (removed token and HA ip)
let widget = await createWidget();
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
async function createWidget(items) {
/* Get data from API */
let req = new Request("http://192.168.xxx.xxx:8123/api/states")
req.headers = { "Authorization": "Bearer <token>", "content-type": "application/json" }
let json = await req.loadJSON();
/* Parse data received from API */
let data = {energia: {}}
data.energia = addData(json, data.energia, ['sensor.iammeter_power']);
/* Create the widget */
const widget = new ListWidget();
widget.backgroundColor = new Color("#03a9f4", 1.0);
/* Design the widget header */
let headerStack = widget.addStack();
const logoStack = headerStack.addStack();
headerStack.addSpacer(2);
const titleStack = headerStack.addStack();
headerStack.addSpacer(7);
const tempImageStack = headerStack.addStack();
headerStack.addSpacer(14);
const humidImageStack = headerStack.addStack();
widget.addSpacer(5)
/* Add the sensor entries */
const bodyStack = widget.addStack();
/* First, the label column */
const labelStack = bodyStack.addStack();
labelStack.setPadding(0, 0, 0, 0);
labelStack.borderWidth = 0;
labelStack.layoutVertically();
addLabel(labelStack, " Energia:")
/* Second, the temperature column */
const tempStack = bodyStack.addStack();
tempStack.setPadding(0, 3, 0, 0);
tempStack.borderWidth = 0;
tempStack.layoutVertically();
addTemp(tempStack, data.energia)
/* Done: Widget is now ready to be displayed */
return widget;
}
/* Adds the entries to the label column */
async function addLabel(labelStack, label) {
const mytext = labelStack.addText(label);
mytext.font = Font.semiboldSystemFont(10);
mytext.textColor = Color.black();
}
/* Adds the entries to the temperature column */
async function addTemp(tempStack, data) {
const mytext = tempStack.addText(data.temp + "W");
mytext.font = Font.heavyMonospacedSystemFont(10);
mytext.textColor = Color.white();
}
/* Adds the entries to the humidity column */
async function addHumid(humidStack, data) {
const mytext = humidStack.addText("(" + data.humid + "%)");
mytext.font = Font.mediumMonospacedSystemFont(10);
mytext.textColor = Color.white();
mytext.textOpacity = 0.8;
}
/* Searches for the respective sensor values ('state') in the API response of Home Assistant */
function addData(json, room, sensors) {
room.temp = "N/A";
room.humid = "N/A";
var i;
for (i = 0; i < json.length; i++) {
if (json[i]['entity_id'] == sensors[0]) {
room.temp = Math.round(json[i]['state']);
}
if (json[i]['entity_id'] == sensors[1]) {
room.humid = Math.round(json[i]['state']);
}
}
return room;
}
I wanted to do something similar to this, essentially a gauge from 0-5000 with changing colours between values, is it to hard to do?