Multiple lamps switched by one Raspberry Pi Zero W

It is possible to switch more than one lamp with a single Raspberry Pi Zero W. The advantage is that it saves costs, but remember that you always need a cable to connect the lamp to the Raspberry Pi Zero W.

There are in fact quite a number of possibilities to switch multiple lamps with one Raspberry Pi Zero W.

Multiple lamps switched by one relay

Power strip

The simplest way to switch multiple lamps with one Raspberry Pi Zero W is to connect the lamps to a power strip. The relay then powers the entire power strip. As long as you don’t switch a total of more than 2 A, this is perfectly fine. At 230 V you can switch 460 VA. But beware, the power factor of the used lamps plays a significant role. According to the formula P(W) = S(VA) × PF, the power in watts decreases as the power factor (PF) decreases. Incandescent lamps have a power factor of 1 (so the power in watts is equal to the power in volt-amperes), but compact fluorescent lamps can have a power factor as low as 0.5 (leaving only 230 W out of 460 VA). Good quality LED lamps can have a power factor of 0.9 to 0.95. In that case you can switch, for example, 82 LED lamps of 5 W with one 2 A relay (460 VA × 0.9 / 5 W = 414 / 5 = 82.8).

It’s a cheap solution, but there is the disadvantage (apart from the number of cables) that all of the lamps switch on and off simultaneously.

Multiple lamps switched by more than one relay

You can also switch multiple lamps by using multiple relays. This is a configuration option in the configuration file /etc/etha/etha-light-switch.conf (by default). In it you can specify (for example):

gpio_relay = [25, 26]

You can specify as many ports as there are GPIO ports and relays left. Of course the relays still don’t switch separately, so this solution doesn’t have many things to offer. Also note that in this case you’ll need a different housing than the connector housing I used.

Multiple relays switched by more than one ETHA Light Switch Python program

It is possible to run more than one ETHA Light Switch Python program on the same Raspberry Pi Zero W. This is accomplished by specifying an argument on the command line:

/opt/etha/etha-light-switch -d /path/to/configuration_directory

or:

/opt/etha/etha-light-switch --config-dir /path/to/configuration_directory

Without the --config-dir argument the configuration directory defaults to the /etc/etha directory. This way you can specify a second (and third…) configuration directory with its own configuration and clock rules files. To start more than one ETHA Light Switch Python program you’ll need more than one systemd service file. For that you can copy the file /etc/systemd/system/etha-light-switch.service that you’ve already downloaded (see here) to the same directory (/etc/systemd/system), but with a different filename (the file’s filename extension must remain service). In that service file you simply add the --config-dir argument pointing to your extra configuration directory.

There are some caveats though:

  • In the [gpio] section you’ll need to specify other GPIO ports for every option, or 0 if you don’t have multiple LEDs or buttons (I assume you have at least two relays, otherwise there’s no point to this exercise). Technically there is no objection to specifying double GPIO ports, but you will get some strange results for sure. Specifying the same gpio_button for more than one instance of the ETHA Light Switch Python program will certainly leave you confused at some point in time. Pressing the button can switch one lamp on and another one off at the same time, for instance.
  • Other options in the configuration file need to be different as well (light_name, mqtt_topic_identification). For group_name and mqtt_topic_group the same values can be used.
  • The same clock_rules_filename can be used, because there are now more than one configuration directories, each with its own clock rules file.

ETHA Light Switch Python program satellites

The last option to switch multiple lamps independent of each other is to use the so-called --satellite (or -s) command line argument:

/opt/etha/etha-light-switch --satellite {satellite-name}

A satellite is defined here as a stand-alone process using the same configuration directory as the main process. The satellite process requires a separate systemd service file (see above). In that process the ETHA Light Switch Python program will look for a section named [{satellite-name}] (any name, apart from the defined section headers, will do) in the configuration file. The main ETHA Light Switch Python program (the one running without the --satellite argument) will skip this section altogether, but the --satellite version will first apply all options in the other sections ([common], [coordinates], [time], [mqtt] and [gpio]), and then override them with options in the [{satellite-name}] section. Here’s an example of such configuration file:

