Skip to content

Lights example 0 objectives

Andre Hartman edited this page Oct 27, 2019 · 15 revisions

Control lights with Homebridge and Node-RED

Background

I am an absolute beginner and I find node-red + homebridge a steep learning curve. I will give some examples in these wiki pages as I go along and build my solution to help other newbies to make their first steps easier. These pages presume that you have a working installation of Homekit, Homebridge, Node-RED and the node-red-contrib-homebridge-automation component. You should also know something about Javascript and JSON objects.

So bear with me and let us have some fun with node-red.

The Situation

This is about controlling lighting for a professional person seeing customers by appointment in his office. The office has an entrance hall, an appointments room and exterior parking space. Lighting is as follows:

  1. Exterior parking: A lamppost, switched by a wall switch at the front door.
  2. Entrance hall: 3 ceiling lights, switched by 2-gang wall switches at the front door and the appointments room door.
  3. Appointments room: 6 ceiling lights. These are IKEA Tradfri white spectrum lights; they can be switched, dimmed and the color temperature can be varied between cool and warm white. They are controlled by an IKEA wireless controller.

Instrumenting for automation

The Exterior parking light and the Entrance hall are still 'dumb' and need instrumentation to make these available to automation. It seems obvious to use IKEA on/off lightbulbs, considering there is already an IKEA Gateway in place but this is not the best way forward. Doing so would render the existing wall switches obsolete. Automation should help manual use, not replace it; the wall switches will need to stay.

We will use Shelly switches for this. They are a recent product - from Bulgaria - and can be easily controlled by Homebridge. These switches have three unique selling points:

  1. Shelly switches are very small and fit behind a European wall switch.
  2. These switches have a separate 'SW' input; by connecting the existing wall switch to this, the wall switch and automation will work in parallel.
  3. They are very cheap at €10 for a single and €20 for a double switch.

Lighting scenes

  1. Morning (before sunrise). Hall lights on, Room lights on, at 100% brightness and normal white, Exterior lights on.
  2. Day. Hall lights on, Room lights on, at 100% brightness and normal white, Exterior lights off.
  3. Night (after sunset). Hall lights on, Room lights on, at 75% brightness and warm white, Exterior lights on.

Software: Homebridge components

It is necessary to check if there are appointments as well as the sun's position to control the lighting scenes appropriately.

Appointments are held in a Apple iCloud Calendar. Currently an AppleScript scans the appointments calendar for the day and write one single event in a special calendar, starting 30 minutes before the first appointment and ending 30 minutes after the last one; it is this calendar that is monitored by the 'homebridge-calendar' components. This logic may be brought into node-red later but such is the current situation.

'homebridge-calendar' monitors this calendar and fakes a contact sensor that is open when there is an on-going event and closed when not 'homebridge-automation-calendar' fakes another sensor holding values about the sunrise position. One such value is 'Timeoftheday': 1 (Dawn), 2 (Sunrise), 3 (Daylight), 4 (Sunset), 5 (Dusk), 6 (Night).

Lighting needs to be set 30 minutes before the first appointment to welcome early arrivals and needs to be switched off 30 minutes after the last appointment for the day. Additionally, a reaction is necessary if sunrise or sunset occur during appointments as the desired lighting scenes needs to be adapted.

The node-red-contrib-homebridge-automation component

This component operates between Node-RED and Homebridge; it does not 'know' anything about accessories defined in Homekit itself. In a situation where an IKEA Gateway with lightbulb 1 is defined as an accessory in Homekit and another IKEA Gateway with lightbulb 2 is defined in Homebridge and exposed via Homebridge to Homekit, the user will not experience any difference between lightbulbs 1 and 2. However, only lightbulb 2 will be visible and controllable in Node-RED.

Homekit model

Notes

  1. Homebridge and Node-RED reside on a server that is always on. In this case - as in the majority of Homebridge/Node-RED instances - a Raspberry Pi SBC (single board computer) is used.
  2. There will more ways to automate this situation with other Homebridge components or more clever use of Node-RED. However, the pages are not about finding the best way. They explain how I solved this in order to be of help to other people that want to start and use these fascinating components.

Following pages: Example 1 : switch a light