Switch
The Switch component handles a switch (relay) output terminal with optional power metering capabilities. It uses Switch
as RPC namespace and provides the 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 status
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. |
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
: 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 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 switchfollow
andflip
are available only when the input has state (e.g.type
:switch
).
When setting a configuration through Switch.SetConfig() take 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 default valueundervoltage_limit
can be set from 0V (disabled) to thevoltage_limit
or tonull
to reset to default valuecurrent_limit
can be set from 0A to the max rated output current or tonull
to reset to 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) | ||||||||||||
| object | Information about the 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 switch command receives incorrect parameters or results in an error.
Accepted commands are:
status_update
- causes 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>
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,
"aenergy": {
"total": 11.679,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511972
},
"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,
"aenergy": {
"total": 11.679,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511972
},
"temperature": {
"tC": 53.3,
"tF": 127.9
}
}
}
{
"id": 0,
"source": "WS_in",
"output": false,
"apower": 0,
"voltage": 225.9,
"current": 0,
"aenergy": {
"total": 11.679,
"by_minute": [
0,
0,
0
],
"minute_ts": 1654511972
},
"temperature": {
"tC": 53.3,
"tF": 127.9
}
}
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