google-home / smart-home-nodejs

A sample of the Smart Home device control APIs in Actions on Google
Apache License 2.0
888 stars 289 forks source link

thermostatMode issue #546

Open Wuttiwong opened 2 years ago

Wuttiwong commented 2 years ago

i try to using ac_unit with trait like this

app.onSync((body) => { return { requestId: body.requestId, payload: { agentUserId: USER_ID, devices: [{ id: '123', type: 'action.devices.types.AC_UNIT', traits: [ 'action.devices.traits.FanSpeed', 'action.devices.traits.OnOff', 'action.devices.traits.TemperatureSetting', ], name: { defaultNames: ['Air Condition'], name: 'Air Condition', nicknames: ['Air Condition'], }, deviceInfo: { manufacturer: 'smart-home-inc', model: 'hs1234', hwVersion: '3.2', swVersion: '11.4', }, willReportState: true, attributes: { availableThermostatModes: [ 'off', 'heat', 'on', ], thermostatTemperatureRange: { minThresholdCelsius: 15, maxThresholdCelsius: 30, }, pausable: true, availableFanSpeeds: { speeds: [{ speed_name: 'speed_low', speed_values: [{ speed_synonym: ['low'], lang: 'en', }], }, { speed_name: 'speed_medium', speed_values: [ { speed_synonym: ['medium'], lang: 'en', }, ], }, { speed_name: 'speed_high', speed_values: [ { speed_synonym: ['high'], lang: 'en', }, ], }, ], ordered: true, }, commandOnlyFanSpeed: true, supportsFanSpeedPercent: true, queryOnlyTemperatureSetting: true, thermostatTemperatureUnit: 'C', }, }], }, }; });

const queryFirebase = async (deviceId) => { const snapshot = await firebaseRef.child(deviceId).once('value'); const snapshotVal = snapshot.val(); return { on: snapshotVal.OnOff.on, currentFanSpeedSetting: snapshotVal.FanSpeed.currentFanSpeedSetting, currentFanSpeedPercent: snapshotVal.FanSpeed.currentFanSpeedPercent, thermostatMode: snapshotVal.TemperatureSetting.thermostatMode, }; };

when i running it, its return this error message at firebase TypeError: Cannot read property 'thermostatMode' of undefined

please help me

armendpe commented 7 months ago

The error message "Cannot read property 'thermostatMode' of undefined" indicates that the thermostatMode property is being accessed on an undefined object. This suggests that snapshotVal.TemperatureSetting is undefined.

To resolve this , you should first ensure that the TemperatureSetting property exists within your Firebase data structure for the specified deviceId. If it does not exist, you need to handle this case gracefully in your code.

You can modify the queryFirebase function to check if snapshotVal.TemperatureSetting exists before accessing its properties.