Skip to content

Controller for starting and stopping specific devices on the network via MQTT.

License

Notifications You must be signed in to change notification settings

lunarys/mqtt-device-controller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MQTT device controller

What does this do?

This python script executed in a docker container manages the start and shutdown of devices I do not always need. The controller is built to work autonomously.

The current state of the controlled device is tracked via MQTT and MQTT topics are subscribed for messages from other devices and users. The controller acts differently based on different conditions:

  • The device is already running: Add the device or user to the active instances and wait until they are done.
  • The device is disabled: Do not start the device and notify the user about this.
  • The device is not running:
    • Start of the device is desired: Start the device and tell the user to wait for a notification when the device is online.
    • Only use the device if online: Notify the user that the device is not running.
  • All devices are done: Wait for a configured time in case the device is required again, then shut it down.

The current state of active devices and users is also observed in order to handle a shutdown or crash of those accordingly.

Related projects

Configuration

Everything can be configured via the docker-compose .env file, which is provided as a template.

Option Default Description
DEVICE_NAME The name of the controlled device. Determines the MQTT topic as described below.
MQTT_BROKER localhost The hostname of the MQTT broker.
MQTT_USER The username for the MQTT broker.
MQTT_PASSWORD The password for the MQTT broker.
MQTT_PORT 1883 The port for the MQTT broker.
MQTT_QOS 1 The quality of service for the MQTT broker.
WAIT_AFTER_START 600 The amount of seconds to wait for users after the device started.
WAIT_BEFORE_STOP 300 The amount of seconds to wait before shutting down the controlled device after all users are done.
MINIMUM_RUN_TIME 600 Keep the device running for at least this amount of seconds. Enables shorter times for WAIT_BEFORE_STOP.

More options are theoretically supported to change subscription and publishing topics, but not listed here.

Default topics:

Topic Description
device/$DEVICE_NAME/status The status topic for the controlled device.
device/$DEVICE_NAME/status/retained The topic for receiving the state of the remote device retained. ONLINE, OFFLINE or CRASHED.
device/$DEVICE_NAME/status/desired The topic for interacting with the remote device. Send ON or OFF.
device/$DEVICE_NAME/controller/status The status of the controller. ENABLED or DISABLED.
device/$DEVICE_NAME/controller/status/desired Set the status of the controller. ENABLE or DISABLE.
device/$DEVICE_NAME/controller/status/active A list of users using the device currently.
device/$DEVICE_NAME/contoller/status/waiting A list of users waiting for the start of the controlled device.
device/$DEVICE_NAME/controller/to/$USER Outgoing messages to $USER
device/$DEVICE_NAME/controller/from/$USER Incoming messages from $USER
device/$USER/status The observed topic for $USER. Listens for OFFLINE or CRASHED

About

Controller for starting and stopping specific devices on the network via MQTT.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published