Skip to content

Switches

José Antonio Jiménez Campos edited this page May 17, 2021 · 45 revisions

A switch can be defined as a Switch or an Outlet. Both types have the same set of options available to them.

Type Service Type
1 Switch
2 Outlet

The following configuration is available for switches:

Section Key Description
Actions "0", "1" etc. The actions performed by the service
Digital Inputs "b" GPIOs that invoke specific actions
Inching Time "i" Time period before returning service to previous state
State & Status Inputs "f[n]" & "g[n]" Inputs that manage service state
ICMP Ping Inputs "q[n]" & "p[n]" Ping inputs that manage service state
Service Notifications "m" Notifications to send to another service
Initial State "s" State an service enters on boot
Maximum Use Time "d" Maximum time service can be on
Actions on Boot "xa" Enable / Disable execution of service actions on boot

Example

{
  "c": { "l": 13, "b": [{ "g": 0, "t": 5 }] },
  "a": [{
    "t": 1,
    "d": 3600,
    "s": 5,
    "0": { "r": [{ "g": 12, "v": 0 }] },
    "1": { "r": [{ "g": 12, "v": 1 }] },
    "b": [{ "g": 0 }]
  }]
}

This is an example of a Sonoff Basic unit configured as a switch ("t": 1), using GPIO 12 as the digital output to control the power relay in the device. The LED is defined as being connected to GPIO 13. A button is also defined for entering Setup Mode when the button on GPIO 0 is pressed for longer then 8 seconds.

A digital input is configured as being connected to GPIO 0 and two button actions are defined; "0" & "1". The first press of the button invokes action "0" and sets the pin to a logic low ("v": 0). The second press of the button invokes action "1" and sets the output to a logic high ("v": 1).

Set the type to 2 ("t": 2) if you would prefer the switch to appear in HomeKit as an Outlet e.g. when you have the device controlling a power socket or power strip.

The switch also has a maximum use time of 1 hour ("d": 3600) and its initial state when the service is powered on or rebooted will be the last state before it was rebooted (`"s":5').

NOTE: The following options are not needed in this example as they are the default options but are included to make the example more readable: "t": 1 & "v": 0

Actions

Key Action Description
"0" Off The default action after boot unless the Initial State has been set
"1" On

A switch has two actions. The Digital Outputs "r": [{}] for each should be configured to attain the desired state.

Maximum Use Time

Maximum use time is defined by the "d" key contained within the service object.

Key Value Description
"d" 0 Timer is disabled (default)
1 to ∞ Integer value specifying number of seconds

This option limits the maximum amount of time that the service can be in the ON state. The default is no limit.

When the timer is enabled then the service will be turned OFF after the maximum use time has elapsed.

NOTE: The value is an integer so no decimals :-)

Service Notifications

The list of Service Notifications "m" values supported are:

Key Value Notification
"v" 0 Switch OFF (default)
1 Switch ON
2 Switch state only OFF
3 Switch state only ON
-1 Reset maximum use time "d"

See the general Service Notifications section for details of how to configure these notifications.

Digital Inputs

Digital Inputs "b" are supported by this service.

See Digital Inputs for details on how to define this mandatory option.

State and Status Inputs

State inputs "f[n]" & Status Inputs "g[n]" are supported by this service. The supported list is:

Key Action
"f0" Perform action "0" (Off)
"f1" Perform action "1" (On)
"g0" Set service state to Off
"g1" Set service state to On

Refer to State Inputs for more detail and examples.

ICMP Ping Inputs

ICMP Ping inputs "p[n]" and "q[n]" are supported by this service. The supported list is:

Key Action
"p0" Perform action "0" (Off)
"p1" Perform action "1" (On)
"q0" Set service state to Off
"q1" Set service state to On

Refer to ICMP Ping Inputs for more detail.

Initial State

The Initial State key is supported by this service. Refer to Initial State for details of the available values.

Clone this wiki locally