Koenkk / zigbee-herdsman-converters

Collection of device converters to be used with zigbee-herdsman
MIT License
905 stars 3.02k forks source link

fromZigbee.humidity and fromZigbee.soil_moisture with multiEndpoint #6091

Closed p11max closed 1 year ago

p11max commented 1 year ago

Hello,

I made a gardening device which measures humidity and activates relays to water plants. Each device has 4 relays (fromzigbee.on_off) and 4 sensors. So I have 4 EndPoints, each one having 1 relay and 1 sensor. I made a custom converter (using multiEndpoint). When using only the relays I had no problem. But when using the humidity sensors, I had some errors.

I did some digging and realize that fromzigbee.humidity didn’t have the property postfixWithEndpointName.

I modified the fromZigbee.js file localy and it worked.

Is it possible to modify fromZigbee.js file?

Here are the modifications I made to fromZigbee.humidity:

    humidity: {
        cluster: 'msRelativeHumidity',
        type: ['attributeReport', 'readResponse'],
        options: [exposes.options.precision('humidity'), exposes.options.calibration('humidity')],
        convert: (model, msg, publish, options, meta) => {
            const humidity = parseFloat(msg.data['measuredValue']) / 100.0;
            const property = postfixWithEndpointName('humidity',msg,model,meta);

            // https://github.com/Koenkk/zigbee2mqtt/issues/798
            // Sometimes the sensor publishes non-realistic vales, it should only publish message
            // in the 0 - 100 range, don't produce messages beyond these values.
            if (humidity >= 0 && humidity <= 100) {
                return {[property]: calibrateAndPrecisionRoundOptions(humidity, options, 'humidity')};
            }
        },
    },

Soil_moisture could also be modified, as it’s actually what I should have used.

    soil_moisture: {
        cluster: 'msSoilMoisture',
        type: ['attributeReport', 'readResponse'],
        options: [exposes.options.precision('soil_moisture'), exposes.options.calibration('soil_moisture')],
        convert: (model, msg, publish, options, meta) => {
            const soilMoisture = parseFloat(msg.data['measuredValue']) / 100.0;
            const property = postfixWithEndpointName('soil_moisture',msg,model,meta);
            return {[property]: calibrateAndPrecisionRoundOptions(soilMoisture, options, 'soil_moisture')};
        },
    },

Thanks for having a look and checking if it could be implemented.

p11max

Koenkk commented 1 year ago

You can keep this in your external converter, e.g.:

const fzLocal = {
    humidity: {
        cluster: 'msRelativeHumidity',
        type: ['attributeReport', 'readResponse'],
        options: [exposes.options.precision('humidity'), exposes.options.calibration('humidity')],
        convert: (model, msg, publish, options, meta) => {
            const humidity = parseFloat(msg.data['measuredValue']) / 100.0;
            const property = postfixWithEndpointName('humidity',msg,model,meta);

            // https://github.com/Koenkk/zigbee2mqtt/issues/798
            // Sometimes the sensor publishes non-realistic vales, it should only publish message
            // in the 0 - 100 range, don't produce messages beyond these values.
            if (humidity >= 0 && humidity <= 100) {
                return {[property]: calibrateAndPrecisionRoundOptions(humidity, options, 'humidity')};
            }
        },
    },
}

and then in the fromZigbee section of your device use fzLocal.humidity instead of fz.humidity

p11max commented 1 year ago

Thanks for tip and the quick answer. It's working. I just had to add const utils = require('zigbee-herdsman-converters/lib/utils');

and replace postfixWithEndpointName and calibrateAndPrecisionRoundOptions by utils.postfixWithEndpointName and utils.calibrateAndPrecisionRoundOptions

I close the issue. Thanks one more time.

p11max