Switch
The Switch component handles a switch (relay) output terminal with optional power metering capabilities. It uses Switch
as an RPC namespace and provides the following methods:
Switch.Set
to control the output stateSwitch.Toggle
to toggle the output stateSwitch.SetConfig
to update the component's configurationSwitch.GetConfig
to obtain the component's configurationSwitch.GetStatus
to obtain the component's statusSwitch.ResetCounters
to reset component's energy counters (if applicable)
Switch components are identified with switch:<id>
in objects containing multiple component payloads.
Methods
Switch.SetConfig
Properties:
Property | Type | Description |
---|---|---|
| number | Id of the Switch component instance |
| object | Configuration that the method takes |
Find more about the config properties in config section
Switch.GetConfig
Properties:
Property | Type | Description |
---|---|---|
| number | Id of the Switch component instance |
Find the Switch.GetConfig response properties in config section
Switch.GetStatus
Properties:
Property | Type | Description |
---|---|---|
| number | Id of the Switch component instance |
Find more about the status response properties in status section
Switch.Set
This method sets the output of the Switch component to on or off. It can be used to trigger webhooks. More information about the events triggering webhooks available for this component can be found below.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the Switch component instance. Required |
| boolean | True for switch on, false otherwise. Required |
| number | Optional flip-back timer in seconds. Optional |
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| boolean | True if the switch was on before the method was executed, false otherwise. |
Switch.Toggle
This method toggles the output state. It can be used to trigger webhooks. More information about the events triggering webhooks available for this component can be found below.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the Switch component instance. Required |
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| boolean | True if the switch was on before the method was executed, false otherwise. |
Switch.ResetCounters
This method resets associated counters (if applicable).
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the Switch component instance. Required |
| array of strings | Array of strings, selects which counter to reset Optional |
If no 'type' is provided, the method will reset all available counters.
Response
Attributes in the result:
Property | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
| object | Information about the active energy counter prior to reset (shown if applicable)
| ||||||
| object | Information about the returned active energy counter prior to reset (shown if applicable)
|
HTTP Endpoint: /relay/id
Through this endpoint, a switch can be turned on/off with or without a timer. This can be used to trigger webhooks. More information about the events triggering webhooks available for this component can be found below.
Request
Parameters:
Property | Type | Description |
---|---|---|
| string | Action to be executed. Range of values: |
| number | A one-shot flip-back timer in seconds. |
Response
Received attributes:
Property | Type | Description |
---|---|---|
| boolean | True if the switch is turned on, false otherwise |
| boolean | True if the switch is turned on, false otherwise |
| number | Unix timestamp, start time of the timer (in UTC) |
| number | Duration of the timer in seconds |
| number | Time remaining (in seconds) until the request is executed |
| boolean | True if overpower condition occurred, false otherwise (shown if applicable) |
| string | Source of the last command, for example: |
Configuration
The configuration of the Switch component contains information about the input mode, the timers, and the protection settings of the chosen switch instance. To Get/Set the configuration of the Switch component its id
must be specified.
Property | Type | Description |
---|---|---|
| number | Id of the Switch component instance |
| string or null | Name of the switch instance |
| string | Mode of the associated input. Range of values: |
| string | Output state to set on power_on. Range of values: |
| boolean | True if the "Automatic ON" function is enabled, false otherwise |
| number | Seconds to pass until the component is switched back on |
| boolean | True if the "Automatic OFF" function is enabled, false otherwise |
| number | Seconds to pass until the component is switched back off |
| boolean | True if switch output state should be restored after over/undervoltage error is cleared, false otherwise (shown if applicable) |
| number | Id of the Input component which controls the Switch. Applicable only to Pro1 and Pro1PM devices. Valid values: |
| number | Limit (in Watts) over which overpower condition occurs (shown if applicable) |
| number | Limit (in Volts) over which overvoltage condition occurs (shown if applicable) |
| number | Limit (in Volts) under which undervoltage condition occurs (shown if applicable) |
| number | Number, limit (in Amperes) over which overcurrent condition occurs (shown if applicable) |
momentary
andcycle
: available only when the corresponding input is stateless (e.g.type
:button
)follow
: the state of the switch is the same as the state of the input (e.g. when the input is off => the switch is off)flip
: change of the state of the input causes a change of the state of the switch (e.g. when input is toggled the switch is also)detached
: the state of the input doesn't affect the state of the switchactivate
: available only on devices with physical inputfollow
andflip
are available only when the input has a state (e.g.type
:switch
).cycle
mode is available only forShellyPlus2PM
,ShellyPro2PM
andShellyPro2
When setting a configuration through Switch.SetConfig() takes into account that:
power_limit
can be set from 0W to the max rated output power or tonull
to reset to default valuevoltage_limit
can be set fromundervoltage_limit
to the max-rated output voltage or tonull
to reset to the default valueundervoltage_limit
can be set from 0V (disabled) to thevoltage_limit
or tonull
to reset to the default valuecurrent_limit
can be set from 0A to the max-rated output current or tonull
to reset to the default value
Status
The status of the Switch component contains information about the temperature, voltage, energy level, and other physical characteristics of the switch instance. To obtain the status of the Switch component its id
must be specified.
For switches with power metering capabilities, the status payload contains an additional set of properties with information about instantaneous power, supply voltage parameters, and energy counters.
Property | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| number | Id of the Switch component instance | ||||||||||||
| string | Source of the last command, for example: | ||||||||||||
| boolean |
| ||||||||||||
| number | Unix timestamp, start time of the timer (in UTC) (shown if the timer is triggered) | ||||||||||||
| number | Duration of the timer in seconds (shown if the timer is triggered) | ||||||||||||
| number | Last measured instantaneous active power (in Watts) delivered to the attached load (shown if applicable) | ||||||||||||
| number | Last measured voltage in Volts (shown if applicable) | ||||||||||||
| number | Last measured current in Amperes (shown if applicable) | ||||||||||||
| number | Last measured power factor (shown if applicable) | ||||||||||||
| number | Last measured network frequency in Hz (shown if applicable) | ||||||||||||
| object | Information about the active energy counter (shown if applicable)
| ||||||||||||
| object | Information about the returned active energy counter (shown if applicable)
| ||||||||||||
| object | Information about the temperature
| ||||||||||||
| array of type string | Error conditions occurred. May contain |
Webhook Events
Currently, there are two events related to the Switch component that can trigger webhooks:
switch.on
- produced when the switch changes its state fromoff
toon
switch.off
- produced when the switch changes its state fromon
tooff
MQTT Control
Since version 0.14.0
Shelly will subscribe to the following topics, accepting commands:
<topic_prefix>/command/switch:<id>
Shelly publishes on:
<topic_prefix>/status/switch:<id>
- topic where the result of thestatus_update
command is published. The command can be sent either on the common device topic or the component-specific topic. This topic is already used for existing status notifications if enabled.<topic_prefix>/error/switch:<id>
- error message is published if the switch command receives incorrect parameters or results in an error.
Accepted commands are:
status_update
- causes the status of the corresponding component to be published on its<topic_prefix>/status/switch:<id>
topic.on[,number]
- turns the switch on, optionaltoggle_after
parameter.off[,number]
- turns the switch off, optionaltoggle_after
parameter.toggle
- toggles the switch.
<topic_prefix>
is a custom prefix if set or defaults to <device_id>
Modbus registers
Coils
Coils add the ability to drive an output of the device. **
Address | Type | Description |
---|---|---|
100 | 1 bit | State and control of the device output * |
* Writing 1
to the coil sets the output to ON
, and writing 0
to the coil sets the output to OFF
. Reading the coil returns the current state of the output.
** Currently available only on ShellyPro3EM
with ShellyProOutputAddon
and ShellyProEM
To calculate the address of a coil corresponding to a Switch component on the device, use the following formula: coil_address = coils_offset + switch_id * coils_size
,
where:
coils_offset: fixed to 100 for coils.
switch_id: id of the Switch component of the device.
coils_size: fixed to 10 for coils.
Example:
An addon is added to the Pro3EM
device with switch_id: 100
to be controlled over ModBus, how to find the coil address:
coil_address = offset + switch_id * coils_size = 100 + 100 * 10 = 1100
the coil can be read or written on the address 1100
Examples
Switch.Set example
- Switch.Set HTTP GET Request
- Switch.Set Curl Request
- Switch.Set Mos Request
http://192.168.33.1/rpc/Switch.Set?id=0&on=true
curl -X POST -d '{"id":1,"method":"Switch.Set","params":{"id":0,"on":true}}' http://${SHELLY}/rpc
mos --port ${PORT} call Switch.Set '{"id":0,"on":true}'
Response
- Switch.Set HTTP GET Response
- Switch.Set Curl Response
- Switch.Set Mos Response
{
"was_on": false
}
{
"id": 1,
"src": "shellypro4pm-f008d1d8b8b8",
"params": {
"was_on": false
}
}
{
"was_on": false
}
Switch.Toggle example
- Switch.Toggle HTTP GET Request
- Switch.Toggle Curl Request
- Switch.Toggle Mos Request
http://192.168.33.1/rpc/Switch.Toggle?id=0
curl -X POST -d '{"id":1,"method":"Switch.Toggle","params":{"id":0}}' http://${SHELLY}/rpc
mos --port ${PORT} call Switch.Toggle '{"id":0}'
Response
- Switch.Toggle HTTP GET Response
- Switch.Toggle Curl Response
- Switch.Toggle Mos Response
{
"was_on": false
}
{
"id": 1,
"src": "shellypro4pm-f008d1d8b8b8",
"params": {
"was_on": false
}
}
{
"was_on": false
}
Switch.SetConfig example
- Switch.SetConfig HTTP GET Request
- Switch.SetConfig Curl Request
- Switch.SetConfig Mos Request
http://192.168.33.1/rpc/Switch.SetConfig?id=0&config={"name":"Switch0"}
curl -X POST -d '{"id":1,"method":"Switch.SetConfig","params":{"id":0,"config":{"name":"Switch0"}}}' http://${SHELLY}/rpc
mos --port ${PORT} call Switch.SetConfig '{"id":0,"config":{"name":"Switch0"}}'
Response
- Switch.SetConfig HTTP GET Response
- Switch.SetConfig Curl Response
- Switch.SetConfig Mos Response
{
"restart_required": false
}
{
"id": 1,
"src": "shellypro4pm-f008d1d8b8b8",
"params": {
"restart_required": false
}
}
{
"restart_required": false
}
Switch.GetConfig example
- Switch.GetConfig HTTP GET Request
- Switch.GetConfig Curl Request
- Switch.GetConfig Mos Request
http://192.168.33.1/rpc/Switch.GetConfig?id=0
curl -X POST -d '{"id":1,"method":"Switch.GetConfig","params":{"id":0}}' http://${SHELLY}/rpc
mos --port ${PORT} call Switch.GetConfig '{"id":0}'
Response
- Switch.GetConfig HTTP GET Response
- Switch.GetConfig Curl Response
- Switch.GetConfig Mos Response
{
"id": 0,
"name": "Switchdsds",
"in_mode": "follow",
"initial_state": "match_input",
"auto_on": false,
"auto_on_delay": 60,
"auto_off": false,
"auto_off_delay": 60,
"autorecover_voltage_errors": false,
"power_limit": 4480,
"voltage_limit": 280,
"undervoltage_limit": 0,
"current_limit": 16
}
{
"id": 1,
"src": "shellyplus1pm-441793ce3f08",
"params": {
"id": 0,
"name": "Switchdsds",
"in_mode": "follow",
"initial_state": "match_input",
"auto_on": false,
"auto_on_delay": 60,
"auto_off": false,
"auto_off_delay": 60,
"autorecover_voltage_errors": false,
"power_limit": 4480,
"voltage_limit": 280,
"undervoltage_limit": 0,
"current_limit": 16
}
}
{
"id": 0,
"name": "Switchdsds",
"in_mode": "follow",
"initial_state": "match_input",
"auto_on": false,
"auto_on_delay": 60,
"auto_off": false,
"auto_off_delay": 60,
"autorecover_voltage_errors": false,
"power_limit": 4480,
"voltage_limit": 280,
"undervoltage_limit": 0,
"current_limit": 16
}
Switch.GetStatus example
- Switch.GetStatus HTTP GET Request
- Switch.GetStatus Curl Request
- Switch.GetStatus Mos Request
http://192.168.33.1/rpc/Switch.GetStatus?id=0
curl -X POST -d '{"id":1,"method":"Switch.GetStatus","params":{"id":0}}' http://${SHELLY}/rpc
mos --port ${PORT} call Switch.GetStatus '{"id":0}'
Response
- Switch.GetStatus HTTP GET Response
- Switch.GetStatus Curl Response
- Switch.GetStatus Mos Response
{
"id": 0,
"source": "WS_in",
"output": false,
"apower": 0,
"voltage": 225.9,
"current": 0,
"freq": 50,
"aenergy": {
"total": 11.679,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511972
},
"ret_aenergy": {
"total": 5.817,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511615
},
"temperature": {
"tC": 53.3,
"tF": 127.9
}
}
{
"id": 1,
"src": "shellyplus1pm-441793ce3f08",
"params": {
"id": 0,
"source": "WS_in",
"output": false,
"apower": 0,
"voltage": 225.9,
"current": 0,
"freq": 50,
"aenergy": {
"total": 11.679,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511972
},
"ret_aenergy": {
"total": 5.817,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511615
},
"temperature": {
"tC": 53.3,
"tF": 127.9
}
}
}
{
"id": 0,
"source": "WS_in",
"output": false,
"apower": 0,
"voltage": 225.9,
"current": 0,
"freq": 50,
"aenergy": {
"total": 11.679,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511972
},
"ret_aenergy": {
"total": 5.817,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511615
},
"temperature": {
"tC": 53.3,
"tF": 127.9
}
}
Switch.ResetCounters example
- Switch.ResetCounters HTTP GET Request
- Switch.ResetCounters Curl Request
- Switch.ResetCounters Mos Request
http://192.168.33.1/rpc/Switch.ResetCounters?id=0&type=["aenergy","ret_aenergy"]
curl -X POST -d '{"id":1,"method":"Switch.ResetCounters","params":{"id":0,"type":["aenergy","ret_aenergy"]}}' http://${SHELLY}/rpc
mos --port ${PORT} call Switch.ResetCounters '{"id":0,"type":["aenergy","ret_aenergy"]}'
Response
- Switch.ResetCounters HTTP GET Response
- Switch.ResetCounters Curl Response
- Switch.ResetCounters Mos Response
{
"aenergy": {
"total": 11.679
},
"ret_aenergy": {
"total": 5.817
}
}
{
"id": 1,
"src": "shellyplus1pm-441793ce3f08",
"params": {
"aenergy": {
"total": 11.679
},
"ret_aenergy": {
"total": 5.817
}
}
}
{
"aenergy": {
"total": 11.679
},
"ret_aenergy": {
"total": 5.817
}
}
HTTP Endpoint example
Example:
curl http://${SHELLY}/relay/0?turn=on
Example:
{
"ison": true,
"has_timer":false,
"timer_started_at": 0,
"timer_duration": 0.00,
"timer_remaining": 0.00,
"overpower": false,
"source": "http"
}
Request status over MQTT example
export MQTT_SERVER="broker.hivemq.com"
export MQTT_PORT=1883
export SHELLY_ID="shellyplus1-a8032abe54dc" # The <shelly-id> of your device
mosquitto_sub -h ${MQTT_SERVER} -p ${MQTT_PORT} -t ${SHELLY_ID}/status/switch:0
export MQTT_SERVER="broker.hivemq.com"
export MQTT_PORT=1883
export SHELLY_ID="shellyplus1-a8032abe54dc" # The <shelly-id> of your device
mosquitto_pub -h ${MQTT_SERVER} -p ${MQTT_PORT} -t ${SHELLY_ID}/command/switch:0 -m status_update
{
"id": 0,
"source": "init",
"output": false,
"temperature": {
"tC": 46.4,
"tF": 115.6
}
}
Turn switch on with toggle_after over MQTT example
export MQTT_SERVER="broker.hivemq.com"
export MQTT_PORT=1883
export SHELLY_ID="shellyplus1-a8032abe54dc" # The <shelly-id> of your device
mosquitto_pub -h ${MQTT_SERVER} -p ${MQTT_PORT} -t ${SHELLY_ID}/command -m on,10
Additional information
Cycle
in_mode explanation
in_mode: cycle
(if applicable) is an input mode, on devices with two inputs and two outputs like the
ShellyPlus2PM
, ShellyPro2PM
and ShellyPro2
.
Operation principle: at a push of a button with active cycle
in_mode, the sequence is: SW:0-ON && SW:1-OFF, SW:0-OFF && SW:1-ON,
SW:0-ON && SW:1-ON, SW:0-OFF && SW:1-OFF.
Activate
in_mode explanation
in_mode: activate
(if applicable) is an input mode, on devices with a physical input. This mode is designed to be used with PIR sensors.
Operation principle: at a push of a button with active activate
in_mode, the output designated to the input is triggered ON, and if
an auto-off timer is set, it is started/restarted at every trigger of the input.