Skip to content

andyb2000/AutoMower-BLE-MQTT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 

Repository files navigation

AutoMower-BLE-MQTT

Python script for AutoMower-BLE for Husqvarna mowers to send status to MQTT and allow mow and dock commands

Purpose/Information

Due to the complexity of integrations for Home Assistant (HASS) and the constant changes to the ESPHome Bluetooth proxy, I created this simple script that can be ran on a Raspberry PI (Bluetooth models) that will communicate with your mower, connect to your MQTT server and publish status information. It will also subscribe to two topics that will allow for MOW and PARK.

Compatibility

This program uses the excellent library by @alistair23 at https://github.com/alistair23/AutoMower-BLE/ so this is a program requirement. All mowers supported by this Bluetooth Low Energy (BLE) library will work, take a look at the mower compatibility list in the repo for more information.

Requirements

You will need several python libraries: bleak asyncio json aiomqtt automower_ble

Install these using your package manager or pip3. If you are on a Raspberry Pi then use your package manager to install as this is the preferred method.

Clone this repo

git clone https://github.com/andyb2000/AutoMower-BLE-MQTT.git
cd AutoMower-BLE-MQTT

Create a virtual environment (Inside our git clone root)

python3 -m venv .venv
source .venv/bin/activate

Install packages

(.venv):~ $ pip3 install automower-ble
(.venv):~ $ pip3 install bleak
(.venv):~ $ pip3 install asyncio
(.venv):~ $ pip3 install aiomqtt

Configuration

Modify my script to change the variables near the start for your environment. OR, set environment variables to your shell by doing this for each one:

export MQTT_USERNAME="example"
Configuration Env value Description
LOCAL_LIB /usr/src/AutoMower-BLE.git You can custom/specify the location to the Automower git clone repo, this is to override the pip3 install location if you wish
MQTT_BROKER MQTT_HOST The host of your MQTT broker, normally this is your home assistant LAN IP address
MQTT_PORT MQTT_PORT This is the MQTT broker port, normally 1883
MQTT_USERNAME MQTT_USER Username for MQTT to authenticate with, normally mqtt
MQTT_PASSWORD MQTT_PASS Password for MQTT to authenticate with, this is set in Home Assistant
MQTT_BASE_TOPIC MOWER_BASE_TOPIC This creates the base topic used by MQTT, by default use this: homeassistant/mower/automower_ble
POLL_INTERVAL MOWER_POLL How often (in seconds) should we poll the mower for it's status. Don't set too rapid. 30-60seconds should do.
MOWER_ADDRESS MOWER_ADDRESS This is the MAC address of your mower. Find this in your app or by discovering with the library
MOWER_PIN MOWER_PIN This is the PIN as set on the mower, check the library out to find why or how to use this

Use

To use, simply run the script:

python3 mower_mqtt.py

To configure as a service

Create the Service File

sudo nano /etc/systemd/system/mower-mqtt.service

Paste, updating the fields

[Unit]
Description=Husqvarna Automower BLE to MQTT Bridge
After=network.target bluetooth.target

[Service]
Type=simple
User=<user>
WorkingDirectory=/home/<user>/AutoMower-BLE-MQTT
# Using the python executable inside your .venv ensures all libraries are found
ExecStart=/home/<user>/AutoMower-BLE-MQTT/.venv/bin/python3 /home/<user>/AutoMower-BLE-MQTT/mower_mqtt.py
Restart=always
RestartSec=20

[Install]
WantedBy=multi-user.target

Enable and Start

sudo systemctl daemon-reload
sudo systemctl enable mower-mqtt.service
sudo systemctl start mower-mqtt.service

How to check on it

journalctl -u mower-mqtt.service -f

Output

The script will produce the following MQTT output in Home Assistant. You can then use the sensors as you wish. The two switches created are for PARK and MOW.

image

Automower switch shows a power/off symbol which represents PARK.

The power/on symbol represents MOW and will start a custom mow duration of 1 Hour.

MowerState is probably the most important one to use in your automations, that will change to one of the following:

MowerState Description
OFF Mower is powered off
WAIT_FOR_SAFETYPIN Waiting for PIN on mower
STOPPED Stopped requiring manual intervention
FATAL_ERROR A critical error has occurred with the mower
PENDING_START About to begin an action
PAUSED Paused by user
IN_OPERATION Operating, the actual action is in MowerActivity
RESTRICTED Waiting for an action either in calendar or to release from PARK
ERROR An error has occurred and mower needs manual intervention

Other useful sensors:

Automower Battery - in percentage

Automower Charging - 1 for charging, 0 for not charging

Automower CurrUpdateSchedule - Will show the last update from the mower via bluetooth, useful to check for freshness of data.

Automower NextStartSchedule - Will show the next date and time set in the mowers internal scheduler to begin mowing.

Authors

Acknowledgements

Changelog/Revisions

  • Added version to script for tracking (0.0.1)
  • 0.0.2 Add watchdog to main process so if it fails or hangs it will interrupt and gracefully exit setting HASS to offline state
  • 0.0.3 improvement to watchdog timer as it triggers when bluetooth comms going on
  • 0.0.4 improvements again and also to exit immediately on a failure to communicate
  • 0.0.5 New mow and park mqtt topics, set time to mow and more status information returned

About

Python script for AutoMower-BLE for Husqvarna mowers to send status to MQTT and allow mow and dock commands

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages