Skip to content

Commit

Permalink
Copy unit of source state to all statistic states
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Nov 18, 2024
1 parent 13a484f commit 65c6872
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ iobroker add statistics
### __WORK IN PROGRESS__

* (@simatec) Added responsive jsonConfig
* (@klein0r) Copy unit of source state to all statistic states

### 3.0.0 (2024-11-15)
NodeJS >= 18.x and js-controller >= 5 is required
Expand Down
69 changes: 36 additions & 33 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,12 @@ class Statistics extends utils.Adapter {
obj.impUnitPerImpulse = this.config.impUnitPerImpulse; // Default from config if 0
}

const sourceObj = await this.getForeignObjectAsync(id);
const sourceUnit = sourceObj?.type === 'state' && sourceObj?.common?.unit;

// function is called with the custom objects
this.log.debug(`[CREATION] ============================== ${id} =============================`);
this.log.debug(`[CREATION] setup of object ${id}: ${JSON.stringify(obj)}`);
this.log.debug(`[CREATION] setup of object "${id}": ${JSON.stringify(obj)}`);
const logName = obj.logName;

// count
Expand All @@ -750,7 +753,7 @@ class Statistics extends utils.Adapter {
this.typeObjects.count.push(id);
}

await this.defineObject('count', id, logName); // type, id, name
await this.defineObject('count', id, logName);
}

// sumCount
Expand All @@ -761,7 +764,7 @@ class Statistics extends utils.Adapter {
this.typeObjects.sumCount.push(id);
}

await this.defineObject('sumCount', id, logName, obj.impUnit); // type, id, name, Unit
await this.defineObject('sumCount', id, logName, obj.impUnit);
}

// sumDelta
Expand All @@ -772,7 +775,7 @@ class Statistics extends utils.Adapter {
this.typeObjects.sumDelta.push(id);
}

await this.defineObject('sumDelta', id, logName); // type, id, name
await this.defineObject('sumDelta', id, logName, sourceUnit);
}

// minMax
Expand All @@ -783,7 +786,7 @@ class Statistics extends utils.Adapter {
this.typeObjects.minmax.push(id);
}

await this.defineObject('minmax', id, logName); // type, id, name
await this.defineObject('minmax', id, logName, sourceUnit);
}

// avg
Expand All @@ -794,7 +797,7 @@ class Statistics extends utils.Adapter {
this.typeObjects.avg.push(id);
}

await this.defineObject('avg', id, logName); // type, id, name
await this.defineObject('avg', id, logName, sourceUnit);
}

// timeCount
Expand All @@ -805,7 +808,7 @@ class Statistics extends utils.Adapter {
this.typeObjects.timeCount.push(id);
}

await this.defineObject('timeCount', id, logName); // type, id, name
await this.defineObject('timeCount', id, logName);
}

// fiveMin
Expand All @@ -816,7 +819,7 @@ class Statistics extends utils.Adapter {
this.typeObjects.fiveMin.push(id);
}

await this.defineObject('fiveMin', id, logName); // type, id, name
await this.defineObject('fiveMin', id, logName);
}

// sumGroup
Expand Down Expand Up @@ -1017,16 +1020,16 @@ class Statistics extends utils.Adapter {
common: {
name: {
en: `Saved values for ${name.en}`,
de: `Gespeicherte Werte für ${name.de || name.en}`,
ru: `Сохраненные значения для ${name.ru || name.en}`,
pt: `Valores salvos para ${name.pt || name.en}`,
nl: `Bespaarde waarden voor ${name.nl || name.en}`,
fr: `Valeurs sauvegardées pour ${name.fr || name.en}`,
it: `Valori salvati per ${name.it || name.en}`,
es: `Valores guardados para ${name.es || name.en}`,
pl: `Oszczędne wartości dla ${name.pl || name.en}`,
uk: `Збережені значення для ${name.uk || name.en}`,
'zh-cn': `保存的价值 ${name['zh-cn'] || name.en}`
de: `Gespeicherte Werte für ${name.de ?? name.en}`,
ru: `Сохраненные значения для ${name.ru ?? name.en}`,
pt: `Valores salvos para ${name.pt ?? name.en}`,
nl: `Bespaarde waarden voor ${name.nl ?? name.en}`,
fr: `Valeurs sauvegardées pour ${name.fr ?? name.en}`,
it: `Valori salvati per ${name.it ?? name.en}`,
es: `Valores guardados para ${name.es ?? name.en}`,
pl: `Oszczędne wartości dla ${name.pl ?? name.en}`,
uk: `Збережені значення для ${name.uk ?? name.en}`,
'zh-cn': `保存的价值 ${name['zh-cn'] ?? name.en}`
}
},
native: {
Expand All @@ -1040,16 +1043,16 @@ class Statistics extends utils.Adapter {
common: {
name: {
en: `Temporary values for ${name.en}`,
de: `Vorläufige Werte für ${name.de || name.en}`,
ru: `Временные значения для ${name.ru || name.en}`,
pt: `Valores temporários para ${name.pt || name.en}`,
nl: `Tijdelijke waarden voor ${name.nl || name.en}`,
fr: `Valeurs temporaires pour ${name.fr || name.en}`,
it: `Valori temporanei per ${name.it || name.en}`,
es: `Valores temporales para ${name.es || name.en}`,
pl: `Temporary wartości dla ${name.pl || name.en}`,
uk: `Тимчасові значення для ${name.uk || name.en}`,
'zh-cn': `${name['zh-cn'] || name.en} 的临时值`,
de: `Vorläufige Werte für ${name.de ?? name.en}`,
ru: `Временные значения для ${name.ru ?? name.en}`,
pt: `Valores temporários para ${name.pt ?? name.en}`,
nl: `Tijdelijke waarden voor ${name.nl ?? name.en}`,
fr: `Valeurs temporaires pour ${name.fr ?? name.en}`,
it: `Valori temporanei per ${name.it ?? name.en}`,
es: `Valores temporales para ${name.es ?? name.en}`,
pl: `Temporary wartości dla ${name.pl ?? name.en}`,
uk: `Тимчасові значення для ${name.uk ?? name.en}`,
'zh-cn': `${name['zh-cn'] ?? name.en} 的临时值`,
}
},
native: {
Expand All @@ -1076,7 +1079,7 @@ class Statistics extends utils.Adapter {
obj.common.unit = unit;
}

await this.extendObjectAsync(`save.${type}.${id}.${objects[s]}`, obj);
await this.extendObject(`save.${type}.${id}.${objects[s]}`, obj);
}

// states for the temporary values
Expand All @@ -1099,23 +1102,23 @@ class Statistics extends utils.Adapter {
obj.common.unit = unit;
}

await this.extendObjectAsync(`temp.${type}.${id}.${objects[s]}`, obj);
await this.extendObject(`temp.${type}.${id}.${objects[s]}`, obj);
}

await this.setInitial(type, id);
}

async setInitial(type, id) {

const saveObjects = nameObjects[type].save;

for (let s = 0; s < saveObjects.length; s++) {
const name = saveObjects[s];

const targetId = `save.${type}.${id}.${name}`;
const currentVal = await this.getValueAsync(targetId);

if (currentVal === null) {
this.log.debug(`[SET INITIAL] "${id}" -> ${targetId}`);
this.log.debug(`[SET INITIAL] "${id}" -> "${targetId}"`);

if (type === 'count') {
await this.setValueAsync(targetId, 0);
Expand Down Expand Up @@ -1157,7 +1160,7 @@ class Statistics extends utils.Adapter {
const currentVal = await this.getValueAsync(targetId);

if (currentVal === null) {
this.log.debug(`[SET INITIAL] "${id}" -> ${targetId}`);
this.log.debug(`[SET INITIAL] "${id}" -> "${targetId}"`);

if (type === 'count') {
const countInitVal = await this.getForeignStateAsync(id);
Expand Down

0 comments on commit 65c6872

Please sign in to comment.