NxPanel - Replacement Firmware for Sonoff NSPanel

// Enum
const PAGE_HOME            = 1
const PAGE_2_BUTTON        = 2
const PAGE_3_BUTTON        = 3
const PAGE_4_BUTTON        = 4
const PAGE_6_BUTTON        = 5
const PAGE_8_BUTTON        = 6
const PAGE_DIMMER          = 7
const PAGE_DIMMER_COLOR    = 8
const PAGE_THERMOSTAT      = 9
const PAGE_ALERT_1         = 10
const PAGE_ALERT_2         = 11
const PAGE_ALARM           = 12
const PAGE_MEDIA           = 13
const PAGE_PLAYLIST        = 14
const PAGE_STATUS          = 15

const BUTTON_UNUSED        = 0
const BUTTON_TOGGLE        = 1
const BUTTON_PUSH          = 2
const BUTTON_DIMMER        = 3
const BUTTON_DIMMER_COLOR  = 4
const BUTTON_PAGE          = 10

const ICON_BLANK           = 0 
const ICON_BULB            = 1 
const ICON_DIMMER          = 2 
const ICON_DIMMER_COLOR    = 3
const ICON_VACUUM          = 4 
const ICON_BED             = 5 
const ICON_HOUSE           = 6 
const ICON_SOFA            = 7 
const ICON_BELL            = 8 
const ICON_HEAT            = 9
const ICON_CURTAINS        = 10 
const ICON_MUSIC           = 11
const ICON_BINARY          = 12 
const ICON_FAN             = 13
const ICON_SWITCH          = 14
const ICON_TALK            = 15
const ICON_INFO            = 16

const NONE                 = 0
// End Enum

function makeButton(bid,label,type,icon,state,next){
  let json = JSON.parse("{}");
  json["bid"] = bid;
  json["label"] = label;
  json["type"] = type; 
  if (typeof next != "undefined"){
    json["next"] = next;
  }
  if (typeof state != "undefined"){
    json["state"] = state;
  }
  if (typeof icon != "undefined"){
    json["icon"] = icon;
  } 
  let stringJson = JSON.stringify(json)
  if (bid > 1){
    stringJson = "," + stringJson;
  }
  return stringJson;
}

function makePage(pid,name,format){
  let json = JSON.parse('{"refresh": {}}');
  json["refresh"]["pid"] = pid;
  json["refresh"]["name"] = name;
  let stringJson = JSON.stringify(json)
  stringJson = stringJson.substr(0,stringJson.length - 2) + `,${format}buttons:[`
  stringJson = stringJson.replace('"refresh":"",','"refresh":{');
  return stringJson
}

function makeEmptySync(pid){
  let json = JSON.parse("{}");
  json["sync"]["pid"] = pid;
  return JSON.stringify(json);
}

function makeEmptyRefresh(pid){
  let json = JSON.parse("{}");
  json["refresh"]["pid"] = pid;
  return JSON.stringify(json);
}

function makeSyncButtonStart(pid,bid,state){
  let json = JSON.parse('{"sync":{"pid": 5}}');
  json["sync"]["pid"] = pid;
  stringJson = JSON.stringify(json);
  stringJson = stringJson.substr(0,stringJson.length - 2);
  stringJson = stringJson + `,buttons:[{"bid":${bid},"state":${state}}`;
  return stringJson;
}

function addSyncButton(bid,state) {
  return `,{"bid":${bid},"state":${state}}`;
}

//var str = `this event has been triggered by {"page": {"format": 6, "pid": 10, "type": "refresh"}}`
var str = this.event.toString();

var eventJSON = JSON.parse(str.substr(str.indexOf("{"),str.length()));
var pid = parseInt(eventJSON["page"]["pid"],10);
var format = eventJSON["page"]["format"].toString();
var refresh = str.indexOf("refresh")>0;
var messageJson

console.log("Updating NxPanel page: " + pid.toString());

const PANEL_MAIN          = 10
const PANEL_BEDROOM_1     = 11
const PANEL_BEDROOM_2     = 12
const PANEL_LOUNGE        = 13
const PANEL_CABIN         = 14
const PANEL_CABIN_THERMO  = 15
const PANEL_CABIN_LIGHTS  = 16
const PANEL_LOUNGE_FAN    = 17
const PANEL_LOUNGE_LIGHT  = 18
const PANEL_STATUS        = 19
const PANEL_MUSIC         = 20

switch(pid){
  case PANEL_MAIN:
    let movie_state = 1
    let lounge_state = 0
    let cabin_state = 0
    let hall_light_state = 1
     if (refresh) {
      messageJson = makePage(PANEL_MAIN,"Lounge",PAGE_8_BUTTON);
      messageJson = messageJson + makeButton(1,"Movie",BUTTON_TOGGLE,ICON_BULB,movie_state);
      messageJson = messageJson + makeButton(2,"Lounge",BUTTON_TOGGLE,ICON_BULB,lounge_state);
      messageJson = messageJson + makeButton(3,"Hall",BUTTON_PUSH,ICON_HOUSE);
      messageJson = messageJson + makeButton(4,"Bedroom",BUTTON_PAGE,ICON_BED,PAGE_6_BUTTON,PANEL_BEDROOM_1);
      messageJson = messageJson + makeButton(5,"Temp",BUTTON_PAGE,ICON_HEAT,PAGE_THERMOSTAT,PANEL_CABIN_THERMO);
      messageJson = messageJson + makeButton(6,"Light",BUTTON_DIMMER,ICON_DIMMER,hall_light_state,PANEL_LOUNGE_LIGHT);
      messageJson = messageJson + makeButton(7,"Dimmer",BUTTON_DIMMER_COLOR,ICON_DIMMER_COLOR,cabin_state,PANEL_CABIN_LIGHTS);
      messageJson = messageJson + makeButton(8,"Status",BUTTON_PAGE,ICON_INFO,PAGE_STATUS,PANEL_STATUS);
      messageJson = messageJson + "]}}";
    } else {
      messageJson = makeSyncButtonStart(10,1,1);
      messageJson = messageJson + addSyncButton(2,1);
      messageJson = messageJson + "]}}";
    }
    //console.log(messageJson);
    items.getItem("nxpanel_command").sendCommand(messageJson);
    break  
}

It’s more readable instead of line efficient, but it does give me the same outputs for sync and refresh of pages.
EDIT: Note this is in emcascript 2021, sorry OH 2.0 users
EDIT2: Updated script to work with the latest nxpanel version as of 2/20/2022 (american dating)
EDIT3: There are many options besides an nspanel, if you don’t have one yet i recommend looking at other options like the lanbon l8 with openhasp

3 Likes