Skip to content

Instantly share code, notes, and snippets.

@ESDN83
Created March 11, 2022 00:18
Show Gist options
  • Save ESDN83/e7c68c4c3a5a5fbc4b506e72b5ea5653 to your computer and use it in GitHub Desktop.
Save ESDN83/e7c68c4c3a5a5fbc4b506e72b5ea5653 to your computer and use it in GitHub Desktop.
AC-ELWA-E IObroker JS script. Working with Json and can run parallel to Modbus!!
const device_IP = "192.168.1.40"
let opt_path = "mypv.0";
const url_Data = 'http://' + device_IP + '/data.jsn';
const url_Setup = 'http://' + device_IP + '/setup.jsn';
function requestData() {
const options = {
url: url_Data,
method: 'GET'
};
request(options, (error, response, body) => {
if(error) return console.log(error);
if(response.statusCode == 200) {
let array = JSON.parse(response.body);
function createOrUpdateDP(object_path,obj_id,common){
const path = object_path;
const id = path + '.' + obj_id;
//var obj;
var obj = {};
obj['type'] = 'state';
obj['common'] = common;
if(!existsState(id)) {
setObject(id, obj, function (err) {
if (err) log('Cannot write object: ' + err)
else {
var init = null;
if(common.def === undefined) {
if(common.type === 'number') init = 0;
if(common.type === 'boolean') init = false;
if(common.type === 'string') init = '';
} else init = common.def;
setState(id, init);
}
});
}else{
setState(id, common.def);
}
}
let device_val = array.device;
let fwversion_val = array.fwversion;
let mypvstatus_val = parseInt(array.status);
let power_val = parseInt(array.power);
let boostpower_val = parseInt(array.boostpower);
let temp1_val = ((parseInt(array.temp1) / 10).toFixed(1));
let ww1target_val = ((parseInt(array.ww1target) / 10).toFixed(1));
let boostactive_val = parseInt(array.boostactive);
let legboostnext_val = array.legboostnext;
let loctime_val = array.loctime;
let unixtime_val = parseInt(array.unixtime);
let ctrlstate_val = array.ctrlstate;
let blockactive_val = parseInt(array.blockactive);
let tempchip_val = parseInt(array.tempchip);
let cur_ip_val = array.cur_ip;
let cur_sn_val = array.cur_sn;
let cur_gw_val = array.cur_gw;
let cur_dns_val = array.cur_dns;
let cloudstate_val = parseInt(array.cloudstate);
createOrUpdateDP(opt_path,'1000_Device', {read: true, write: true, role: "state", name: "Device (R)", type: "string", def: device_val });
createOrUpdateDP(opt_path,'1001_Power', {read: true, write: true, role: "state", name: "Power (R)", type: "number", unit: "W", def: power_val });
createOrUpdateDP(opt_path,'1002_AktuelleWasserTemp', {read: true, write: true, role: "state", name: "Aktuelle Wasser Temp (R)", type: "string", unit: "°C", def: temp1_val });
createOrUpdateDP(opt_path,'1003_MaxWasserTemp', {read: true, write: true, role: "state", name: "Max Wasser Temp (R)", type: "string", unit: "°C", def: ww1target_val });
createOrUpdateDP(opt_path,'1005_Status', {read: true, write: true, role: "state", name: "Status (R)", type: "number", def: mypvstatus_val });
createOrUpdateDP(opt_path,'1006_HardwareTemp', {read: true, write: true, role: "state", name: "Hardware Temp (R)", type: "number", unit: "°C", def: tempchip_val });
createOrUpdateDP(opt_path,'1007_BoostState', {read: true, write: true, role: "state", name: "Boost Activate (R)", type: "number", def: boostactive_val });
createOrUpdateDP(opt_path,'1008_BoostPower', {read: true, write: true, role: "state", name: "Boost Power (R)", type: "number", def: boostpower_val });
createOrUpdateDP(opt_path,'1015_FWVersion', {read: true, write: true, role: "state", name: "E/Ethernet Version (R)", type: "string", def: fwversion_val });
createOrUpdateDP(opt_path,'1015_Legboostnext', {read: true, write: true, role: "state", name: "Next Leg-Boost in x Days (R)", type: "string", def: legboostnext_val });
createOrUpdateDP(opt_path,'1017_Loctime', {read: true, write: true, role: "state", name: "Local Time (R)", type: "string", def: loctime_val });
createOrUpdateDP(opt_path,'1018_Unixtime', {read: true, write: true, role: "state", name: "Unix Time (R)", type: "number", def: unixtime_val });
createOrUpdateDP(opt_path,'1019_Ctrlstate', {read: true, write: true, role: "state", name: "Status Ansteuerung (R)", type: "string", def: ctrlstate_val });
createOrUpdateDP(opt_path,'1020_Blockactive', {read: true, write: true, role: "state", name: "Block aktiv (R)", type: "number", def: blockactive_val });
createOrUpdateDP(opt_path,'1021_Cur_ip', {read: true, write: true, role: "state", name: "IP (R)", type: "string", def: cur_ip_val });
createOrUpdateDP(opt_path,'1022_Cur_sn', {read: true, write: true, role: "state", name: "Subnet (R)", type: "string", def: cur_sn_val });
createOrUpdateDP(opt_path,'1023_Cur_gw', {read: true, write: true, role: "state", name: "Gateway (R)", type: "string", def: cur_gw_val });
createOrUpdateDP(opt_path,'1024_Cur_dns', {read: true, write: true, role: "state", name: "DNS (R)", type: "string", def: cur_dns_val });
createOrUpdateDP(opt_path,'1025_Cloudstate', {read: true, write: true, role: "state", name: "Cloud Status (R)", type: "number", def: cloudstate_val });
}
});
};
function requestSetup() {
const options = {
url: url_Setup,
method: 'GET'
};
request(options, (error, response, body) => {
if(error) return console.log(error);
if(response.statusCode == 200) {
let array = JSON.parse(response.body);
function createOrUpdateDP(object_path,obj_id,common){
const path = object_path;
const id = path + '.' + obj_id;
//var obj;
var obj = {};
obj['type'] = 'state';
obj['common'] = common;
if(!existsState(id)) {
setObject(id, obj, function (err) {
if (err) log('Cannot write object: ' + err)
else {
var init = null;
if(common.def === undefined) {
if(common.type === 'number') init = 0;
if(common.type === 'boolean') init = false;
if(common.type === 'string') init = '';
} else init = common.def;
setState(id, init);
}
});
}else{
setState(id, common.def);
}
}
let psversion_val = array.psversion;
let tout_val = array.tout;
let bstmode_val = array.bstmode;
let bsttemp_val = array.bsttemp;
let bstton1_val = array.bstton1;
let bsttof_val = array.bsttof1;
let fuse_val = array.fuse;
createOrUpdateDP(opt_path,'1014_PSVersion', {read: true, write: true, role: "state", name: "P/Leistungsteil Version (R)", type: "number", def: psversion_val });
createOrUpdateDP(opt_path,'1004_Power_Timeout', {read: true, write: true, role: "state", name: "Power Timeout (R/W)", type: "number", def: tout_val });
createOrUpdateDP(opt_path,'1009_BoostMode', {read: true, write: true, role: "state", name: "Boost Mode (R/W)", type: "number", def: bstmode_val });
createOrUpdateDP(opt_path,'1010_BoostTemp', {read: true, write: true, role: "state", name: "Boost Temp (R/W)", type: "number", unit: "°C", def: bsttemp_val });
createOrUpdateDP(opt_path,'1011_BoostStartHour', {read: true, write: true, role: "state", name: "Boost Start Hour (R/W)", type: "number", def: bstton1_val });
createOrUpdateDP(opt_path,'1012_BoosStopHour', {read: true, write: true, role: "state", name: "Boost Stop Hour (R/W)", type: "number", def: bsttof_val });
createOrUpdateDP(opt_path,'1013_Fuse', {read: true, write: true, role: "state", name: "Fuse_Type 16/13 (R/W)", type: "number", def: fuse_val });
}
});
};
schedule("*/10 * * * * *", function () {
requestData();
requestSetup();
});
function updateELWA(newStateVal,sourceprop,sourceURL) {
let requestURL = sourceURL + '?' + sourceprop + '=' + newStateVal;
const options = {url: requestURL,method: 'GET'}
request(options, (error, response) => {
if(error) return console.log(error);
if(response.statusCode == 200) return console.log('Value updated',sourceprop,newStateVal);
});
};
on({id:(opt_path + '.1004_Power_Timeout'),change:'any',ack:true}, function(obj) {updateELWA(obj.state.val,'tout',url_Setup)});
on({id:(opt_path + '.1009_BoostMode'),change:'any',ack:true}, function(obj) {updateELWA(obj.state.val,'bstmode',url_Setup)});
on({id:(opt_path + '.1010_BoostTemp'),change:'any',ack:true}, function(obj) {updateELWA(obj.state.val,'bsttemp',url_Setup)});
on({id:(opt_path + '.1011_BoostStartHour'),change:'any',ack:true}, function(obj) {updateELWA(obj.state.val,'bstton1',url_Setup)});
on({id:(opt_path + '.1012_BoosStopHour'),change:'any',ack:true}, function(obj) {updateELWA(obj.state.val,'bsttof',url_Setup)});
on({id:(opt_path + '.1013_Fuse'),change:'any',ack:true}, function(obj) {updateELWA(obj.state.val,'fuse',url_Setup)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment