The tale of how I automated the apartment using Node-RED. Part II

The long-awaited continuation of the post about the automation of the apartment . In this part I will talk about lighting, a multimedia system and security sensors.



Lighting


To control the lighting, several components are used:

  • motorized curtains;
  • dimmable lighting.

Shine


The light in the rooms is divided into two zones. I use ikeev LED dimmable lamps. I use three modules for control:
Built-in dimmer FIBARO Dimmer 2
Built-in dimmer FIBARO Dimmer 2

Built-in double relay FIBARO Double Switch 2x1.5kW
Built-in dual relay FIBARO Double Switch 2x1.5kW

Built-in Relay FIBARO Single Switch 2.5kW
Built-in relay FIBARO Single Switch 2.5kW

Dimmer can automatically choose a dimming method (on the leading and trailing edges of the phase) and calibrate for minimum and maximum brightness. Since I had a three-wire system in advance for all the sockets, I did not have to use a bypass. LED lamps do not flicker and do not light when there is no voltage. For dimmable modules, I use ringer switches so that you can adjust the brightness.


Three-wire dimmer wiring diagram

Here's how it works:

  • - , ( ).
  • - , — . . , .
  • , , . — .
  • , . , 1% .
  • , 15 .
  • , .














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

At first I wanted to buy ready-made kits, but the prices were very high, and I did not like the color scheme. This idea had to be discarded and assemble several sets on their own.

Of the 220 V engines, the choice was small: either French Somfy or Chinese Dooya. Somfy is quieter, but also more expensive, so the choice fell on Dooya DM35S engines. I ordered them along with fasteners on the "Aliexpress". The engine has two limit switches that adjust the maximum and minimum position.


Engine with mounts

The next problem was the pipe for winding curtains. According to the rules of the mail message, you can send a parcel no longer than 180 cm, and one of the windows I have is more than 2 meters. In the nearest construction market, I found an aluminum pipe of the desired diameter - 50 mm. After installing the engines in the pipes, it remains to find blackout curtains. I ordered the fabric in my sizes from "Aliexpress" after agreeing with the seller that he would send me only the fabric, without mechanisms.


The fixed assembled curtain The

curtains are controlled using the FIBARO Roller Shutter 2 Z-Wave blind control module. Louver


control module

The module is built into the socket. For such modules, it is advisable to immediately lay deep sockets to push the module, wires and switch. The module has enough settings, including response to alarms, calculation of power consumption, calibration with limit switches.


Assembled switch

Curtains are installed in each room and in the kitchen. You can control the curtains either by the switches located next to them, or by using an interface or script.

Curtains are lowered automatically when dusk sets in and the lights turn on on the street. They rise according to the programmed time for each room on weekdays and weekends. If it is dark outside, the light starts to turn on smoothly in the room where the curtain is lowered.

Working days and weekends are calculated according to the downloaded production calendar. You can reconfigure the time the next day for each room using the telegram command. Before folding the curtains according to the “alarm clock”, a telegram message arrives with the ability to confirm, cancel and reschedule for 30 minutes. If the action does not follow, the curtains automatically fold. On bright days, the curtains automatically set their position depending on the lighting in the room and the time of day, and also check whether the light is on or not. Upon receipt of the “Vacation” command, all curtains in all rooms are lowered. Curtain Management Scenario Curtain Management Scenario








Work example

Multimedia system


I have many multimedia devices with various remotes in the living room, and I wanted to control them with one button.

On the Internet, I found separate IR codes for turning on and off the amplifier, TV and HDMI splitter. Using the pronto_broadlink Python script , they were transcoded to base64, and then converted to an array of bytes and written to the IR code base. Therefore, now it is not necessary to know what state the device is in now: you can simply send the signal again.

Since my amplifier is quite old and does not know how to process a 4K picture via HDMI, I bought an HDMI splitter. It allows you to split the video and audio signal into two separate HDMI: one of them is connected to the amplifier, the second to the TV.


Amplifier and other devices

At the moment I have two sources of audio and video: a desktop computer and a server. From the interface, you can turn on all 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 - from the computer. In the future, it is planned to connect game consoles to the splitter.

If everyone leaves the house, all multimedia devices are turned off. Media control scene




The presence system operates on 6 motion sensors and a front door opening sensor. If, after opening and closing the door, not a single occupancy sensor has triggered, a message will be sent to Telegram at a certain interval with confirmation. If no actions are taken within two minutes or the “Ok” button is pressed, the system switches to the “Out of home” mode. If movement occurs at this time, the command is canceled and the message is deleted.

I tried using BLE, but in my case it did not work very stable and depended heavily on the phone transmitter. Automatic presence detection




Safety sensors


Gidrolock locking devices are installed on the water pipes . When the control wire is shorted to phase, the actuator closes the tap; when disconnected, it opens. Both drives are connected to a dry contact Philio built-in relay .