[common]
group_name = Living Room
light_name = Standing Lamp
button_send = True

[coordinates]
latitude = 53.219409
longitude = 6.568361

[time]
time_server = nl.pool.ntp.org

[mqtt]
mqtt_broker = 192.168.1.250
mqtt_broker_scheme = ssl
mqtt_username = etha
mqtt_password = something
mqtt_topic_base = home/lamps
mqtt_topic_group = livingroom
mqtt_topic_identification = standinglamp

[gpio]
gpio_light_switch_led = 24
gpio_nm_state_led = 18
gpio_relay = 7
gpio_button = 21

[tablelamp]
light_name = Table Lamp
default_flash_time = 30
clock_rules_filename = tablelamp
button_receive = True

mqtt_topic_identification = tablelamp

gpio_light_switch_led = 25
gpio_nm_state_led = 0
gpio_relay = 8
gpio_button = 0

So now you can start two instances of the ETHA Light Switch Python program, one without the --satellite command line argument and one with --satellite tablelamp. (You can specify multiple [{satellite-name}] sections to run more than one satellite process.) Notice that the options in the [tablelamp] section are now stand-alone options; the section headers of the main configuration are not used here.

Not all options can be used in the [{satellite-name}] section. Only the following options are valid:

  • light_name. Mandatory. Must be different to the value of light_name in [common].
  • default_flash_time. Optional. The default value is the value of default_flash_time in [common].
  • shift_solar_times. Optional. The default value is the value of shift_solar_times in [common].
  • clock_rules_filename. Optional. The default value is the value of clock_rules_filename in [common]. If this option is specified (and is different to the value of clock_rules_filename in [common]), then this satellite has it’s own (independent) clock rules. Otherwise the clock rules are the same.
  • button_receive. Optional. The default value is False. See below.
  • mqtt_topic_identification. Mandatory. Must be different to the value of mqtt_topic_identification in [mqtt].
  • gpio_light_switch_led. Optional. The default value is 0.
  • gpio_nm_state_led. Optional. The default value is 0.
  • gpio_relay. Optional. The default value is 0.
  • gpio_button. Optional. The default value is 0.

You may have noticed that now there are two extra options available:

  • button_send. Optional. The default value is False. This option must be specified in the [common] section. Setting this option to True means that button presses (from gpio_button in [common]) will be sent to all satellites via inter-process communication. Inter-process communication will be started, regardless of the value of button_send, when there are satellite processes defined in the configuration file. In certain cases extra commands will be sent through the inter-process communication as well.

  • button_receive. Optional. The default value is False. This option must be specified in the [{satellite-name}] section. The following commands will be received via inter-process communication and acted upon:

    1. Stop IPC thread. This is an internal command that stops the thread that listens to button_receive when the satellite process shuts down.
    2. Button press; light on. When the button that is controlled by the main process is pressed and the lamp in the main process is switched on, this command is received by all satellite processes that have button_receive enabled. The satellite process will then switch its relay on.
    3. Button press; light off. When the button that is controlled by the main process is pressed and the lamp in the main process is switched off, this command is received by all satellite processes that have button_receive enabled. The satellite process will then switch its relay off. Note that the situation that one lamp is switched on and the other one is switched off (as mentioned above) no longer exists. The satellite processes follow the state of the main lamp.
    4. Button press; flash. When the button that is controlled by the main process is long pressed and the lamp in the main process is switched to flash state, this command is received by all satellite processes that have button_receive enabled. The satellite process will then switch its relay to flash state. Note that for each lamp (either controlled by the main process or by a satellite) the flash times can be different.
    5. Reload tables. When the main process receives a reload (with systemctl reload etha-light-switch or from the ETHA Manager Android app), it will send this command via the inter-process communication mechanism to all satellites that use the same clock rules file. The satellite will then also reload this clock rules file.
    6. Reload configuration. When the configuration of the main process is updated with the ETHA Manager Android app, it will send this command via the inter-process communication mechanism to all satellites. The satellite will then also reload its configuration options.

Combining the above methods

Of cource you can combine any and all methods mentioned above.

Leave a Reply

Your email address will not be published.