arachnetech / homebridge-mqttthing

A plugin for Homebridge allowing the integration of many different accessory types using MQTT.
Apache License 2.0
462 stars 104 forks source link

Speed presets in a fan #512

Closed miguelpruivo closed 2 years ago

miguelpruivo commented 2 years ago

Hi, I'm trying to setup my vacumm and added a fan speed toggle for the speed which can have 6 enums as values, so far, it's been working great, the problem is that the UI in the Homekit allows me to toggle "in-between" values from 0-100 and I wanted to lock it to jumps of 20, like [0, 20, 40, 60, 80, 100] as the only valid values.

In the Roborock homebridge plugin there is something similar and I can effectively only toggle between those, but I'm yet to found a way to do it with mqtt. This is my setup so far for it:

  "type": "fan",
            "name": "Speed",
            "topics": {
                "getRotationSpeed": {
                    "topic": "valetudo/s50/FanSpeedControlCapability/preset",
                    "apply": "if(message == 'min') {return 20} else if(message == 'low') {return 40} else if(message == 'medium') {return 60} else if(message == 'high') {return 80} else if(message == 'max' || message == 'turbo') {return 100} else {return 0}"
                },
                "setRotationSpeed": {
                    "topic": "valetudo/s50/FanSpeedControlCapability/preset/set",
                    "apply": "if(message == 20) {return 'min'} else if(message == 40) {return 'low'} else if(message == 60) {return 'medium'} else if(message == 80) {return 'high'} else if(message == 100) {return 'max'} else {return 'off'}"
                }
            }

Thank you!

arachnetech commented 2 years ago

Something like this should work:

                "getRotationSpeed": { "topic": "test/fan/getRotationSpeed", "apply": "return Math.floor(message/20)*20" },
                "setRotationSpeed": { "topic": "test/fan/setRotationSpeed", "apply": "return Math.floor(message/20)*20" },

... rounding down, or possibly better:

                "getRotationSpeed": { "topic": "test/fan/getRotationSpeed", "apply": "return Math.floor((message+10)/20)*20" },
                "setRotationSpeed": { "topic": "test/fan/setRotationSpeed", "apply": "return Math.floor((message+10)/20)*20" },

... rounding to the nearest value.