Home automation with HubHazard
Tags: hubitat, automation, server, typescript
Published: 2020-08-31 05:54:57

Most home automation systems are trying to take the user as far from the code as possible. You have to configure everything with buttons and drop-downs. When it comes to simple automations, like *`if-this-then-that`* it's super handy - a couple of clicks and you're done. This way of creating automations still works in some more complex situations, but when you have to involve variables and nested conditions, it becomes a nightmare. Modifying any of those *not-really-complex* automations requires **a lot of clicking** and quite often you can't even see the whole system at a glance since it exists in several independent pieces.

As a software developer, I get pretty annoyed by the lack of an easy option of creating complex automations with code. I want my smart home to be **really** smart while staying sane at the same time! That's where [HubHazard][hubhazardcore] comes in.

## What is HubHazard?

Basically, [HubHazard][hubhazardcore] is a simple Node.js server that allows you to easily write smart home automations using Typescript. It's targeted towards people with basic programming skills. You don't have to be a professional developer to use it effectively.

### Easy to start with

Just use the [ready-made project template][hubhazardtemplate], add you configuration file (the template comes with an example file to copy) and you're ready to automate!

### Easy to write automations

Adding an automation to your smart home shouldn't be a hassle and that's why [HubHazard][hubhazardcore] is allowing you to write an automation as a simple class:

```ts
@Injectable()
export class BasicAutomation extends Automation {
  // Each automation needs to have a unique name
  readonly name = 'Basic automation';

  // Register trigger that will execute this automation every 15 seconds
  readonly triggers = [TimerTrigger.every(15, 'seconds')];

  // Handle the timer event
  async handleEvent() {
    console.log('Hey! I was triggered!');
  }
}
```

### Easy local hosting

Every smart home enthusiast wants their home to remain smart even when there's no internet connection. Most automation services rely on the cloud and when there's no connection, nothing works. [HubHazard][hubhazardcore] solves this problem by being built on the Node.js platform. It's extremely easy to host on devices like the [$5 Raspberry Pi Zero W](raspberry). This way you can communicate with your hub directly. No more high latency and internet-dependent automations.

### Safety of Typescript

The usage of Typescript in the [HubHazard][hubhazardcore] instead of plain Javascript allows for a familiar coding experience (for JS devs) but with lots of safeguards protecting you from making mistakes. In addition, all modern IDEs will provide code suggestions, so there's less of a need to search for things in [the documentation][hubhazarddocs].

### Modular design

The [HubHazard][hubhazardcore] server is built using the [Nest.js framework][nestjs]. This allows you to create your own modules that can extend the functionality of the server. If you want to integrate a different hub other than the Hubitat hub, go ahead and use the ready made [HubHazard module template](https://github.com/hubhazard/module-package-template)!

## Why does it exist?

Throughout my journey of building a smart home, I used many different solutions to automate things. Each one of them had some problem, that in my case was considered a **big problem**:

- Operations are performed in the cloud:
  - It introduces additional latency.
  - No internet = nothing works.
- The lack of tools to express complex automations with ease.
- The process of creating an extensive automation is unpleasant.

All I wanted was a user-friendly way of writing automations using a popular programming language. I also wanted the automations to be executed locally. I ended up with the [Hubitat Elevation hub](https://hubitat.com/) and created the [HubHazard][hubhazardcore] server.
- Hubitat allows you to connect to Zigbee and Z-Wave devices as well as some cloud based devices via [IFTTT](https://ifttt.com/).
- The HubHazard server connects to the Hubitat over WiFi and handles all automations.

Now everything is running locally, fast and exists in the form of a versionable Typescript codebase.

## Up next

In the next article I'll explain how to start with [HubHazard][hubhazardcore] in minutes using the [HubHazard Basic Template][hubhazardtemplate].

[hubhazardcore]: https://github.com/hubhazard/core
[hubhazardtemplate]: https://github.com/hubhazard/hubhazard-basic-template
[hubhazarddocs]: https://github.com/hubhazard/core/wiki
[raspberry]: https://www.raspberrypi.org/products/raspberry-pi-zero-w/
[nestjs]: https://nestjs.com/