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.
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.
Below is a list of currently supported interfaces for toggles.
Interface | Description | |
example | Toggles a boolean state in memory | Learn More |
gpio | Turn a linux supported board GPIO on or off | Learn More |
nanpy | Control GPIO on a device running Nanpy | Learn More |
If you would like at add support for another toggle interface read the developer docs and submit a PR!
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:
Action | |
toggle | Toggles the state from the current state |
turn_off | Turns the toggle state OFF |
turn_on | Turns the toggle state ON |
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
}
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}
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
}
}
Send a Toggle
event to toggle the toggle to the opposite current state. No additional data is required.
{
"event": "Toggle"
}
Send a TurnOff
event to turn_off
the toggle. No additional data is required.
{
"event": "TurnOff"
}
Send a TurnOn
event to turn_on
the toggle. No additional data is required.
{
"event": "TurnOn"
}