Hello there,
I created a homeyscript, which can be called with an argument and then displays anything on a DIY-instance on your LaMetric device and thought it might be nice to share it here.
The input string must look like this, every new variable is divided by “_”:
device_widget_framecount_contenttype_content
So for example a final string looks like
time_diy_2_text_hello world_icon_99_goodbye
The script knows the five content types written down here: My Data DIY : Support
- text: just plain text, one variable (x)
- icon: icon & text, two variables (x_x)
- simplegoal: goal structure from link above, four variables (x_x_x_x)
- icongoal: icon & goal structure, five variables (x_x_x_x_x)
- spike: spike structure from link above, one variable, numbers seperated by commas (x,y,z)
You will need the following:
- LOCALIPOFDEVICE: IP of LaMetric device in your network
- LOCAL-API-KEY: the API-Key of the device: Authorization | LaMetric API v2.3.0 documentation
- WIDGET-CODE: the last part of the URL given in the App-settings
The strings for YOURDEVICE and WIDGETNAMES can be chosen freely, they are just your personal identifiers.
All icons can be found here, you will need the numbers of the chosen icon only: LaMetric: Web
let deviceip = new String;
let apikey = new String;
let widget = new String;
// set API-part of URL
let apiPart = ":8080/api/v2/widget/update/com.lametric.diy.devwidget/";
// Split input
let input = args[0];
let array = input.split("_");
// set device
if (array[0] === "YOURDEVICE1") {
deviceip = "http://LOCALIPOFDEVICE1";
apikey = "DEVICE-API-KEY1";
}
if (array[0] === "YOURDEVICE2") {
deviceip = "http://LOCALIPOFDEVICE2";
apikey = "DEVICE-API-KEY2";
}
// set widget
if (array[1].includes("WIDGETNAME1")) {
widget = "WIDGET-CODE1";
}
if (array[1].includes("WIDGETNAME2")) {
widget = "WIDGET-CODE2";
}
// set number of frames
let frameAnzahl = array[2];
let frameHop = 3;
let frames = "\{\"frames\":\[";
// create frames
for (let i = 0; i < frameAnzahl; i++) {
if (array[frameHop] == "text") {
frames = frames + "\{\"text\":\"" + array[frameHop + 1] +"\"\}";
frameHop = frameHop + 2;
if (frameHop < array.length) {
frames = frames + ",";
}
}
if (array[frameHop] == "icon") {
frames = frames + "\{\"icon\":\"" + array[frameHop + 1] +"\",\"text\":\"" + array[frameHop + 2] +"\"\}";
frameHop = frameHop + 3;
if (frameHop < array.length) {
frames = frames + ",";
}
}
if (array[frameHop] == "simplegoal") {
frames = frames + "\{\"goalData\":\{\"start\":" + array[frameHop + 1] + ",\"current\":" + array[frameHop + 2] + ",\"end\":" + array[frameHop + 3] + ",\"unit\":\"" + array[frameHop + 4] + "\"\}\}";
frameHop = frameHop + 5;
if (frameHop < array.length) {
frames = frames + ",";
}
}
if (array[frameHop] == "icongoal") {
frames = frames + "\{\"icon\":\""+ array[frameHop + 1] + "\",\"goalData\":\{\"start\":" + array[frameHop + 2] + ",\"current\":" + array[frameHop + 3] + ",\"end\":" + array[frameHop + 4] + ",\"unit\":\"" + array[frameHop + 5] + "\"\}\}";
frameHop = frameHop + 6;
if (frameHop < array.length) {
frames = frames + ",";
}
}
if (array[frameHop] == "spike") {
frames = frames + "\{\"chartData\":\[" + array[frameHop + 1] +"\]\}";
frameHop = frameHop + 2;
if (frameHop < array.length) {
frames = frames + ",";
}
}
}
frames = frames + "\]\}";
let url = (deviceip + apiPart + widget);
fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from("dev:" + apikey).toString('base64'),
"Content-Type": "application/json"
},
body: frames
});
Please feel free to ask anything, I will be happy to help.