How I automated my apartment with Node-RED. Part II

Andrew Peyve
IT’s Tinkoff
Published in
12 min readApr 15, 2020

The second part of the story about the automation of the apartment. In this part, I will talk about lighting, multimedia systems, and security sensors.

Lighting

Several components are used to control lighting:

  • Motorized blinds
  • Dimmable lighting

Light

The light in the rooms is divided into two zones. I use Ikea LED dimmable lamps. I use three modules for control:

FIBARO Dimmer 2 dimmer
FIBARO Double Switch 2x1.5kW relay
FIBARO Single Switch 2,5kW relay

The dimmer can automatically select the dimming method (on the front and back sides) and calibrate to a minimum and maximum brightness. Since I had a three-wire system in advance for all wrappers, I did not have to use the bypass. The LED lights do not flicker, when there is no voltage. For dimmable modules, I use the bell switches to adjust the brightness.

Dimmer connection with three-wire system

That’s how it works:

  • If someone is in the house and the curtains are down, the lighting is completely manual (except for the bathroom and the on-duty lighting).
  • If there was no one at home and someone came through the front door but it was dark or the curtains were down, the light in the hallway turns on. At night, the lights on duty are automatically switched on and off. But if the light in the kitchen is on, it switches to manual operation.
  • In the bathroom, the light is switched on by a motion sensor and is not switched off while the door is closed. When the door is opened and there is no movement, the light is switched off by the dispenser.
  • In rooms, the light turns on if there was movement and it is darker outside than a certain threshold. If there is no movement for a long time, the light switches to 1% brightness.
  • After the automatic lowering of the curtains, the light turns on where there was a movement in the last 15 minutes.
  • If everybody leaves the house, the light turns off completely.
Light Control
Light Control
Light Control
Light Control

Curtains

As I wrote before, all the windows of my apartment overlook a busy avenue with bright lights. To solve the problem of bright light from the lanterns at night or in summer from the bright sun, I use motorized roller blinds.

At first, I wanted to buy ready-made sets, but the prices are very high and I did not like the color scheme. I had to reject this idea and assemble several sets myself.

There was not much choice from 220V engines: either French Somfy or Chinese Dooya. Somfy is quieter, but also more expensive, so the choice was made for Dooya DM35S engines. I ordered them together with the mounts on the Aliexpress.com. The engine has two ends, which adjust the maximum and minimum position.

Motor with mounts

The next problem is with the curtain winding pipes. According to the rules of the Russian Post, you can send a parcel no longer than 180 cm, and one of my windows is more than 2 meters. At the nearest construction market, I found an aluminum pipe of the required diameter — 50 mm. After mounting the motors in the pipes, I have to find blackout curtains. The fabric I ordered on my size from Aliexpress.com after agreement with the seller that he will send me only fabric, without mechanisms.

Fixed and assembled curtain

The blinds are controlled using the Z-Wave control module of the FIBARO Roller Shutter 2.

Blinds control module

The module is built into a wrapper. It’s better to use deep sockets at once to fit the module, the wires, and the switch. The module has sufficient settings, including alarm response, power consumption calculation, calibration at the end of the module.

Built-in switch

Curtains are installed in every room and in the kitchen. Curtains can be controlled either by switches located next to them or from an interface or script.

Curtains are lowered automatically when it is dusk and the light turns on outside. They are raised at the programmed time for each room on weekdays and weekends. If it is dark outdoors, the light in the room where the curtain comes down starts to gently turn on.

Workdays and weekends are calculated according to the downloaded production calendar. The time can be adjusted to the next day for each room using the command from the Telegram messenger. Before the curtain is minimized, a Telegram message is sent via an alarm clock with the possibility to confirm, cancel and postpone it for 30 minutes. If no action is taken, the curtains will be automatically minimized. On bright days, curtains automatically set their position depending on the lighting in the room and the time of day, and check whether the light is on or off. When the “Vacation” command is received, all curtains in all rooms are lowered.

Curtain control scenario
Curtain control scenario

Multimedia system