Gidrolock
The Gidrolock

Relay drive is controlled by NEO Coolcam Z-Wave sensors located in the areas of potential leaks. All sensors are directly in association with the relay, so that water shuts off without the participation of the controller. To prevent the tap from becoming soured, every two weeks at night it automatically opens and closes. Drive Automation Diagram When Philio Smokes a Sensor




the ventilation is turned off so as not to inflate the fire. At the same time, a message is sent to the telegram chat, and the sensor squeaks disgustingly.

Other sensors and devices



Authorize server


This module is a simple implementation of an authorization service.
There are two http-methods: one will check if the request came from the internal network and if so, it will authorize the user by issuing him a JWT token. The JWT token is encrypted using RSA 256, the private key must be registered in the file next to it. If the request is not from the internal network, the user will be redirected to the login page, where he must enter the login and password and log in. Since separation into users is not supposed, the login file and password in encrypted form (SHA 512) are nearby in the configuration file.

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 via a socket and pass authentication during authorization, subscribing to the topic, and publishing any message. The JWT token that was received from the authorization server comes in and is validated using the public key. For requests from the server, validation does not occur.

Custom Node-RED Nodes


When writing automation, it was required to implement one additional node and two to finalize.

I had to modify node-red-contrib-openzwave , since my pull request was accepted only after 7 months, but now you can use the library from npm. The refinement consisted of throwing a node removal event.



The MQTT node has also been finalized. In fact, the main refinement in it is pulling the id of the connected client and writing it back when sent to the queue. Here you can get by with the subflow functionality. It was also supposed to transfer any data to the input of the MQTT-node, but it was not useful.



To control the curtains and water taps, a schedule node was required. Available at that time in the library were too feature-rich and did not support a simple implementation using CRON. My implementation is quite simple: a schedule can be taken either at the input of a node or set internally. It is possible to cancel the schedule. Under the hood, node-schedule is used .



Front


Each active component of the apartment, which is wound up in automation, is a module at the front. Each module is self-contained and contains all the necessary components for rendering. Modules are divided into two aggregations: static and Z-Wave. 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>>;
}


Implementation 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 - you need to implement a data warehouse at the front. Such an implementation is inconvenient, in a good way it needs to be changed to one repository.

For Z-Wave-modules I organized a common repository and tracking of all changes on it.

The module implements the display in two ways:

  • for "large" screens (tablet, computer);
  • for phones.

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

The application is built using webpack + babel: according to the current settings, it is assembled for the last two versions of Firefox, Chrome, Chrome Android.

Features of source codes


Sources

All Flow are disabled, so as not to throw errors in the log. The configuration node for Z-Wave is deleted: it is buggy if it does not find the Z-Wave controller, and drops the entire Node-RED. It should look something like this:


For the work calendar loader to work, you need to register on data.gov.ru , get a token there and enter it after access_token:


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


To send telegram notifications, you need to get your bot, enter it in the Telegram configuration node and write the chat id where necessary. In the case of my Flow, this is subflow: The



sources are divided into front and back. In the back part lies:

  • Authorize server.
  • MQTT server.
  • Custom Node-RED Modules.
  • Exported Flows in one file, which can be either uploaded to your own account or a separate instance for tests.
  • A project for Node-RED, in which you need to install the modules and then only load the exported Flow (the modules are already in the project).
  • Backup Mongo-base.

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

Plans


The plans are to use the microphone of a smartphone or wall-mounted tablet for voice control. I plan to deploy my open-source system on a server based on CMU Sphinx .

I also plan to assemble a module for taking and transmitting readings of plumbing and electricity meters.

It is also worth considering the use of accumulated statistics for various parameters in order to optimize automation.

Conclusion


At the moment, I have achieved almost complete automation of the house to my needs - to maintain a comfortable climate and lighting in the house. Due to some automation, it was possible to reduce electricity consumption (about 1,500 ₽ in winter compared to the same equipment without automation), both from the side of heating elements and from the side of lighting.

Of course, there are still bugs in the algorithms, but I'll catch them someday, at least I'm moving in this direction. The system works stably, I do not see any brakes, despite the very low speed in the Z-Wave protocol.

Of course, the system will not pay for itself at the expense of savings, there is more a question of comfort. Such automation is very damp in terms of little things like closing and opening curtains, controlling light. When you are in an ordinary apartment, you sometimes forget to turn the lights on or off, and you don’t open the curtains at all.

I rarely adjust the climate system with my hands, but sometimes you need to turn the air conditioner on or off or adjust the temperature on the battery. In general, I am satisfied with the resulting system: it allows less time to spend on any adjustments. In the event of a controller failure, all functionality will remain in place (only automation will fall off). The functionality of the water shutoff will also be preserved. If I decide to sell the apartment, then I will easily remove all the automation. Or leave it in place, but selling more.

All Articles