Created
October 24, 2023 21:17
-
-
Save K4CZP3R/d35fd3dc074fed31aa4c152251bfa8d4 to your computer and use it in GitHub Desktop.
Scriptable Home Assistant widget which shows entity state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Scriptable HomeAssistant Widget | |
const hassUrl = "https://.....com"; | |
const hassToken = | |
"eyJhbGciOi...."; | |
const sensors = { | |
"sensor.ewelink_th01_woonkamer_temperature_2": "Woonkamer", | |
"sensor.ewelink_th01_kacper_temperature_2": "Kacper", | |
}; | |
async function getHomeAssistantStates() { | |
let req = new Request(`${hassUrl}/api/states`); | |
req.headers = { | |
Authorization: `Bearer ${hassToken}`, | |
"Content-Type": "application/json", | |
}; | |
let json = await req.loadJSON(); | |
let statesToReturn = {}; | |
for (let sensor in sensors) { | |
let sensorState = json.find((s) => s.entity_id === sensor); | |
if (sensorState) { | |
statesToReturn[sensors[sensor]] = sensorState.state; | |
} | |
} | |
return statesToReturn; | |
} | |
let widget = new ListWidget(); // | |
async function createWidget() { | |
let states = await getHomeAssistantStates(); | |
console.log(states); | |
let stack = widget.addStack(); | |
stack.layoutVertically(); | |
let titleText = widget.addText("Home Assistant"); | |
titleText.font = Font.boldSystemFont(15); | |
widget.addSpacer(1); | |
for (let sensor in states) { | |
let stateText = widget.addText(`${sensor} ${states[sensor]} °C`); | |
// not bold | |
stateText.font = Font.systemFont(12); | |
} | |
// let updateText = widget.addText(new Date().toLocaleTimeString()); | |
// updateText.font = Font.systemFont(10); | |
Script.setWidget(widget); | |
Script.complete(); | |
} | |
await createWidget(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment