diff --git a/lib/statescontroller.js b/lib/statescontroller.js index fa785c43..4c9e7102 100644 --- a/lib/statescontroller.js +++ b/lib/statescontroller.js @@ -624,61 +624,68 @@ class StatesController extends EventEmitter { } // find states for payload if (devStates.states !== undefined) { - const states = statesMapping.commonStates.concat( - devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id))); - - for (const stateInd in states) { - const statedesc = states[stateInd]; - let value; - if (statedesc.getter) { - value = statedesc.getter(payload); - } else { - value = payload[statedesc.prop || statedesc.id]; - } - // checking value - if (value === undefined || value === null) { - continue; - } - let stateID = statedesc.id; + try { + const states = statesMapping.commonStates.concat( + devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id)) + ); + + for (const stateInd in states) { + const statedesc = states[stateInd]; + let value; + if (statedesc.getter) { + value = statedesc.getter(payload); + } else { + value = payload[statedesc.prop || statedesc.id]; + } + // checking value + if (value === undefined || value === null) { + continue; + } - if (has_debug && statedesc.id !== 'msg_from_zigbee') { - this.warn(`ELEVATED publishToState: value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`); - } + let stateID = statedesc.id; - const common = { - name: statedesc.name, - type: statedesc.type, - unit: statedesc.unit, - read: statedesc.read, - write: statedesc.write, - icon: statedesc.icon, - role: statedesc.role, - min: statedesc.min, - max: statedesc.max, - }; - - if (typeof value === 'object' && value.hasOwnProperty('stateid')) { - stateID = `${stateID}.${value.stateid}`; - if (value.hasOwnProperty('unit')) { - common.unit = value.unit; + if (has_debug && statedesc.id !== 'msg_from_zigbee') { + this.warn(`ELEVATED publishToState: value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`); } - common.name = value.name ? value.name : value.stateid; - common.role = value.role ? `value.${value.role}` : 'number'; - value = value.value; - } - // if needs to return value to back after timeout - if (statedesc.isEvent) { - this.updateStateWithTimeout(devId, statedesc.id, value, common, 300, !value); - } else { - if (statedesc.prepublish) { - this.collectOptions(devId, model, options => - statedesc.prepublish(devId, value, newvalue => - this.updateState(devId, stateID, newvalue, common), options)); + const common = { + name: statedesc.name, + type: statedesc.type, + unit: statedesc.unit, + read: statedesc.read, + write: statedesc.write, + icon: statedesc.icon, + role: statedesc.role, + min: statedesc.min, + max: statedesc.max, + }; + + if (typeof value === 'object' && value.hasOwnProperty('stateid')) { + stateID = `${stateID}.${value.stateid}`; + if (value.hasOwnProperty('unit')) { + common.unit = value.unit; + } + common.name = value.name ? value.name : value.stateid; + common.role = value.role ? `value.${value.role}` : 'number'; + value = value.value; + } + + // if needs to return value to back after timeout + if (statedesc.isEvent) { + this.updateStateWithTimeout(devId, statedesc.id, value, common, 300, !value); } else { - this.updateState(devId, stateID, value, common); + if (statedesc.prepublish) { + this.collectOptions(devId, model, options => + statedesc.prepublish(devId, value, newvalue => + this.updateState(devId, stateID, newvalue, common), options) + ); + } else { + this.updateState(devId, stateID, value, common); + } } } + } catch (e) { + this.debug(`No states in device ${devId} : payload ${JSON.stringify(payload)}`); } } }