Hi!
I could find the answer on the forum, so I just “stole” some code I found on the internet to create an “Custom Fan” component for ESPHome and the Sonoff 4CH (Pro). We have Mechinical Ventilation at home and it uses a Perilex plug (https://www.ratio.nl/media/products/large/per_aan.jpg) and I wanted to switch it on with a selection for speed. This isn’t possible at our home, because the they only have an “On / Off” switch, instead of a state switch.
Configuration files: https://github.com/Daandamhuis/esphome
Custom Component
#include "esphome.h"
using namespace esphome;
#define PIN_1 12
#define PIN_2 5
#define PIN_3 4
class Sonoff4CHProFan : public Component, public FloatOutput {
public:
void write_state(float state) override {
if (state < 0.3) {
// OFF
digitalWrite(PIN_1, LOW);
digitalWrite(PIN_2, LOW);
digitalWrite(PIN_3, LOW);
} else if (state < 0.6) {
// low speed
digitalWrite(PIN_1, HIGH);
digitalWrite(PIN_2, LOW);
digitalWrite(PIN_3, LOW);
} else if (state < 0.9) {
// medium speed
digitalWrite(PIN_1, LOW);
digitalWrite(PIN_2, HIGH);
digitalWrite(PIN_3, LOW);
} else {
// high speed
digitalWrite(PIN_1, LOW);
digitalWrite(PIN_2, LOW);
digitalWrite(PIN_3, HIGH);
}
}
};