Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to customize the names for temperature, etc, used in the MQTT message #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ Copy or rename the `config.sample.ini` to `config.ini` in the `dbus-mqtt-tempera

OR

```json
{
"value": 22.0
}
```

OR

```json
22.0
```
Expand All @@ -77,6 +69,7 @@ OR
```
</details>

You can customize the names used in the MQTT message in the config.

## Install / Update

Expand Down
17 changes: 16 additions & 1 deletion dbus-mqtt-temperature/config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,20 @@ broker_port = 1883
;password = mypassword

; Topic where the temperature data as JSON string is published
; minimum required JSON payload: {"temperature": 22.0 } or {"value": 22.0}
; minimum required JSON payload: {"temperature": 22.0 }
topic = N/aa00aa00aa00/battery/1/System/Temperature2

; Temperature
; Set the name for temperature in the MQTT message
; default: temperature
temperature_name = temperature

; Humidity
; Set the name for humidity in the MQTT message
; default: humidity
humidity_name = humidity

; Pressure
; Set the name for pressure in the MQTT message
; default: pressure
pressure_name = pressure
23 changes: 12 additions & 11 deletions dbus-mqtt-temperature/dbus-mqtt-temperature.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
else:
type = 2

# get names used in the MQTT messages
temperature_name = str(config["MQTT"]["temperature_name"])
humidity_name = str(config["MQTT"]["humidity_name"])
pressure_name = str(config["MQTT"]["pressure_name"])

# set variables
connected = 0
Expand Down Expand Up @@ -136,26 +140,23 @@ def on_message(client, userdata, msg):

last_changed = int(time())

if "temperature" in jsonpayload or "value" in jsonpayload:
if "temperature" in jsonpayload:
temperature = float(jsonpayload["temperature"])
elif "value" in jsonpayload:
temperature = float(jsonpayload["value"])
if temperature_name in jsonpayload:
temperature = float(jsonpayload[temperature_name])

# check if humidity exists
if "humidity" in jsonpayload:
humidity = float(jsonpayload["humidity"])
if humidity_name in jsonpayload:
humidity = float(jsonpayload[humidity_name])

# check if pressure exists
if "pressure" in jsonpayload:
pressure = float(jsonpayload["pressure"])
if pressure_name in jsonpayload:
pressure = float(jsonpayload[pressure_name])

else:
logging.error('Received JSON MQTT message does not include a temperature object. Expected at least: {"temperature": 22.0} or {"value": 22.0}')
logging.error(f'Received JSON MQTT message does not include a temperature object. Expected at least: {{"{temperature_name}": 22.0}}')
logging.debug("MQTT payload: " + str(msg.payload)[1:])

else:
logging.warning('Received JSON MQTT message was empty and therefore it was ignored. Expected at least: {"temperature": 22.0} or {"value": 22.0} or 22.0')
logging.warning(f'Received JSON MQTT message was empty and therefore it was ignored. Expected at least: {{"{temperature_name}": 22.0}} or 22.0')
logging.debug("MQTT payload: " + str(msg.payload)[1:])

except TypeError as e:
Expand Down