Custom component with sensor and binary_sensor

Hi guys,

I really like the simple and lightweight approach to program my ESP32 with ESPHome. I already created a custom component with two sensors that works flawlessly, but now I have a problem with my second custom component: this one has both sensors and binary sensors (and maybe later also text sensors). Can anyone give me a hint how the configuration and/or code has to look like? Perhaps based on this example with an additional binary_sensor very_hot, that is true for a temperature reading above 40°C?

Thanks a lot!

First, you can easily react to the temperatures in home assistant.

Second, even if you want to do all this on esphome, I don’t think it will need a custom component, see in general

Can you explain why you need this so answers are more accurate?

I have done something like you want.
I created a custom component then I created custom binary sensor, custom output, etc which have a reference to the main custom component and interface i2c in a single point

@nickrout: You are right, my example with very_hot was really bad, of course you can calculate it in esphome or in home assistant. I couldn’t think of a better example, it should just be for demonstration. For my component, the binary_sensor can not be calculated from any other value (see below).

@glmnet: My actual use case is reading the temperatures and statuses from my central heating. Here I have sensors (eg. water temperatures), binary sensors (e.g. night mode active) and text sensors (e.g. error messages). The thing you describe sounds really similar to what I want to achieve. Could you please share your configuration and code with me?

So how are you reading from the central heating?

check my repo here
check how EHComedor.yaml works, there is a custom_component: which is the main connection with an Arduino Pro Mini via i2c, this device has several devices I use, most are binary_sensor and output (switches and relays)
Then there are the output and binary_sensor sections, with custom platforms, the implementation is in ape.h
The arduino code in case you are interested is buried a bit in the ArduinoPortExtender branch

1 Like

@nickrout: exactly this is what I am doing

@glmnet: you are my superhero! This line in your code did the trick:

#define get_ape(constructor) static_cast<ArduinoPortExtender *>(const_cast<CustomComponentConstructor *>(&constructor)->get_component(0))

I tried several ways to cast the custom component to my specific one but the compiler didn’t stop complaining. He obviously knows that I’m an absolute C and C++ noob :wink:

btw: Why do you use an Arduino and don’t connect your devices directly to the ESP? Do they all use 5V?

Yeah that line didn’t come out so easily. Not a C++ developer either, but learning a lot with ESPHome, I’m even sending some PRs now :blush:. I do C# mostly.

I had a network of MySensors running on pro minis, connected relays for lights / fans and all house switches. It didn’t work well for me due to comm issues, WiFi works better for me. OTA is awesome and esp8266 is way more powerful. But I have already the arduinos connected to many stuff so I just added the esp to them. Also the esp does not have as many io as I needed. Some nodes have like 15 io pins in use.

I have many arduino pro mini directly connected to i2c with esp8266. The Arduinos are 5V/16Mhz but I power everything at 3.3V and have no issues so far.

For a new project maybe 3.3v arduino would be more suitable.

I should write a cookbook about this sometime. Seems useful.

I think I have to create a lot more projects with C++ before I can think of creating PRs. I mostly do Java and sometimes a bit of Python. But who knows, perhaps with my next component on my list (I want to communicate with my Comet Blue thermostats) I might create one. The Arduino sketch already works, but I want to integrate it into ESPHome.

@glmnet You saved another life today :stuck_out_tongue:

1 Like

I’m putting together a cookbook to use an arduino as an salve and read
/write pins from esphome.
This way you get 18 extra io pins per arduinos (you can add many)
Stay tuned!