Hello,
I made a cloud Integration for the Netatmo Weather Station.
I successfully added the devices with data, but I’m not able to refresh the devices.
Here’s my Lambda function:
'use strict';
const { SchemaConnector } = require('st-schema')
const axios = require('axios')
const connector = new SchemaConnector()
.enableEventLogging(2)
.discoveryHandler(async (accessToken, response) => {
const axiosInstance = axios.create({
baseURL: 'https://api.netatmo.com/api',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}
})
const stations = await axiosInstance.get('/getstationsdata?get_favorite=false')
for (const station of stations.data.body.devices) {
response.addDevice(station._id, station.module_name, '29a6f358-b677-4918-9fa5-437aa9e61a26')
.manufacturerName('Netatmo')
.modelName(station.type)
.hwVersion('v1')
.swVersion('1.0.0')
if (station.modules) {
for (const module of station.modules) {
if (module.type === 'NAModule1') {
response.addDevice(module._id, module.module_name, '78c75c34-24cd-44bd-89f6-d7c5a7f2e03b')
.manufacturerName('Netatmo')
.modelName(station.type)
.hwVersion('v1')
.swVersion('1.0.0')
}
if (module.type === 'NAModule4') {
response.addDevice(module._id, module.module_name, 'aa6afe0c-24ee-4055-9699-01de5140d6f5')
.manufacturerName('Netatmo')
.modelName(station.type)
.hwVersion('v1')
.swVersion('1.0.0')
}
}
}
}
}).stateRefreshHandler(async (accessToken, response) => {
const axiosInstance = axios.create({
baseURL: 'https://api.netatmo.com/api',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}
})
const stations = await axiosInstance.get('/getstationsdata?get_favorite=false')
console.log(stations.data.body.devices.length , "Station(s) found")
for (const station of stations.data.body.devices) {
const mainStation = response.addDevice(station._id);
const mainStationComponent = mainStation.addComponent('main');
if (station.dashboard_data) {
const dashboardData = station.dashboard_data;
if (dashboardData.hasOwnProperty('Temperature')) {
mainStationComponent.addState('st.temperatureMeasurement', 'temperature', dashboardData.Temperature, "C");
}
if (dashboardData.hasOwnProperty('max_temp') && dashboardData.hasOwnProperty('min_temp')) {
mainStationComponent.addState('st.temperatureMeasurement', 'temperatureRange', {
maximum: dashboardData.max_temp,
minimum: dashboardData.min_temp,
step: 0.1
}, "C");
}
if (dashboardData.hasOwnProperty('Humidity')) {
mainStationComponent.addState('st.relativeHumidityMeasurement', 'humidity', dashboardData.Humidity, "%");
}
if (dashboardData.hasOwnProperty('CO2')) {
mainStationComponent.addState('st.carbonDioxideMeasurement', 'carbonDioxide', dashboardData.CO2);
if (dashboardData.CO2 <= 900) {
mainStationComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'good');
} else if (dashboardData.CO2 <= 1150) {
mainStationComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'moderate');
} else if (dashboardData.CO2 <= 1600) {
mainStationComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'slightlyUnhealthy');
} else if (dashboardData.CO2 <= 2000) {
mainStationComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'unhealthy');
} else {
mainStationComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'veryUnhealthy');
}
}
if (dashboardData.hasOwnProperty('Noise')) {
mainStationComponent.addState('st.soundPressureLevel', 'soundPressureLevel', dashboardData.Noise);
}
if (dashboardData.hasOwnProperty('Pressure')) {
mainStationComponent.addState('st.atmosphericPressureMeasurement', 'atmosphericPressure', dashboardData.Pressure / 10);
}
}
console.log(station.modules.length , "Module(s) found")
if (station.modules.length > 0) {
for (const module of station.modules) {
if (module.type === 'NAModule1') {
const outdoorModule = response.addDevice(module._id);
const outdoorModuleComponent = outdoorModule.addComponent('main');
outdoorModuleComponent.addState('st.battery', 'battery', module.battery_percent);
if (module.dashboard_data) {
const dashboardData = module.dashboard_data;
if (dashboardData.hasOwnProperty('Temperature')) {
outdoorModuleComponent.addState('st.temperatureMeasurement', 'temperature', dashboardData.Temperature, "C");
}
if (dashboardData.hasOwnProperty('max_temp') && dashboardData.hasOwnProperty('min_temp')) {
outdoorModuleComponent.addState('st.temperatureMeasurement', 'temperatureRange', {
maximum: dashboardData.max_temp,
minimum: dashboardData.min_temp,
step: 0.1
}, "C");
}
if (dashboardData.hasOwnProperty('Humidity')) {
outdoorModuleComponent.addState('st.relativeHumidityMeasurement', 'humidity', dashboardData.Humidity, "%");
}
}
}
if (module.type === 'NAModule4') {
const indoorModule = response.addDevice(module._id);
const indoorModuleComponent = indoorModule.addComponent('main');
indoorModuleComponent.addState('st.battery', 'battery', module.battery_percent);
if (module.dashboard_data) {
const dashboardData = module.dashboard_data;
if (dashboardData.hasOwnProperty('Temperature')) {
indoorModuleComponent.addState('st.temperatureMeasurement', 'temperature', dashboardData.Temperature, "C");
}
if (dashboardData.hasOwnProperty('max_temp') && dashboardData.hasOwnProperty('min_temp')) {
indoorModuleComponent.addState('st.temperatureMeasurement', 'temperatureRange', {
maximum: dashboardData.max_temp,
minimum: dashboardData.min_temp,
step: 0.1
}, "C");
}
if (dashboardData.hasOwnProperty('Humidity')) {
indoorModuleComponent.addState('st.relativeHumidityMeasurement', 'humidity', dashboardData.Humidity, "%");
}
if (dashboardData.hasOwnProperty('CO2')) {
indoorModuleComponent.addState('st.carbonDioxideMeasurement', 'carbonDioxide', dashboardData.CO2);
if (dashboardData.CO2 <= 900) {
indoorModuleComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'good');
} else if (dashboardData.CO2 <= 1150) {
indoorModuleComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'moderate');
} else if (dashboardData.CO2 <= 1600) {
indoorModuleComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'slightlyUnhealthy');
} else if (dashboardData.CO2 <= 2000) {
indoorModuleComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'unhealthy');
} else {
indoorModuleComponent.addState('st.carbonDioxideHealthConcern', 'carbonDioxideHealthConcern', 'veryUnhealthy');
}
}
}
}
}
}
}
}).commandHandler((accessToken, response, devices) => {
console.log("Hello")
}).callbackAccessHandler((accessToken, callbackAuthentication, callbackUrls) => {
console.log("World")
});
exports.handler = async (evt, context, callback) => {
return connector.handleLambdaCallback(evt, context, callback);
};
When I refresh manually the device on the app, nothing is triggerred, and I don’t know how to update the station periodically