A YAML-based automation engine for SwitchBot BLE devices with a Prometheus exporter.
Project description
SwitchBot Actions: A YAML-based Automation Engine
switchbot-actions is a lightweight, standalone automation engine that turns a single config.yaml file into a powerful local controller for your SwitchBot BLE devices. React to device states, create time-based triggers, and integrate with MQTT and Prometheus—all with a simple, configuration-driven approach.
At its core, switchbot-actions monitors various input sources and, based on your rules, triggers a wide range of actions.
Its efficiency makes it a great fit for resource-constrained hardware like a Raspberry Pi Zero, allowing you to build a sophisticated, private, and reliable home automation hub without needing a large, all-in-one platform.
Key Features
- Powerful Automation Rules: Define complex automations with a unified
if/thenstructure. - Inter-Device Communication: Create rules where one device's state triggers an action based on the state of another device.
- Direct Device Control: A
switchbot_commandaction allows you to directly control any SwitchBot device, enabling powerful, interconnected automations. - Event-Driven & Time-Based Triggers: React to state changes instantly or trigger actions only when a condition has been met for a specific duration (e.g., "if a door has been open for 5 minutes").
- Full MQTT Integration: Use MQTT messages as triggers and publish messages as an action.
- Prometheus Exporter: Exposes device metrics at a configurable
/metricsendpoint. It also provides a specialswitchbot_device_infometric, which allows you to use your custom device names in PromQL queries for improved readability. For technical details and query examples, please see the Project Specification.-
switchbot_device_infometric: This metric provides metadata about configured SwitchBot devices, including theiraddress, user-definedname(alias), andmodel. Its value is always1and it's useful for joining with other metrics to make queries more readable.Example PromQL Query (to get temperature by device name):
switchbot_temperature * on(address) group_left(name) switchbot_device_info{name="living_room_meter"}
-
Prerequisites
- Python: Version 3.11 or newer is required.
- Operating System: The application is tested and supported on Linux. It is also expected to work on other platforms like macOS and Windows, but BLE functionality may vary depending on the system's Bluetooth support.
- Permissions (Linux): The application needs permissions to access the Bluetooth adapter. This can typically be achieved by running with sudo or by setting the necessary capabilities (see the Deployment Guide).
- Supported Architectures (Docker): linux/amd64, linux/arm64
Quick Start
1. Installation
We strongly recommend installing with pipx to keep your system clean and avoid dependency conflicts.
# Install pipx
pip install pipx
pipx ensurepath
# Install the application
pipx install switchbot-actions
2. Configuration
Create a config.yaml file. Start with this example that showcases inter-device automation: it turns on a fan only if the room is hot and the window is closed.
# config.yaml
# 1. Define aliases for your devices for easy reference.
devices:
office-meter:
address: "aa:bb:cc:dd:ee:ff" # Your temperature sensor's address
office-window:
address: "11:22:33:44:55:66" # Your contact sensor's address
automations:
- name: "Turn on Fan when Hot and Window is Closed"
if:
# This rule triggers when the temperature goes above 28 degrees.
source: "switchbot"
device: "office-meter"
conditions:
temperature: "> 28.0"
# And at that moment, check the state of the window sensor.
office-window.contact_open: false
then:
# In a real scenario, you would control a fan here.
# This example just logs a message.
type: "log"
level: "WARNING"
message: "Room is hot and window is closed. Turning on fan..."
3. Run
Launch the application with your configuration. Use the -vvv flag first to ensure your devices are discovered.
# Run with sudo if you encounter permission errors
switchbot-actions -vvv -c config.yaml
What's Possible? (More Examples)
switchbot-actions enables highly context-aware automations.
- React to State Changes: Trigger an action only when a state changes. This is perfect for detecting button presses.
if:
source: "switchbot"
conditions:
# Triggers when the button_count is different from its previous value.
button_count: "!= {previous.button_count}"
- Time-Based Alerts: Send a notification if a door has been left open for more than 10 minutes.
if:
source: "switchbot"
duration: "10m"
conditions:
contact_open: True
- Full MQTT Integration: Control your devices with MQTT messages from other systems.
if:
source: "mqtt"
topic: "home/living/light/set"
conditions:
payload: "ON" # React to a simple string payload
- Execute Shell Commands Safely: Run external commands with arguments passed as a list, preventing command injection vulnerabilities.
then:
type: "shell_command"
command: ["echo", "Hello, {username}!"]
For a complete reference of all configuration options, please see the Project Specification.
Advanced Usage
-
Running as a Service: For continuous, 24/7 monitoring, we recommend running the application as a systemd service. View the Deployment Guide.
-
Running with Docker: The application is also available as a multi-arch Docker image. For detailed instructions on how to run it with Docker, please see the Deployment Guide.
-
Command-Line Overrides: You can temporarily override settings in your
config.yamlusing command-line flags. For example, to enable the Prometheus exporter and set its port:switchbot-actions --prometheus --prometheus-port 8080
To enable MQTT:
switchbot-actions --mqtt --mqtt-host my-broker
To disable the scanner (useful in CI/CD environments or if you don't need BLE scanning):
switchbot-actions --no-scannerRun
switchbot-actions --helpfor a full list of options, which are grouped by function (Scanner,MQTT,Prometheus) for clarity.For scanner settings, you can now specify
--scanner-durationand--scanner-waitinstead of--scanner-cycle. For example:switchbot-actions --scanner-duration 5 --scanner-wait 10
Robustness Features
switchbot-actions is designed for reliability:
- Fail-Fast Startup: The application performs critical resource checks at startup and exits with a clear error if a resource (e.g., a port) is unavailable.
- Configuration Reload with Rollback: Send a SIGHUP signal (sudo systemctl kill -s HUP switchbot-actions.service) to reload your configuration without downtime. If the new configuration is invalid, the application automatically rolls back to the last known good state.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file switchbot_actions-2.3.0.tar.gz.
File metadata
- Download URL: switchbot_actions-2.3.0.tar.gz
- Upload date:
- Size: 67.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35042a510f233d8ecf8ec89ef0943fcb30cffdd7c0cdcb8a4533b32e974ab73d
|
|
| MD5 |
73bda3a1f58adb894ab74ecad77b474a
|
|
| BLAKE2b-256 |
23e0195a9f8159301ec6986496c772a7aeccc8f2f3545b1d91ae532b36e383c7
|
Provenance
The following attestation bundles were made for switchbot_actions-2.3.0.tar.gz:
Publisher:
release.yml on hnw/switchbot-actions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
switchbot_actions-2.3.0.tar.gz -
Subject digest:
35042a510f233d8ecf8ec89ef0943fcb30cffdd7c0cdcb8a4533b32e974ab73d - Sigstore transparency entry: 855253085
- Sigstore integration time:
-
Permalink:
hnw/switchbot-actions@d000b36879688037170fdbb08e3a1c6861e03f49 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/hnw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d000b36879688037170fdbb08e3a1c6861e03f49 -
Trigger Event:
release
-
Statement type:
File details
Details for the file switchbot_actions-2.3.0-py3-none-any.whl.
File metadata
- Download URL: switchbot_actions-2.3.0-py3-none-any.whl
- Upload date:
- Size: 37.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6a935875e06253ed6d9561f27a41a007e694d0f92482a4c1ef67c1c5bfe65b2
|
|
| MD5 |
1aa721e6c97db92517a0d9772a2f1349
|
|
| BLAKE2b-256 |
a97296daabf68eed55f0c5cdfad1908d4fd5953561acc6d94ce8aa011601aaad
|
Provenance
The following attestation bundles were made for switchbot_actions-2.3.0-py3-none-any.whl:
Publisher:
release.yml on hnw/switchbot-actions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
switchbot_actions-2.3.0-py3-none-any.whl -
Subject digest:
c6a935875e06253ed6d9561f27a41a007e694d0f92482a4c1ef67c1c5bfe65b2 - Sigstore transparency entry: 855253087
- Sigstore integration time:
-
Permalink:
hnw/switchbot-actions@d000b36879688037170fdbb08e3a1c6861e03f49 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/hnw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d000b36879688037170fdbb08e3a1c6861e03f49 -
Trigger Event:
release
-
Statement type: