This program allows a Telegram user to issue commands against devices managed by Hubitat through a Telegram bot.
Notable pros compared to alternatives are fine-grained access control and not requiring a VPN.
- Can issue commands to Hubitat devices by talking to a Telegram robot, e.g.,
/on Office Lightto turn on the device named "Office Light". - Can issue different types of command: on/off, lock/unlock, open/close, dim, ...
- Can get the status, capabilities and history of a device.
- Can give multiple names to devices, e.g.,
/on hwdoing the same as/on Hot Water. - Can act on multiple devices at once, e.g.,
/on hw, officeto turn on both "Hot Water" and "Office Light". - Can query and change Hubitat's mode or security monitor state, e.g.
/mode homeand/arm home. - Can expose different sets of devices and permissions to different groups of people, e.g., person managing Hubitat, family members and friends.
- A Hubitat hub
- A device, capable of running either Docker containers or Python, that is on the same LAN as the hub e.g., Raspbian or Windows
- Maker API app installed and configured in Hubitat
- A Telegram account to interact with the bot
- A Telegram bot. Use BotFather to create one
The app reads the settings from template.config.yaml, then config.yaml (if it exists), then environment variables
in the form HUBIBOT_KEY=value, then command line parameters in the form HUBIBOT_KEY=value.
In both cases value needs to be valid JSON compatible with the entries in template.config.yaml, e.g. 123, 'string', [ 'a', 'list', 'of', 'string'], etc.
The absolute path to the config file can be set via the HUBIBOT_CONFIG_FILE environment variable if it cannot be collocated with template.config.yaml.
Environment variables can be especially useful docker environments, such as TrueNAS via Launch Docker Image. All environment variables consumed by the app are all caps and prefixed with HUBIBOT_.
At the minimum, you'll need to specify the telegram and hubitat tokens, enable one user group (and put a valid user in it) and enable one device group. Assuming no config file, the minimal workable command-line incantation with debug logs enabled will look something like:
python main.py \
"HUBIBOT_MAIN_LOGVERBOSITY='DEBUG'" \
"HUBIBOT_TELEGRAM_TOKEN='8419043u1:fcdklasednfow8124'" \
"HUBIBOT_TELEGRAM_ENABLED_USER_GROUPS=['admins']" \
"HUBIBOT_TELEGRAM_USER_GROUPS_ADMINS_IDS=[ 123456 ]" \
"HUBIBOT_HUBITAT_ENABLED_DEVICE_GROUPS=['all']" \
"HUBIBOT_HUBITAT_URL='http://hubitat.local/'" \
"HUBIBOT_HUBITAT_APPID=51" \
"HUBIBOT_HUBITAT_TOKEN='fa763de0-9d0b-11ee-8c90-0242ac120002'"
And, assuming the config file is not used, the minimal workable docker setup will look something like:
sudo docker run -d \
--name my_hubibot \
-e "HUBIBOT_MAIN_LOGVERBOSITY='DEBUG'" \
-e "HUBIBOT_TELEGRAM_TOKEN='8419043u1:fcdklasednfow8124'" \
-e "HUBIBOT_TELEGRAM_ENABLED_USER_GROUPS=['admins']" \
-e "HUBIBOT_TELEGRAM_USER_GROUPS_ADMINS_IDS=[ 123456 ]" \
-e "HUBIBOT_HUBITAT_ENABLED_DEVICE_GROUPS=['all']" \
-e "HUBIBOT_HUBITAT_URL='http://hubitat.local/'" \
-e "HUBIBOT_HUBITAT_APPID=51" \
-e "HUBIBOT_HUBITAT_TOKEN='fa763de0-9d0b-11ee-8c90-0242ac120002'" \
vdbg/hubibot:latest
And a command line version adding an extra user group and device group:
python main.py \
"HUBIBOT_TELEGRAM_TOKEN='8419043u1:fcdklasednfow8124'" \
"HUBIBOT_MAIN_LOGVERBOSITY='DEBUG'" \
"HUBIBOT_TELEGRAM_ENABLED_USER_GROUPS=['admins','family']" \
"HUBIBOT_TELEGRAM_USER_GROUPS_ADMINS_IDS=[ 123 ]" \
"HUBIBOT_TELEGRAM_USER_GROUPS_FAMILY_IDS=[ 456 ]" \
"HUBIBOT_TELEGRAM_USER_GROUPS_FAMILY_ACCESS_LEVEL='SECURITY'" \
"HUBIBOT_TELEGRAM_USER_GROUPS_FAMILY_DEVICE_GROUPS=['regular']" \
"HUBIBOT_HUBITAT_ENABLED_DEVICE_GROUPS=['all','regular']" \
"HUBIBOT_HUBITAT_URL='http://hubitat.local/'" \
"HUBIBOT_HUBITAT_APPID=51" \
"HUBIBOT_HUBITAT_TOKEN='fa763de0-9d0b-11ee-8c90-0242ac120002'"
"HUBIBOT_HUBITAT_DEVICE_GROUPS_REGULAR_ALLOWED_DEVICE_IDS=[]" \
"HUBIBOT_HUBITAT_DEVICE_GROUPS_REGULAR_REJECTED_DEVICE_IDS=[ 302, 487, 522 ]"
See template.config.yaml for more details on configuration options.
To install, choose one of these 3 methods (using config file in these examples):
Dependency: Docker installed.
touch config.yaml- This will fail due to malformed config.yaml. That's intentional :)
sudo docker run --name my_hubibot -v "`pwd`/config.yaml:/app/config.yaml" vdbg/hubibot sudo docker cp my_hubibot:/app/template.config.yaml config.yaml- Edit
config.yamlby following the instructions in the file sudo docker start my_hubibot -i -e HUBIBOT_MAIN_LOGVERBOSITY=DEBUGThis will display logging on the command window allowing for rapid troubleshooting.Ctrl-Cto stop the container ifconfig.yamlis changed- When done testing the config:
sudo docker container rm my_hubibotsudo docker run -d --name my_hubibot -v "`pwd`/config.yaml:/app/config.yaml" --restart=always --memory=100m vdbg/hubibot- To see logs:
sudo docker container logs -f my_hubibot
Dependency: Docker installed.
git clone https://github.com/vdbg/hubibot.gitsudo docker build -t hubibot_image hubibotcd hubibotcp template.config.yaml config.yaml- Edit
config.yamlby following the instructions in the file - Test run:
sudo docker run --name my_hubibot -v "`pwd`/config.yaml:/app/config.yaml" hubibot_imageThis will display logging on the command window allowing for rapid troubleshooting.Ctrl-Cto stop the container ifconfig.yamlis changed - If container needs to be restarted for testing:
sudo docker start my_hubibot -i - When done testing the config:
sudo docker container rm my_hubibotsudo docker run -d --name my_hubibot -v "`pwd`/config.yaml:/app/config.yaml" --restart=always --memory=100m hubibot_image- To see logs:
sudo docker container logs -f my_hubibot
Dependency: Python 3.11+ and pip3 installed.
git clone https://github.com/vdbg/hubibot.gitcd hubibotcp template.config.yaml config.yaml- Edit
config.yamlby following the instructions in the file pip3 install -r requirements.txt- Run the program:
- Interactive mode:
python3 main.py - Shorter:
.\main.py(Windows) or./main.py(any other OS). - As a background process (on non-Windows OS):
python3 main.py > log.txt 2>&1 &
- To exit:
Ctrl-Cif running in interactive mode,killthe process otherwise.
From your Telegram account, write /h to the bot to get the list of available commands.
User and device groups allow for fine-grained access control, for example giving access to different devices to parents, kids, friends and neighbors.
While three user groups ("admins", "family", "guests") and three device groups ("all","regular","limited") are provided in the template config file as examples, any number of user and device groups are supported (names are free-form, alphabetical). If only one single user is using the bot, only keeping "admins" user group & "all" device group will suffice.
Device groups represent collection of Hubitat devices that can be accessed by user groups. In the template config file "admins" user group has access to "all" device group, "family" to "regular", and "guests" to "limited".
User groups represent collection of Telegram users that have access to device groups. User groups can contain any number of Telegram user ids (those with no user ids are ignored) and reference any number of device groups. User groups with an access_level set to:
NONE: cannot use any commands. Useful to disable a user group.DEVICE: can use device commands e.g.,/list,/regex,/on,/off,/open,/close,/dim,/status,/info.SECURITY: can use the same commands asaccess_level: DEVICE, and also act on locks with/lock&/unlockcommands, the/armcommand for Hubitat Safety Monitor, the/modecommand to view and change the mode, the/eventscommand to see a device's history, and the/tzcommand to change the timezone for/eventsand/lastevent.ADMIN: can use the same commands asaccess_level: SECURITY, and also admin commands e.g.,/users,/groups,/refresh,/exit. In addition some commands have more detailed output (e.g.,/list,/status).
A user can only belong to one user group, but a device can belong to multiple device groups and a device group can be referenced by multiple user groups.
For all commands taking in device names (such as : /on name of device), the app will:
- Split the input using the
device_name_separatorsetting inconfig.yamland remove all leading/trailing spaces. For example, " office light, bedroom " becomes "office light", "bedroom" - Look for these in the list of devices Hubitat exposes through MakerAPI that are accessible to the current user (see previous section).
If the
case_insensitivesetting inconfig.yamlis set totrue, then the case doesn't need to match. For example "office light" will be resolved to "Office Light" - For devices not found, the bot will try and interpret the input as a regex. For example, "(Office|Bedroom) Light" will resolve to both "Office Light" and "Bedroom Light"
- For devices still not found, the transforms in the
devicesetting underaliasessection inconfig.yamlare tried in order. For example, the app will transform "bedroom" to "bedroom light" and look for that name - If there are entries that still could not be found, the entire name resolution process fails.
/listuses the filter as a substring./regexuses the filter as a regex.
- Set
logverbosityundermaintoDEBUGinconfig.yamlto get more details. Note: Hubitat's token is printed in plain text whenlogverbosityisDEBUG - Ensure the bot was restarted after making changes to
config.yaml - Ensure the device running the Python script can access the Hubitat's Maker API by trying to access
<url>/apps/api/<appid>/devices?access_token=<token>url from that device (replace placeholders with values fromhubitatsection in config.yaml) - If a given device doesn't show up when issuing the
/listcommand:- Check that it is included in the list of devices exposed through Hubitat's MakerAPI
- Check that the
device_groups:<name>:allowed_device_idssetting inconfig.yamlfor the device group(s) of the current user is either empty or includes the device's id - If the device was added to MakerAPI after starting the bot, issue the
/refreshcommand - Check the device group(s) of the given user with the
/userscommand - Check that the device has a label in Hubitat in addition to a name. The former is used by the bot
The config.yaml file takes user Ids instead of user handles because the later are neither immutable nor unique.
There are two methods for getting a Telegram user Id:
- Ask that user to write to the @userinfobot to get their user Id
- Ensure
logVerbosityundermainis set toWARNINGor higher inconfig.yamland ask that user to write to the bot. There will be a warning in the logs with that user's Id and handle.
Style:
- From command line:
pip3 install black, - In VS code: Settings,
- Text Editor, Formatting, Format On Save: checked
- Python, Formatting, Provider:
black - Python, Formatting, Black Args, Add item:
--line-length=200