Skip to content

Commit

Permalink
Home Assistant add-on (#327)
Browse files Browse the repository at this point in the history
* Move ha-addon files to repository

* Add repository json file

* Map raw usb access and uart devices

* Fix meter configuration naming

* Allow to use custom configuration for MQTT

* Fix syntax related to mqtt config

* Fix shothand if syntax

* Another try on syntax fix

* Fix mosquitto_pub.sh

* Fix mosquitto_pub.sh

* Refactor mqtt publisher script

* Clean config use

* Use explicit if

* Add docs for mqtt option
  • Loading branch information
to-masz committed Aug 28, 2021
1 parent de91a30 commit bba4cc4
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ha-addon/CHANGELOG.md
@@ -0,0 +1,5 @@
## 0.2.0
- Wmbusmeters configuration migrated to add-on page

## 0.1.0
- First versioned release of the add-on
103 changes: 103 additions & 0 deletions ha-addon/DOCS.md
@@ -0,0 +1,103 @@
# Home Assistant Community Add-on: wmbusmeters (W-MBus to MQTT)

This add-on allows you to acquire utility meter readings **without** the vendors bridge or gateway as long as they support C1, T1 or S1 telegrams using the wireless mbus protocol (WMBUS).



## Installation

The installation of this add-on is pretty straightforward and not different in
comparison to installing any other community-driven Home Assistant add-on.

1. Navigate to Supervisor > Add-on Store > Repositories
1. Add https://github.com/weetmuts/wmbusmeters/tree/master/ha-addon
1. Install Wmbusmeters
1. Plug-in your radio receiver USB dongle
1. Start the addon container
1. In the logs you should see all the W-Mbus telegrams that wmbusmeter is able to receive. <br> _If you don't see anything, check the logs carefully. <br> If you configure it late evening or in the night, please note the radio modules often send telegrams less frequently than in typical working hours or don't send them at all. <br> If your antenna is in a distance to the radio module, try to locate it closer._
1. You are ready to configure!
1. Finally, don't forget about adding the MQTT sensor into your Home Assistant.

## Configuration

Once the wmbusmeters is receiving the telegrams you need to configure your meter using `meters` option (see below) to pass the readings to MQTT topic.

#### Option: `meters`

Specify your meters usign wmbusmeters meter file format. The `driver` and `id` values can be read from the add-on logs after the initial start (with empty `meters` configuration). The `name` is your label for the meter and `key` is the encryption key to decrypt telegrams (if your meter use any).

See [project README for more information][github]

```yaml
meters:
- |-
name=MainWater
driver=apator162
id=01234567
key=00000000000000000000000000000000
```

#### Option: `conf`

The wmbusmeters configuration file. It will be used as `wmbusmeters.conf`.

See [project README for more information][github]


#### Option: `data_path`

Path relative for add-on where wmbusmeters files are stored:
```
/logs/meter_readings/
/etc/wmbusmeters.conf
/etc/wmbusmeters.d/
```

#### Option: `mqtt`

By default it is empty `{}` and leverages then the _Moquitto broker_ addon details provided by supervisor. However, you can specify the custom mqtt broker connection details here
```yaml
mqtt:
host: your-broker-host
port: 1883
user: your-username
password: your-password
```

## Home Assistant integration

Finally, you need to tell Home Assistant how to extract the readings from the MQTT. You can add the following sensor definition into your `sensor:` section of `configuration.yaml`.

```yaml
sensor:
- platform: mqtt
state_topic: "wmbusmeters/MainWater"
json_attributes_topic: "wmbusmeters/MainWater"
unit_of_measurement: "m3"
value_template: "{{ value_json.total_m3 }}"
name: Water usage
icon: "mdi:gauge"
```

_Please note: `MainWater` is the water meter name used in `meters` configuration._


## Support

Got questions?

You have several options to get them answered:

- [Open an issue here][issue] in project GitHub
- The Home Assistant [Community Forum][forum].
- Join the [Reddit subreddit][reddit] in [/r/homeassistant][reddit]

## Authors & contributors

[Wmbusmeters contributors][contributors]

[contributors]: https://github.com/weetmuts/wmbusmeters/graphs/contributors
[forum]: https://community.home-assistant.io/c/home-assistant-os/25
[github]: https://github.com/weetmuts/wmbusmeters
[issue]: https://github.com/weetmuts/wmbusmeters/issues
[reddit]: https://reddit.com/r/homeassistant
30 changes: 30 additions & 0 deletions ha-addon/Dockerfile
@@ -0,0 +1,30 @@
ARG BUILD_FROM
FROM $BUILD_FROM AS build

ENV LANG C.UTF-8

RUN apk add --no-cache alpine-sdk gcc linux-headers ncurses-dev librtlsdr-dev cmake libusb-dev

ADD https://api.github.com/repos/weetmuts/wmbusmeters/git/refs/heads/master version.json
RUN git clone https://github.com/weetmuts/wmbusmeters.git && \
git clone https://github.com/weetmuts/rtl-wmbus.git && \
git clone https://github.com/merbanan/rtl_433.git
WORKDIR /wmbusmeters
RUN make
WORKDIR /rtl-wmbus
RUN make release && chmod 755 build/rtl_wmbus
WORKDIR /rtl_433
RUN mkdir build && cd build && cmake ../ && make

FROM $BUILD_FROM AS scratch
ENV QEMU_EXECVE=1
RUN apk add --no-cache mosquitto-clients libstdc++ curl libusb ncurses rtl-sdr
WORKDIR /wmbusmeters
COPY --from=build /wmbusmeters/build/wmbusmeters /wmbusmeters/wmbusmeters
COPY --from=build /rtl-wmbus/build/rtl_wmbus /usr/bin/rtl_wmbus
COPY --from=build /rtl_433/build/src/rtl_433 /usr/bin/rtl_433

COPY run.sh /
RUN chmod a+x /run.sh

CMD ["/run.sh"]
7 changes: 7 additions & 0 deletions ha-addon/README.md
@@ -0,0 +1,7 @@
# Home Assistant Community Add-on: wmbusmeters (W-Mbus to MQTT)

Allows you to acquire utility meter readings **without** the vendors bridge or gateway as long as they support C1, T1 or S1 telegrams using the wireless mbus protocol (WMBUS).

It bridges WMBUS readings via MQTT. In this way you can integrate with whatever smart home infrastructure you are using.

See Documentation tab for more details.
38 changes: 38 additions & 0 deletions ha-addon/config.json
@@ -0,0 +1,38 @@
{
"name": "Wmbusmeters (W-MBus to MQTT)",
"version": "0.2.19",
"slug": "wmbusmeters",
"description": "Acquire utility meter readings using the wireless mbus protocol (WMBUS)",
"arch": ["armv7", "aarch64", "amd64"],
"url": "https://github.com/weetmuts/wmbusmeters",
"startup": "application",
"boot": "auto",
"devices": ["/dev/ttyUSB0", "/dev/ttyAMA0"],
"usb": true,
"uart": true,
"services": [
"mqtt:need"
],
"map": [
"share:rw",
"config:rw"
],
"options": {
"data_path": "/config/wmbusmeters",
"conf":
"loglevel=normal\ndevice=/dev/ttyUSB0:t1\nlogtelegrams=false\nformat=json\nlogfile=/dev/stdout\nshell=/wmbusmeters/mosquitto_pub.sh wmbusmeters/$METER_NAME $METER_JSON",
"meters": [],
"mqtt": {}
},
"schema": {
"data_path": "str",
"mqtt": {
"host": "str?",
"port": "port?",
"user": "str?",
"password": "str?"
},
"conf": "str",
"meters": [ "str" ]
}
}
Binary file added ha-addon/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ha-addon/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions ha-addon/run.sh
@@ -0,0 +1,56 @@
#!/usr/bin/with-contenv bashio

CONFIG_PATH=/data/options.json

CONFIG_DATA_PATH=$(bashio::config 'data_path')
CONFIG_CONF="$(jq --raw-output -c -M '.conf' $CONFIG_PATH)"
CONFIG_METERS="$(jq --raw-output -c -M '.meters' $CONFIG_PATH)"

echo "Syncing wmbusmeters configuration ..."
[ ! -d $CONFIG_DATA_PATH/logs/meter_readings ] && mkdir -p $CONFIG_DATA_PATH/logs/meter_readings
[ ! -d $CONFIG_DATA_PATH/etc/wmbusmeters.d ] && mkdir -p $CONFIG_DATA_PATH/etc/wmbusmeters.d
echo -e "$CONFIG_CONF" > $CONFIG_DATA_PATH/etc/wmbusmeters.conf

echo "Registering meters ..."
rm -f $CONFIG_DATA_PATH/etc/wmbusmeters.d/*
meter_no=0
IFS=$'\n'
for meter in $(jq -c -M '.meters[]' $CONFIG_PATH)
do
meter_no=$(( meter_no+1 ))
METER_NAME=$(printf 'meter-%04d' "$(( meter_no ))")
echo "Adding $METER_NAME ..."
METER_DATA=$(printf '%s\n' $meter | jq --raw-output -c -M '.')
echo -e "$METER_DATA" > $CONFIG_DATA_PATH/etc/wmbusmeters.d/$METER_NAME
done

echo "Generating MQTT configuration ... "
if bashio::config.exists "mqtt.host"
then
MQTT_HOST=$(bashio::config "mqtt.host")
if bashio::config.exists "mqtt.port"; then MQTT_PORT=$(bashio::config "mqtt.port"); fi
if bashio::config.exists "mqtt.user"; then MQTT_USER=$(bashio::config "mqtt.user"); fi
if bashio::config.exists "mqtt.password"; then MQTT_PASSWORD=$(bashio::config "mqtt.password"); fi
else
MQTT_HOST=$(bashio::services mqtt "host")
MQTT_PORT=$(bashio::services mqtt "port")
MQTT_USER=$(bashio::services mqtt "username")
MQTT_PASSWORD=$(bashio::services mqtt "password")
fi

echo "Broker $MQTT_HOST will be used."
pub_args=('-h' $MQTT_HOST)
[[ ! -z ${MQTT_PORT+x} ]] && pub_args+=( '-p' $MQTT_PORT )
[[ ! -z ${MQTT_USER+x} ]] && pub_args+=( '-u' $MQTT_USER )
[[ ! -z ${MQTT_PASSWORD+x} ]] && pub_args+=( '-P' $MQTT_PASSWORD )

cat > /wmbusmeters/mosquitto_pub.sh << EOL
#!/usr/bin/with-contenv bashio
TOPIC=\$1
MESSAGE=\$2
/usr/bin/mosquitto_pub ${pub_args[@]} -t \$TOPIC -m "\$MESSAGE"
EOL
chmod a+x /wmbusmeters/mosquitto_pub.sh

echo "Running wmbusmeters ..."
/wmbusmeters/wmbusmeters --useconfig=$CONFIG_DATA_PATH
5 changes: 5 additions & 0 deletions repository.json
@@ -0,0 +1,5 @@
{
"name": "Wmbusmeter",
"url": "https://github.com/weetmuts/wmbusmeters/",
"maintainer": "weetmuts"
}

0 comments on commit bba4cc4

Please sign in to comment.