This device has no voice for this language. You can add one in your device’s speech settings.
Going Beyond Ready-Made Rules
Most home automation can be built with the visual rule editors in apps and controllers. Sometimes, however, the logic becomes too complex, a device needs behaviour that no editor offers, or you want full control over how something works.
That is when programming comes in – from a few lines of configuration to complete programs running on a microcontroller.
This guide gives an overview of the programming options in home automation and the practices that keep custom logic reliable and maintainable.
1. Where Custom Logic Can Run
Custom logic can run in different places, each with its own strengths:
| Location | Examples | Best For |
|---|---|---|
| In the device | Firmware on an ESP32 | Fast, local reactions that must work even without the network |
| In the central controller | Scripts, templates and automations in a home automation server | Logic that combines many devices and sources |
| In a separate tool | Node-RED flows, small programs on a home server | Complex flows, data processing, integrations |
A useful principle: put safety and time-critical logic as close to the device as possible, and coordination logic in the central controller.
2. Programming Options
- ESPHome – devices are described in configuration files, and logic can be added with built-in automations or short code sections.
- Tasmota rules and scripts – simple logic directly on devices running Tasmota.
- Node-RED – a visual tool where logic is built by connecting blocks in flows; well suited to MQTT and integrations.
- Arduino framework (C++) – widely used for microcontrollers, with a large number of libraries.
- ESP-IDF – Espressif’s own development framework, with full control over the ESP32.
- MicroPython – Python for microcontrollers; easy to learn and quick to test.
- Python and other languages on a server – for data processing, integrations and complex logic.
Choose the simplest tool that solves the problem. Configuration-based tools are easier to maintain than custom code.
3. Thinking in States
Automation logic becomes much clearer when it is designed as a state machine: the system is always in one defined state, and specific events move it to another state.
Example for a garage door:
| State | Event | Next State |
|---|---|---|
| Closed | Open command | Opening |
| Opening | Top limit switch reached | Open |
| Opening | No limit switch after 30 seconds | Fault |
| Open | Close command | Closing |
| Closing | Obstacle detected | Opening |
| Closing | Bottom limit switch reached | Closed |
Writing the states and transitions down before programming makes it easy to see missing cases – such as what happens when a limit switch never responds.
4. Non-Blocking Code
On a microcontroller, one program does everything: reading sensors, keeping the network connection alive and controlling outputs. If the program waits – for example with a long delay – nothing else happens during that time, and the device may lose its network connection or miss events.
- Instead of waiting, record the time and check regularly whether enough time has passed.
- Keep each part of the main loop short.
- Use the timers, events and scheduling features of the framework you use.
Frameworks such as ESPHome handle this automatically, which is one reason they are recommended for most projects.
5. Watchdogs and Recovery
Programs can stop responding because of bugs, memory problems or unexpected situations. A watchdog is a timer that restarts the device if the program stops reporting that it is alive.
- Keep the watchdog enabled on microcontrollers.
- Make sure the device returns to a safe state after a restart.
- Report restarts and their causes, so that recurring problems are noticed.
- Let the central controller notice when a device stops sending data (see Guide 11).
6. Updates Over the Network
Devices installed in walls, attics and gardens should be updatable without being removed. Over-the-air (OTA) updates make this possible.
- Protect OTA updates with a password.
- Test new firmware on a test device or on the bench first.
- Keep the previous working version, so you can go back.
- Avoid updating devices that control critical functions while they are in use.
7. Secrets and Security
Programs and configurations often contain Wi-Fi passwords, API keys and MQTT credentials:
- Store secrets in a separate file that is not shared – many tools support a dedicated secrets file.
- Never publish configurations or code with real passwords, for example on public code-sharing sites.
- Give each device its own credentials where possible.
- Disable debug interfaces and default passwords on finished devices.
8. Version Control and Documentation
Code and configurations change over time. Version control, for example with Git, keeps a history of every change and makes it easy to go back to a version that worked.
- Commit changes with a short description of what was changed and why.
- Keep the configuration of all devices and the central controller in one place.
- Add comments where the logic is not obvious.
- Write down the states, inputs and outputs of each custom device.
9. Testing Custom Logic
- Test the normal case.
- Test every state transition, including faults and timeouts.
- Simulate missing sensors and lost network connections.
- Restart the device in the middle of an operation and check the result.
- Run the logic for several days and review the logs.
Many errors only appear in rare situations. Thinking about them in advance is cheaper than discovering them when something goes wrong.
Final Thoughts
Programming opens possibilities beyond ready-made rules: complex logic, unusual devices and complete control. Choose the simplest tool that does the job, from ESPHome and Node-RED to your own microcontroller code.
Design logic as clear states and transitions, avoid blocking code, use watchdogs and plan for safe updates.
Keep secrets out of shared code, use version control and test thoroughly – and your custom logic will be as dependable as the rest of the system.
MISOLIMA® – Technology Made Practical.
Practical knowledge for understanding, designing and building home automation.
Continue reading
Back to the topGuide 24
Using Relays and Digital Outputs
Guide 26