wcbonner / GoveeBTTempLogger

Govee H5072, H5074, H5075, H5100, H5101, H5104, H5105, H5174, H5177, H5179, H5181, H5182, and H5183 Bluetooth Low Energy Temperature and Humidity Logger
MIT License
177 stars 26 forks source link

Feature Request: Push Values on to a MQTT Queue #58

Closed yonz2 closed 8 months ago

yonz2 commented 8 months ago

Hi,

I'm working on a remote data logger, that logs data from my Govee sensors. I would like have the RPI put the values read at a certain interval on to a MQTT queue.

I'm not a CPP developer, so I can't implement it myself and submit a pull request for a patch.

I did look into it, though, and using the Eclipse Paho Library and a few new parameters this should be a straight forward thing to do...

Parameters

[MQTT]
MQTT_URI=myserver.athome.com  
MQTT_PORT=1883 
MQTT_Username=myMQTTuser
MQTT_Password=myMQTTpassword
MQTT_TOPIC_PREFIX=goveebttemplogger
MQTT_INTERVAL=300

The routine should then every MQTT_INTERVAL seconds send the latest reading from each device as a JSON payload to the MQTT Topic: MQTT_TOPIC_PREFIX/<<Mac Address of Device>> (or use the filename prefix used for the log files, for consistency)

JSON Payload

{
  "temperature": 23.20,
  "humidity": 43.70,
  "battery": 100
}

On the other end we can then use e.g. Home Assistant's MQTT Sensor to process the data

Example Home Assistant configuration.yaml entry

mqtt:
  sensor:
    - name: "Temperature"
      state_topic: "goveebttemplogger/gvh-A4C13876CCXX"
      suggested_display_precision: 1
      unit_of_measurement: "°C"
      value_template: "{{ value_json.temperature }}
    - name: "Humidity"
      state_topic: "goveebttemplogger/gvh-A4C13876CCXX"
      unit_of_measurement: "%"
      value_template: "{{ value_json.humidity }}"
    - name: "Battery"
      state_topic: "goveebttemplogger/gvh-A4C13876CCXX"
      unit_of_measurement: "%"
      value_template: "{{ value_json.battery }}"

(Note: This config is just a basic template, needs to be tuned to the respective HA installation)

Just a thought.....

Best regards, Yonz

wcbonner commented 8 months ago

I don't see myself adding this to the core program. One of my design principles has been to limit the dependencies as much as possible.

I believe I'm currently only dependent on c++ std libraries and libbluetooth.

You could probably achieve what you want by monitoring the log directory for updates, then using a stream text editor (sed?) to manipulate the logged data and submit it to MQTT. E.g. "tail -f /var/log/govee/gvh-xxxxxx.txt"

I just thought of the tail -f command as an example of monitoring a text file for updates. I'm sure there are better methods in a language such as Perl that are designed to handle text better than C/C++.

yonz2 commented 8 months ago

Hi,

I see your point. It does make sense to keep the code clean and simple.

I created a Python script to do exactly what you suggested, with the addition of dynamically create the HA Devices when the GoveeBTTempLogger encounters a new device (writes to a new log file)

My script is here: Datalogger