I have a lot of multimedia devices with different remote controls in my living room, and I wanted to control them with one button.

I found separate IR codes to turn on and off the amplifier, TV and HDMI splitter. With Python pronto_broadlink they were transcoded to base64 and then converted to an array of bytes and written to the IR code base. So now you don’t have to know what state the device is in: you can just send the signal again.

Since my amplifier is quite old and can’t process 4K images via HDMI, I bought an HDMI splitter. It allows you to divide the video and audio signal into two separate HDMIs: one of them is connected to an amplifier and the other to a TV.

Amplifier and other devices

At the moment I have two audio and video sources: a desktop computer and a server. From the interface you can turn on all the devices for playback with one button, only in one case the signal reception will be configured on the splitter from the server, and in the second case — from the computer. In the future, it is planned to connect game consoles to the splitter.

If everyone leaves home, all multimedia devices are disabled.

Multimedia Control

The presence system works on 6 motion sensors and the entrance door opening sensor. If no presence sensor is triggered after opening and closing the door, a confirmation message is sent to Telegram after a certain period of time. If no action is taken within two minutes, or if the “Ok” button is pressed, the system enters the “Outdoor” mode. If there is movement at this time, the command is canceled and the message is deleted.

I tried to use BLE, but in my case, it was not very stable and was heavily dependent on the phone transmitter.

Automatic presence detection

Safety sensors

The pipes have the locking devices — “Hydrolock”. When the control line is closed to the phase, the actuator closes the tap; when disconnected, it opens. Both actuators are connected to an integrated Philio relay with dry contacts.

The Gidrolock

The relay is controlled by Z-Wave NEO Coolcam sensors located in places where leaks are likely to occur. All sensors are directly associated with the relay so that the water can shut off without the controller. To prevent the tap from souring, it automatically opens and closes every two weeks at night.

Automation circuit for drives

When the Philio smoke sensor is triggered, the supply ventilation is switched off so as not to inflate the fire. This sends a message to the Telegram and the sensor beeps nastily.

Other sensors and devices

  • Sensor Multisensor 6 in 1 Aeotec Multisensor 6
  • Opening sensor NEO Coolcam

Authorize-server

This module is a simple authorization service implementation.

There are two HTTP-methods. One checks if the request came from the internal network and if so, authorizes the user by giving him a JWT token. The JWT token is encrypted with RSA 256, the private key must be written into the file next to it. If the request is not from the internal network, the user will be taken to the login page where he must enter his login and password and authenticate. Since there is no separation between users, there is an encrypted login and password (SHA 512) in the configuration file next to it.

MQTT-server

The MQTT server is implemented using the Aedes component.

The server rises on two ports: internal for communication with the server, without authorization, and external, which implements WebSocket. All front-end clients connect through a socket and are authenticated when authorizing, subscribing to the top and publishing any message. On an input comes the JWT-token which has been received from an authorization server, and there is its validation by means of a public key. Validation is not performed for requests from the server.

Custom Node-RED nodes

I implement one additional node and two modifications.

I had to modify node-red-contrib-openzwave, because my pull request was accepted only after 7 months, but now I can use the library from npm. The refinement was the node-reduction event.

The MQTT-node has also been modified. Essentially the main improvement is the pulling of the id of the connected client and the writing back of the client when sending it to the queue. Here you can do with sub-flow functionality. It was also supposed to send some data to the MQTT-node, but it was never useful.

A node with a schedule was required to control curtains and water taps. The ones available at that time in the library were too fancy and did not support a simple implementation using CRON. My implementation is quite simple: the schedule can either be accepted as a node input or set internally. It is possible to cancel the timetable. Under the hood, you can use node-schedule.

Frontend

Each active component of the apartment, which is started in automation, is a module at the frontend. Each module is self-contained and contains all necessary components for drawing. The modules are divided into two aggregates: static and Z-Wave. The modules must implement one interface.

Interface

