Advice writing my own integrations

We built a new house six years ago, and I put in insane amounts of conduit with Cat6 cables. There are at least one Cat6 to every physical lightswitch for example. Then, I put in a few Arduinos to control stuff, with the intention to make that smart when I got around to it, and now six years have gone by and I’m just getting started. A home is never finished :slight_smile:

I have for example two Arduinos controlling light, one is for toggleable lights, the other for dimmable. I figured, I’ll just start with the first to understand how that integrates. The concept I’d like to keep is that one device is responsible for its own operations, it shouldn’t rely on another computer for basic operation, if someone hits a switch, the lights should turn on or off. So, the idea is to add an interface to the Arduino so that it can be integrated in HA. I’m not sure how to do it though, as it appears that the complexity is greater than what available Firmatas can do (though the protocol seems interesting).

Currently, when someone hits a switch (they look like normal switches), a circuit is closed, and that is then registered by an Arduino Mega. To one output pin, I have attached three 8-bit shift registers, so I send a 24 bit number there. This was needed, because not even the Mega has that many pins to allow both >24 inputs, 24 outputs and Ethernet shield.

Currently, it doesn’t have an interface to anything over that Ethernet shield, so I need to change that, as I write an integration for HA.

I went through the Firmata documentation, and it didn’t really get me started, mostly because my device is both a sensor (I need to read the status of the light) and a switch (I need to toggle the light), and there’s no direct mapping to a pin. So, any pointers on how I can get started integrating my own devices would be greatly appreciated!

You could add the sensors using manual mqtt, its not quite as clean as writing a real integration, but certainly a good first step to get your devices connected.

  • Install MQTT broker (ie. mosquitto) on the HA machine
  • Use a MQTT lib on the arduino to send state changes, and listen for switch events
  • Add the sensor to HA configuration
sensor:
  - platform: mqtt
    unique_id: "1234567890"
    name: "light"
    state_topic: "home/light1/state"

Your arduino then needs to send state changes, using mqtt to the topic “home/light1/state”

MQTT Integration is the way to go. Trying to do this yourself and get the same feature set that MQTT Integration would provide would be a lot of work. For example, MQTT Integration is Local Push. In order to achieve that, you’d either need your arduino to provide a websocket type connection, or hit a callback URL on every state change.

MQTT Integration also means that you can use that same data, and control the same lights from any other system that speaks MQTT. This adds a bit of future proofing to your implementation.

It also makes your code more shareable, should you choose to do so. Integrating via MQTT means the same Arduino code can be used by users of other Home Automation software (not Home Assistant).

Great, thanks a lot! As usual, I didn’t manage to do any work on this before now, but I’m going with MQTT integration, starting to try it out now!