How to implement Automations for a Smart Home?

Hi, I want to implement a (very simple) Smart Home System in Rust. I need a way to create functions that are not hard coded in Rust and don't need compilation. For example I want to execute a function that is triggered by an input (e.g. light switch) and sets an output (e.g. turn on a light)

A quick brain storm with ChatGPT returns some options like an embedded scripting language, parsing a configuration file (e.g. Home Assistant uses Yaml), Wasm, a RESTful API or a plugin system.

Because it should be a small and simple system I tend to use a scripting language or a configuration file.

They both have pros and cons. An embedded scripting language is more flexible, but it is easier to build a gui for a a configuration file.

The Smart Home should run on limited hardware like a Raspberry Pi zero, so it should be efficient

Home Assistant uses Yaml as configuration files and embeds Jinja scripts for more functionality. It's easy for beginners because you can use a GUI to build simple automations, but you need to embed Jinja scripts for more functionality.

I don't know if I will ever add a web GUI to build automations, but it would be a nice to have.

Maybe someone can help me to decide which way I should go.

First you need to specify all the capabilities that will need to be configurable. This decision shouldn't be made before doing that design work.

If those capabilities can all be described by simple properties, then a config file is a adequate. If logic or transformations are needed to configure a capability, then a scripting language may be needed, although I recommend making this optional since many home users won't be able to use a scripting language.

Well, that's basically what I said. Simplicity or flexibility. So I need to implement both.

But maybe I already have an idea. I think it is easier to make a scripting language simple than a configuration file flexible. If I use a scripting language maybe I can also use it as configuration file or even create a DSL.

So currently I tend to only use a scripting language.

On are we game yet is a list of possible languages. But which ones should I take a closer look at? Something like Rhai or Gluon or something more familiar like Lua? I would prefer native Rust implementations over wrappers to C or similar. Everything should be compiled with Rust, if possible.

What do you mean by "Automations for a Smart Home"?

I imaging a smart home system as comprising various tiny cheap embedded devices scattered around the place, monitoring and controlling lights, temperatures, alarms, switches, displays, whatever you want.

Likely they will all communicate with some central system which intern collects data from them, provides them with configurations allows user setup and control, etc. This could be a PC, tablet, whatever.

So, do you mean software in those "edge" devices or software in that central control machine. Or all of it.

I think if I were building such a system from the ground up it would use little WiFi equipped micros for the "edge" units. Like the ESP32s or some such. A Raspberry Pi would provide a central system control and display. The whole thing would be operable from my MacBook wherever I am in the world.

All that code would be in Rust of course. From the edge units measuring temperatures or whatever to the web server on the Raspberry Pi.

But do you mean to have something a regular user could configure and even script easily? It's amazing what even non-programmers can do with Javascript on Espruino boards or other. Or there is Micro Python.

I mean the central control. The user can define custom behavior. That is what's called "Automation" in Home Assistant. I don't know how it's called in other Smart Home systems.

The user can define behaviour like "if the light switch is triggered, turn the light on" or "if the temperature is rising turn the air condition on".

Basically you have a trigger, that executes the automation, you have conditions determine if the action should be executed (e.g. turn the light on if a motion is detected, but only if it is dark), and you have the action itself.

Or in pseudo Code:

if trigger
when condition
then action

In Home Assistant this is done with a Yaml file. This works for simpler functions. But if you need more functionality you need to embed Jinja Code.

I think in my case using only an embedded scripting language would be a better solution, but I'm not sure.


I think Rhai is the best choice for the moment

That is really confusing. In all my decades on this planet "automation" has meant "the use of machines and computers that can operate without needing human control"

Anyway. Do you expect your users to have the sophistication to be writing such "programs" in any scripting language or even YAML configs? Are you the user?

It is exactly the same here. You automate certain functions. But someone has to be able to create these functions and adapt them to their personal needs.

Once programmed, the functions run "automatically"

Obviously. A smart home system (the control center) only offers the basic functions (communication with sensors and actuators, visual display, etc.).

But the actual logic must be able to be created individually. This can be any conceivable scenario, starting with the light in the hallway, which is switched on when someone moves in the hallway and it is dark, to the dishwasher, which is switched on when electricity is cheap, for example, to the electric car, which is charged when the sun shines on the solar system.

To do this, users need a system that allows them to decide for themselves what should happen and when. Some systems are simple drag and drop programs on a smartphone, but for me, creating them in text form is enough. And embedded scripting languages and/or configuration files are suitable here.

Since I don't want to develop a high-end system but just a simple and rudimentary system, I think a scripting language like Rhai, which works similarly to a scripting language in a game to create or extend the game logic, is enough for me.

You can compare such a smart home with a game engine like Unity or Godot. The game engine provides the resources for input and output, but does not define the actual game content. The only difference is that in a smart home, the end user must be able to create their own logic (What I called automation) in an simple way.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.