In your setup you may need to toggle a device or produce an output. This is where the aptly named toggle is useful. As the name implies you can use toggles to turn a component on or off. An example of a toggle could be a gpio pin connected to a LED you want to turn on and off.

Toggle Basics

A toggle is a MudPi component and must extend the base Toggle component class. The toggle can be toggled or turned directly on/off. The main purpose of toggles are producing output such as turning on a led or setting a GPIO pin output.

Available Interfaces

Below is a list of currently supported interfaces for toggles.

exampleToggles a boolean state in memoryLearn More
gpioTurn a linux supported board GPIO on or offLearn More
nanpyControl GPIO on a device running NanpyLearn More

If you would like at add support for another toggle interface read the developer docs and submit a PR!

Available Actions

In order to interact with a toggle you need to use the actions it provides (typically on a trigger). You can use the following actions:

toggleToggles the state from the current state
turn_offTurns the toggle state OFF
turn_onTurns the toggle state ON

Toggle Update Event

While you can listen to the StateUpdated event for all the toggles they also broadcast an event on the toggle topic as well. This event is broadcast for you to hook into a toggle and add additional functionality.

Event: ToggleUpdated

"event": "ToggleUpdated",
"component_id": "example_toggle_1",
"name": "Example Toggle 1",
"updated_at": "2021-03-14 09:34:33",
"state": 1,
"invert_state": 0

Controlling Toggles Through Events

Below are the events that a toggle is also listening for and how you can interact with toggles by sending an event to it.

topic: Based on value set for topic option in toggle configuration. Default: toggle/{key}

Switch Toggle

Send a Switch event to switch the toggle to a set state. The event expects a state in the data that is either 0 or 1 depending on if you want to turn it off or on.

    "event": "Switch",
    "data": {
    	"state": "1

Toggle the Toggle

Send a Toggle event to toggle the toggle to the opposite current state. No additional data is required.

    "event": "Toggle"

Turn Off the Toggle

Send a TurnOff event to turn_off the toggle. No additional data is required.

    "event": "TurnOff"

Turn On the Toggle

Send a TurnOn event to turn_on the toggle. No additional data is required.

    "event": "TurnOn"