LaMetric SKY Display: Local HTTP Push

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:

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.

1 Like

Hello there again,

I created a second script, this time it is for sending notifications to the SKY device.
The syntax is similar to the script above, but the variables are a bit different and the options are a bit more limited.

The script knows only three types, as listed here: Notifications | LaMetric API v2.3.0 documentation

  • icon
  • icongoal
  • spike

You need also these two types:

  • priority (must be of the following: info|warning|critical)
  • icontype (must be one these: none|info|alert)
  • cycles (times the notification is repeated)

Here is an example input string:

sky_info_none_3_1_icon_298_test

And here comes the full script:

let deviceip = new String;
let apikey = new String;

// Set API-notification-URL
let apiPart = ":8080/api/v2/device/notifications";

// Split input
let input = args[0];
let array = input.split("_");

// Set device
if (array[0] === "YOURDEVICE1") {
  deviceip = "LOCALIPOFDEVICE1";
  apikey = "DEVICE1-API-KEY";
}
if (array[0] === "YOURDEVICE2") {
  deviceip = "LOCALIPOFDEVICE2";
  apikey = "DEVICE2-API-KEY";
}

// Set priority, must be one of info|warning|critical
let priority = array[1];

// Set icon type, must be one of none|info|alert
let icontype = array[2];

// Set cycles
let cycles = array[3];

// Set number of frames
let frameAnzahl = array[4];
let frameHop = 5;
let frames = "\{\"priority\":\"" + priority + "\",\"icon_type\":\"" + icontype + "\",\"lifeTime\":1000,\"model\": \{\"frames\":\[";

// Set frame-type
for (let i = 0; i < frameAnzahl; i++) {
  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] == "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 + "\],\"cycles\":" + cycles "+\}\}";

let url = (deviceip + apiPart);
  
fetch(url, {
    method: 'POST',
    headers: {
        'Authorization': 'Basic ' + Buffer.from("dev:" + apikey).toString('base64'),
        "Content-Type": "application/json"
    },
    body: frames
});

Of course both scripts work on every device, that uses javscript, not just Homey.