/**************************************************************
Github - https://github.com/xCruziX/ioBroker-Creating-Alias/blob/master/CreateAlias.js
Changelog
Version 1.1.4
- fix error by using bCreateAliasPath and bConvertExistingPath (has no target 9/Error creating alias-path)
**************************************************************/
/**************************************
Flags /
Variablen
***************************************/
// typeAlias = 'boolean'; // oder 'number'
// read = "val == 'Ein' ? true : false"; // Erkennung "Aus" --> false erfolgt automatisch
// write = "val ? 'Ein' : 'Aus'";
// nameAlias = 'Licht Haustür';
// role = 'switch';
// desc = 'per Script erstellt';
// min = 0; // nur Zahlen
// max = 100; // nur Zahlen
// unit = '%'; // nur für Zahlen
// states = {0: 'Aus', 1: 'Auto', 2: 'Ein'}; // Zahlen (Multistate) oder Logikwert (z.B. Aus/Ein)
let bCreateAliasPath = true; // If this flag is true, each folder is created seperately so rooms and functions can be assigned.
/*
Requirements: bCreateAliasPath == true
If this flag is true, existing folders in the path will be converted so rooms and functions can be assigned.
*/
let bConvertExistingPath = false;
/***************************************
Don't change anything from here /
Ab hier nichts verändern
***************************************/
let arEnum = [];
let arId = [];
let timeoutAssignEnum;
var callbackAlias = undefined;
function createAlias(idSrc, idDst,raum, gewerk,typeAlias, read, write, nameAlias, role, desc, min, max, unit, states) {
if(!idDst.includes('alias.0.'))
idDst = 'alias.0.' + idDst;
if(!existsObject(idSrc))
{
log('Source-Id ' + idSrc +' does not exists.','warn');
return;
}
// Create the object Path for alias id,
// so you can assign rooms and function to the parents
var createAliasPath = (id) => {
if(bCreateAliasPath){
let lisMergedIds = [];
let mergedId = 'alias.0';
id = id.replace(mergedId + '.', ''); // Remove prefix alias so it will not be changed
let split = id.split('.');
for(let i=0;i<split.length-1;i++){
mergedId += '.' + split[i];
lisMergedIds.push(mergedId);
}
function path(){
if(lisMergedIds.length == 0) {
alias();
return;
}
let tmpId = lisMergedIds[0];
lisMergedIds.splice(0,1); // remove element
if(!existsObject(tmpId) || bConvertExistingPath){ // not exists
let obj;
if(existsObject(tmpId))
obj = getObject(tmpId);
else
obj = {};
let bApply = false;
if(obj != undefined){
if(obj.type == undefined || String(obj.type) != 'meta'){
obj.type = 'meta';
bApply = true;
}
if(obj.common == undefined){
obj.common = {};
obj.common.type = 'meta.folder';
bApply = true;
}
else if(obj.common.type == undefined || String(obj.common.type) != 'meta.folder'){
obj.common.type = 'meta.folder';
bApply = true;
}
if(obj.native == undefined){
obj.native = {};
bApply = true;
}
}
else{
path();
log('Object is undefined');
return;
}
if(bApply){
setObject(tmpId, obj, (err) =>{
if(!err){
log('Created Alias-Path ' + tmpId);
path();
}
else
log('Error creating alias-path','error');
});
}
else
path();
}
}
path();
}
else
alias();
}
function alias(){
// Create alias object
if(!existsObject(idDst)){
let obj = {};
obj.type = 'state';
obj.common = getObject(idSrc).common;
obj.common.alias = {};
obj.common.alias.id = idSrc;
if(typeAlias !== undefined)
obj.common.type = typeAlias;
if(obj.common.read !== undefined)
obj.common.alias.read = read;
if(obj.common.write !== undefined)
obj.common.alias.write = write;
if(nameAlias !== undefined)
obj.common.name = nameAlias;
if(role !== undefined)
obj.common.role = role;
if(desc !== undefined)
obj.common.desc = desc;
if(min !== undefined)
obj.common.min = min;
if(max !== undefined)
obj.common.max = max;
if(unit !== undefined)
obj.common.unit = unit;
if(states !== undefined)
obj.common.states = states;
obj.native = {};
obj.common.custom = []; // Damit die Zuordnung zu iQontrol, Sql etc. nicht übernommen wird
log('Created Alias-State ' + idDst);
setObject(idDst, obj,(err) =>{
if(!err)
startAttach();
else
log('Error creating-alias','error');
});
}
else
startAttach();
}
// Save ID and Enum (room or function)
var attach = (id, enu,value) => {
if(id.length == 0){
log('ID has lenght 0, can not attach to enum','warn');
return;
}
if(value.length == 0){
log('Value has lenght 0','warn');
return;
}
let sEnuId = 'enum.' + enu + '.' + value;
if(enu.length > 0 && existsObject(sEnuId))
{
let obj = getObject(sEnuId)
let members = obj.common.members;
if(!members.includes(id)){
arEnum.push(sEnuId);
arId.push(id);
}
}
else
log('Can not find enum ' + sEnuId,'warn');
}
function startAttach(){
let bRoom = raum !== undefined && raum.length > 0;
let bGewerk = gewerk !== undefined && gewerk.length > 0;
if(bRoom)
attach(idDst,'rooms',raum);
if(bGewerk)
attach(idDst,'functions',gewerk);
if(bRoom || bGewerk){
if(timeoutAssignEnum){
clearTimeout(timeoutAssignEnum);
timeoutAssignEnum = null;
}
timeoutAssignEnum = setTimeout(finishScript,100);
}
}
createAliasPath(idDst);
}
function finishScript(){
assignEnums();
}
// Add the saved IDs to the rooms/functions
function assignEnums(){
if(arEnum.length == 0 || arId.length == 0){
return;
}
if(arEnum.length != arId.length){
log('Arrays have different size','error');
return;
}
let mapEnumId = new Map();
for(let i=0;i < arEnum.length; i++){
let enu = arEnum[i];
let id = arId[i];
if(existsObject(id)){
let obj = getObject(enu)
let members;
if(!mapEnumId.has(enu)){
members = obj.common.members;
mapEnumId.set(enu,members);
}
else
members = mapEnumId.get(enu);
if(!members.includes(id)){
log("Adding " + id + " to " + enu);
members.push(id);
}
}
else
log('Can not find Alias ' + id,'error');
}
function setMembers(members,enu,map){
let obj = getObject(enu);
obj.common.members = members;
setObject(enu,obj);
}
mapEnumId.forEach(setMembers);
}
/**********************************************************
END /
ENDE
**********************************************************/
//////////////////////////////
// Fenster
//////////////////////////////
//2 x Fenstersensoren HMIP
//einfache HMIP Fenstersensoren über rpc-Adapter in function fenster_alias
//{0: 'geschlossen', 1: 'geöffnet'}
createAlias('hm-rpc.0.xxx.1.STATE'/*Wohnzimmer Fenster links STATE*/, 'Fenster.Wohnzimmer_Fenster_links', undefined, 'fenster_alias', 'number', undefined, undefined, 'Wohnzimmer links', 'window.value', 'per Script erstellt', 0, 1, '',{0: 'geschlossen', 1: 'geöffnet'})
createAlias('hm-rpc.0.xxx.1.STATE'/*Küche Fenster links STATE*/, 'Fenster.Kueche_Fenster_links', undefined, 'fenster_alias', 'number',undefined, undefined, 'Küche links', 'window.value', 'per Script erstellt', 0, 1, '',{0: 'geschlossen', 1: 'geöffnet'})
//2 x Fenstersensoren HM
//einfache HM Fenstersensoren über rpc-Adapter in function fenster_alias
//{false: 'geschlossen', true: 'geöffnet'}
createAlias('hm-rpc.0.xxx.1.STATE'/*Wohnzimmer Fenster links STATE*/, 'Fenster.Wohnzimmer_Fenster_links', undefined, 'fenster_alias', 'boolean', undefined, undefined, 'Wohnzimmer links', 'window.value', 'per Script erstellt', undefined, undefined, undefined, {false: 'geschlossen', true: 'geöffnet'})
createAlias('hm-rpc.0.xxx.1.STATE'/*Küche Fenster links STATE*/, 'Fenster.Kueche_Fenster_links', undefined, 'fenster_alias', 'boolean',undefined, undefined, 'Küche links', 'window.value', 'per Script erstellt', undefined, undefined, undefined, {false: 'geschlossen', true: 'geöffnet'})
//2 x Fensterdrehgriffe HMIP
//HMIP Fensterdrehgriffe über rpc-Adapter in function fenster_alias
//{0: 'geschlossen', 1: 'gekippt', 2: 'geöffnet'}
createAlias('hm-rpc.0.xxx.1.STATE'/*Keller W-Baum Fenster STATE*/, 'Fenster.Keller_Weihnachtsbaum_Fenster', undefined, 'fenster_alias', 'number',undefined, undefined, 'Keller W Baum', 'window.value', 'per Script erstellt', 0, 2, '',{0: 'geschlossen', 1: 'gekippt', 2: 'geöffnet'})
createAlias('hm-rpc.0.xxx.1.STATE'/*Keller Wohnzimmer Fenster mitte STATE*/, 'Fenster.Keller_Wohnzimmer_Fenster_mitte', undefined, 'fenster_alias', 'number',undefined, undefined, 'Keller Wohnzimmer mitte', 'window.value', 'per Script erstellt', 0, 2, '',{0: 'geschlossen', 1: 'gekippt', 2: 'geöffnet'})
//2 x Fenster(Türen) Sensoren HMIP
//einfache HMIP Fenstersensoren über rpc-Adapter in function tueren_alias
//{0: 'geschlossen', 1: 'geöffnet'}
createAlias('hm-rpc.0.xxx.1.STATE'/*Wohnzimmer Terrassentür STATE*/, 'Tueren.Terrassentuer', undefined, 'tueren_alias', 'number',undefined, undefined, 'Terrassentür', 'window.value', 'per Script erstellt', 0, 1, '',{0: 'geschlossen', 1: 'geöffnet'})
createAlias('hm-rpc.0.xxx.1.STATE'/*Haustür STATE*/, 'Tueren.Haustuer', undefined, 'tueren_alias', 'number',undefined, undefined, 'Haustür', 'window.value', 'per Script erstellt', 0, 1, '',{0: 'geschlossen', 1: 'geöffnet'})
//////////////////////////////
// Steckdosen
//////////////////////////////
//2 x Steckdosen
//HMIP Steckdosen über rpc-Adapter in function steckdosen_alias
//{false: 'aus', true: 'an'}
createAlias('hm-rpc.0.xxx.3.STATE'/*Waschmaschine STATE*/, 'Steckdosen.Waschmaschine', undefined, 'steckdosen_alias', 'boolean', undefined, undefined, 'Waschmaschine', 'switch', 'per Script erstellt', undefined, undefined, undefined, {false: 'aus', true: 'an'})
createAlias('hm-rpc.0.xxx.3.STATE'/*Geschirrspueler STATE*/, 'Steckdosen.Geschirrspueler', undefined, 'steckdosen_alias', 'boolean', undefined, undefined, 'Geschirrspüler', 'switch', 'per Script erstellt', undefined, undefined, undefined, {false: 'aus', true: 'an'})
//////////////////////////////
// Bewegungsmelder
//////////////////////////////
//2 x Bewegungsmelder und 2 x Präsenzmelder und 1 x Erschütterungssensor
//HMIP Bewegungsmelder und Präsenzmelder über rpc-Adapter in function bewegungsmelder_alias
//{false: 'inaktiv', true: 'aktiv'}
createAlias('hm-rpc.0.xxx.1.MOTION'/*Bewegungsmelder Weihnachtsbaumkeller MOTION*/, 'Bewegungsmelder.Bewegungsmelder_Weihnachtsbaumkeller', undefined, 'bewegungsmelder_alias', 'boolean', undefined, undefined, 'W Baum Keller', 'sensor.motion', 'per Script erstellt', undefined, undefined, undefined, {false: 'inaktiv', true: 'aktiv'})
createAlias('hm-rpc.0.xxx.1.MOTION'/*Bewegungsmelder Heizungskeller MOTION*/, 'Bewegungsmelder.Bewegungsmelder_Heizungskeller', undefined, 'bewegungsmelder_alias', 'boolean', undefined, undefined, 'Heizungskeller', 'sensor.motion', 'per Script erstellt', undefined, undefined, undefined, {false: 'inaktiv', true: 'aktiv'})
//ab hier 2 x Präsenzmelder
createAlias('hm-rpc.0.xxx.1.PRESENCE_DETECTION_STATE'/*Präsenzmelder Badezimmer PRESENCE DETECTION STATE*/, 'Bewegungsmelder.Praesenzmelder_Badezimmer', undefined, 'bewegungsmelder_alias', 'boolean', undefined, undefined, 'Badezimmer', 'sensor.motion', 'per Script erstellt', undefined, undefined, undefined, {false: 'inaktiv', true: 'aktiv'})
createAlias('hm-rpc.0.xxx.1.PRESENCE_DETECTION_STATE'/*Präsenzmelder Dusche PRESENCE DETECTION STATE*/, 'Bewegungsmelder.Praesenzmelder_Dusche', undefined, 'bewegungsmelder_alias', 'boolean', undefined, undefined, 'Dusche', 'sensor.motion', 'per Script erstellt', undefined, undefined, undefined, {false: 'inaktiv', true: 'aktiv'})
//ab hier 1 x Erschütterungssensor
createAlias('hm-rpc.0.xxx.1.MOTION'/*Briefkasten MOTION*/, 'Bewegungsmelder.Praesenzmelder_Briefkasten', undefined, 'bewegungsmelder_alias', 'boolean', undefined, undefined, 'Briefkasten', 'sensor.motion', 'per Script erstellt', undefined, undefined, undefined, {false: 'inaktiv', true: 'aktiv'})
//////////////////////////////
// Licht
//////////////////////////////
//2 x HUE und 5 x HMIP Licht über HUE und rpc-Adapter in function licht_alias
//{false: 'aus', true: 'an'}
createAlias('hue.0.Badezimmerlampe_links.on'/*Badezimmerlampe links on*/, 'Licht.Badezimmer_Licht_links', undefined, 'licht_alias', 'boolean', undefined, undefined, 'Badezimmer links', 'switch.light', 'per Script erstellt', undefined, undefined, undefined, {false: 'aus', true: 'an'})
createAlias('hue.0.Badezimmerlampe_rechts.on'/*Badezimmerlampe rechts on*/, 'Licht.Badezimmer_Licht_rechts', undefined, 'licht_alias', 'boolean', undefined, undefined, 'Badezimmer rechts', 'switch.light', 'per Script erstellt', undefined, undefined, undefined, {false: 'aus', true: 'an'})
//ab hier 2 x Licht HMIP Steckdosen über rpc Adapter
createAlias('hm-rpc.0.xxx.3.STATE'/*Kugellampe STATE*/, 'Licht.Wohnzimmer_Kugel_Licht', undefined, 'licht_alias', 'boolean', undefined, undefined, 'Kugellicht', 'switch.light', 'per Script erstellt', undefined, undefined, undefined, {false: 'aus', true: 'an'})
createAlias('hm-rpc.0.xxx.3.STATE'/*Gäste WC Licht STATE*/, 'Licht.Gaeste_WC_Licht', undefined, 'licht_alias', 'boolean', undefined, undefined, 'Gäste WC Licht', 'switch.light', 'per Script erstellt', undefined, undefined, undefined, {false: 'aus', true: 'an'})
//////////////////////////////
// Temperaturen
//////////////////////////////
//HMIP 2 x Temperaturen über rpc-Adapter in function temperaturen_alias
createAlias('hm-rpc.0.xxx.1.ACTUAL_TEMPERATURE'/*Wandthermostat Schlafzimmer:1 ACTUAL TEMPERATURE*/, 'Temperaturen.Temperatur_Schlafzimmer', undefined, 'temperaturen_alias', 'number', 'Math.round(val * 10) / 10', undefined, 'Schlafzimmer', 'value.temperature', 'per Script erstellt', undefined, undefined, '°C', undefined)
createAlias('hm-rpc.0.xxx.1.ACTUAL_TEMPERATURE'/*Wandthermostat Wohnzimmer:1 ACTUAL TEMPERATURE*/, 'Temperaturen.Temperatur_Wohnzimmer', undefined, 'temperaturen_alias', 'number', 'Math.round(val * 10) / 10', undefined, 'Wohnzimmer', 'value.temperature', 'per Script erstellt', undefined, undefined, '°C', undefined)
//////////////////////////////
// Batterien_voll_leer
//////////////////////////////
//2 x Batterien voll-leer über boolean Wert false/true in function batterien_voll_leer_alias
createAlias('hm-rpc.0.xxx.0.LOW_BAT'/*Wohnzimmer Fenster links:0 LOW BAT*/, 'Batterien_voll_leer.Wohnzimmer_Fenster_links', undefined, 'batterien_voll_leer_alias', 'boolean', undefined, undefined, 'Wohnzimmer Fenster links', 'indicator.lowbat', 'per Script erstellt', undefined, undefined, undefined, {false: 'voll', true: 'leer'})
createAlias('hm-rpc.0.xxx.0.LOW_BAT'/*Küche Fenster links:0 LOW BAT*/, 'Batterien_voll_leer.Kueche_Fenster_links', undefined, 'batterien_voll_leer_alias', 'boolean', undefined, undefined, 'Küche Fenster links', 'indicator.lowbat', 'per Script erstellt', undefined, undefined, undefined, {false: 'voll', true: 'leer'})
//////////////////////////////
// Batterien_Volt
//////////////////////////////
//2 x Batterien Volt Anzeige in function batterien_volt_alias
createAlias('hm-rpc.0.xxx.0.OPERATING_VOLTAGE'/*Wohnzimmer Fenster links:0 OPERATING VOLTAGE*/, 'Batterien_Volt.Wohnzimmer_Fenster_links', undefined, 'batterien_volt_alias', 'number', undefined, undefined, 'Wohnzimmer Fenster links', 'value.voltage', 'per Script erstellt', undefined, undefined, 'Volt', undefined)
createAlias('hm-rpc.0.xxx.0.OPERATING_VOLTAGE'/*Küche Fenster links:0 OPERATING VOLTAGE*/, 'Batterien_Volt.Kueche_Fenster_links', undefined, 'batterien_volt_alias', 'number', undefined, undefined, 'Küche Fenster links', 'value.voltage', 'per Script erstellt', undefined, undefined, 'Volt', undefined)