export interface INode {id: string;type: NodeTypes;name: string;addWidget: ComponentClass<AddProps> | StatelessComponent<AddProps>;model: INodeModelConstructor;service?: INodeServiceConstructor;smallComponent: ComponentClass<WidgetProps<any>> | StatelessComponent<WidgetProps<any>>;bigComponent: ComponentClass<WidgetProps<any>> | StatelessComponent<WidgetProps<any>>;dialogEditComponent: ComponentClass<WidgetProps<any>> | StatelessComponent<WidgetProps<any>>;dialogViewComponent: ComponentClass<WidgetProps<any>> | StatelessComponent<WidgetProps<any>>;

An example

class Widget extends BaseWidget implements INode {model: INodeModelConstructor = Model;manufacturerid: string = '0x0086';producttype: string = '0x1a02';productid: string = '0x0064';addWidget: StatelessComponent<AddProps> = AddWidget;service: INodeServiceConstructor = Service;smallComponent: StatelessComponent<WidgetProps<Model>> = SmallComponent;bigComponent: StatelessComponent<WidgetProps<Model>> = BigComponent;dialogEditComponent: ComponentClass<WidgetProps<Model>> = DialogEditComponent;dialogViewComponent: ComponentClass<WidgetProps<Model>> = DialogViewComponent;}export default Widget;

For static modules (for example, air conditioners) — it is necessary to implement a data storage at the frontend. Such implementation is inconvenient, in a good way it should be changed for one storage.

For the Z-Wave modules, I have organized a common storage and tracked all the changes on it.

The module implements the display in two views:

  • For “big” screens (tablet or computer).
  • For phones.

Depending on the device type, touch or mouse control will be selected.

The application is built using Webpack + Babel: according to current settings it is built for the latest two versions of Firefox, Chrome, Chrome Android.

Source Features

Source

All Flow is turned off so as not to throw errors in the log. The configuration node for the Z-Wave is removed: it glitches if it doesn’t find the Z-Wave controller and drops the entire Node-RED. It should look like this:

To run the Russian official holiday calendar loader, you need to register at data.gov.ru, get a token there and enter it after access_token:

The weather forecast is downloaded from darksky.net every 15 minutes. It also requires a token, you need to register and enter the token after the forecast, to the coordinates:

To send Telegram notifications, you need to have your own bot, type it into the Telegram configuration node and specify the chat id where necessary. In the case of my Flow it is sub-flow:

The sources are divided into frontend and backend.

In the back part lies:

  • Authorize server.
  • MQTT-server.
  • Node-RED custom modules.
  • Exported Flow one file, which can be either loaded to itself or raise a separate instance for tests.
  • Project for Node-RED, in which you need to install the modules and after only load the exported Flow (modules are already in the project).
  • Mongo base backup.

You can find more detailed installation information in the readme file in GitHub.

What I want to do

I want to use a smartphone microphone or wall tablet for voice control. I plan to deploy my open-source system on CMU Sphinx server.

I also plan to assemble a module for taking and transmitting data of water and electricity meters.

I also want to accumulate statistics on various parameters — to optimize automation.

Conclusion

So far I have achieved almost complete automation of the house to suit my needs — to maintain a comfortable climate and lighting in the house. Thanks to some automation, I have been able to reduce electricity consumption (by about $20 in the month in winter compared to the same appliances without automation), both on the heating element side and the lighting side.

Of course, there are still bugs left in the algorithms, but I will catch them someday. The system is stable and I don’t see any lags despite the very low speed in the Z-Wave protocol.

Of course, the system will not pay for its cost, it’s more a matter of comfort. Such automation is very relaxing in terms of details like closing and opening curtains, lighting control. Being in a usual apartment, sometimes you forget to switch on the light or switch off, and curtains at all do not open.

I regulate the climate system by hand also rarely, but sometimes you need to turn on or off the air conditioner or correct the temperature on the battery. On the whole, I am satisfied with the resulting system: it allows me to spend less time on all kinds of adjustments. In case of failure of the controller, all functionality will remain in place (only automation will fall off). The functionality of the water shut-off will also remain in place. If I decide to sell the apartment, I will easily clean all the automation. Or I will leave it on the spot, but I will sell it for a better price.

--

--