main.js
/**
     * onStateChange
     *
     * Sends the control mqtt message to your device in case you changed a value
     *
     * @param id    {string} id of the datapoint that was changed
     * @param state {object} new state-object of the datapoint after change
     */
onStateChange
Sends the control mqtt message to your device in case you changed a value

@param id    {string} id of the datapoint that was changed
@param state {object} new state-object of the datapoint after change
onStateChange Sends the control mqtt message to your device in case you changed a value @param id { string } id of the datapoint that was changed @param state { object } new state - object of the datapoint after change
async onStateChange(id, state) {
        const thisDevice = id.split('.')[2];
        const action = id.split('.').pop();
        if (state && !state.ack) {
            let dysonAction = await this.getDatapoint( action );
            if ( typeof dysonAction === 'undefined' ) {
                dysonAction = action;
            } else {
                dysonAction = dysonAction[0];
            }
            this.log.debug('onStateChange: Using dysonAction: [' + dysonAction + ']');
            let messageData = {[dysonAction]: state.val};
            switch (dysonAction) {
                case 'fnsp' :
                    if (state.val === 'AUTO') {
                        messageData.auto = 'ON';
                    }
                    break;
                case 'hmax':{
                    let value = Number.parseInt(state.val, 10);
                    switch (this.config.temperatureUnit) {
                        case 'K' : value *= 10;
                            break;
                        case 'C' :
                            value = Number((value*10) + 273.15).toFixed(2);
                            break;
                        case 'F' :
                            value = Number(((value*10) + 273.15) * (9/5) + 32).toFixed(2);
                            break;
                    }
                    messageData = {[dysonAction]: dysonUtils.zeroFill(value, 4)};
                    break;
                }
            }
            this.log.info('SENDING this data to device (' + thisDevice + '): ' + JSON.stringify(messageData));
            const message = {'msg': 'STATE-SET',
                'time': new Date().toISOString(),
                'mode-reason': 'LAPP',
                'state-reason':'MODE',
                'data': messageData
            };
            for (const mqttDevice in devices){
                if (devices[mqttDevice].Serial === thisDevice){
                    this.log.debug('MANUAL CHANGE: device [' + thisDevice + '] -> [' + action +'] -> [' + state.val + ']');
                    devices[mqttDevice].mqttClient.publish(
                        devices[mqttDevice].ProductType + '/' + thisDevice + '/command',
                        JSON.stringify(message)
                    );
                }
            }
        } else if (state && state.ack) {
            if ( action.substr( action.length-5, 5 ) === 'Index' ) {
                this.createOrExtendObject(thisDevice + '.AirQuality', {
                    type: 'state',
                    common: {
                        name: 'Overall AirQuality (worst value of all indexes)',
                        'read': true,
                        'write': false,
                        'role': 'value',
                        'type': 'number',
                        'states' : {0:'Good', 1:'Medium', 2:'Bad', 3:'very Bad', 4:'extremely Bad', 5:'worrying'}
                    },
                    native: {}
                }, Math.max(NO2, VOC, Dust, PM25, PM10));
            }
        }
    }
async onStateChange ( id , state ) { const thisDevice = id . split ( '.' ) [ 2 ] ; const action = id . split ( '.' ) . pop ( ) ; if ( state && ! state . ack ) { let dysonAction = await this . getDatapoint ( action ) ; if ( typeof dysonAction === 'undefined' ) { dysonAction = action ; } else { dysonAction = dysonAction [ 0 ] ; } this . log . debug ( 'onStateChange: Using dysonAction: [' + dysonAction + ']' ) ; let messageData = { [ dysonAction ] : state . val } ; switch ( dysonAction ) { case 'fnsp' : if ( state . val === 'AUTO' ) { messageData . auto = 'ON' ; } break ; case 'hmax' : { let value = Number . parseInt ( state . val , 10 ) ; switch ( this . config . temperatureUnit ) { case 'K' : value *= 10 ; break ; case 'C' : value = Number ( ( value * 10 ) + 273.15 ) . toFixed ( 2 ) ; break ; case 'F' : value = Number ( ( ( value * 10 ) + 273.15 ) * ( 9 / 5 ) + 32 ) . toFixed ( 2 ) ; break ; } messageData = { [ dysonAction ] : dysonUtils . zeroFill ( value , 4 ) } ; break ; } } this . log . info ( 'SENDING this data to device (' + thisDevice + '): ' + JSON . stringify ( messageData ) ) ; const message = { 'msg' : 'STATE-SET' , 'time' : new Date ( ) . toISOString ( ) , 'mode-reason' : 'LAPP' , 'state-reason' : 'MODE' , 'data' : messageData } ; for ( const mqttDevice in devices ) { if ( devices [ mqttDevice ] . Serial === thisDevice ) { this . log . debug ( 'MANUAL CHANGE: device [' + thisDevice + '] -> [' + action + '] -> [' + state . val + ']' ) ; devices [ mqttDevice ] . mqttClient . publish ( devices [ mqttDevice ] . ProductType + '/' + thisDevice + '/command' , JSON . stringify ( message ) ) ; } } } else if ( state && state . ack ) { if ( action . substr ( action . length - 5 , 5 ) === 'Index' ) { this . createOrExtendObject ( thisDevice + '.AirQuality' , { type : 'state' , common : { name : 'Overall AirQuality (worst value of all indexes)' , 'read' : true , 'write' : false , 'role' : 'value' , 'type' : 'number' , 'states' : { 0 : 'Good' , 1 : 'Medium' , 2 : 'Bad' , 3 : 'very Bad' , 4 : 'extremely Bad' , 5 : 'worrying' } } , native : { } } , Math . max ( NO2 , VOC , Dust , PM25 , PM10 ) ) ; } } }
onStateChange
 Sends the control mqtt message to your device in case you changed a value
onStateChange Sends the control mqtt message to your device in case you changed a value
// Warning, state can be null if it was deleted // you can use the ack flag to detect if it is status (true) or command (false) // get the whole data field array // if dysonAction is undefined it's an adapter internal action and has to be handled with the given Name // pick the dyson internal Action from the result row // when AUTO set AUTO to true also // add second value to message to get auto mode working // Target temperature for heating in KELVIN! // convert temperature to configured unit // build the message to be send to the device //noinspection JSUnresolvedVariable //noinspection JSUnresolvedVariable // state changes by hardware or adapter depending on hardware values // check if it is an Index calculation // if some index has changed recalculate overall AirQuality