Mijia Bluetooth Thermometer and Humidity - repohub/fhem GitHub Wiki

Bluetooth Thermometer and Humidity

Because the Rasperry Pi 2 onward has a Bluetooth interface I also added the Mijia Bluetooth Thermometer and Humidity to the FHEM installation. A nice feature of this devices is the capability of over-the-air (OTA) firmware flashing to extent the battery lifespan.

ATC

(Picture linked from https://github.com/pvvx/ATC_MiThermometer)

Helper scripts

Actually everything can be found in the Internet. A lot of people have done a pretty good work for this devices. First is to flash the firmware to the new ATC mode. How to flash the firmware is explained at https://github.com/atc1441/ATC_MiThermometer. The flasher itself is running on Chromium only. Be aware to enable #enable-experimental-web-platform-features to read the MAC. To enable experimental web platform features type chrome://flags in the address bar and hit enter. Now you can filter by feature name or scroll down to a particular feature that you want to enable. From dropdown menu selection Enabled option To the flash the software go to https://atc1441.github.io/TelinkFlasher.html.

The connection to Fhem is done by mqtt. To setup a mqtt callback I use the software of https://github.com/JsBergbau/MiTemperature2. On my first try there has been an issue with the Raspberry Pi firmware which is solved and explained at https://github.com/JsBergbau/MiTemperature2/issues/61.

python3 /home/pi/MiTemperature2/LYWSD03MMC.py --callback sendToMQTT.sh -a -b --devicelistfile /home/user/MiTemperature2/sensors.ini

To make it fully work two files had to be modified. First the sendToMQTT.sh which uses the application mosquitto_pub (sudo apt-get install mosquitto-clients) in a shell script. Everything should clear by the following example. $2 will be replaced by the sensorname defined in the sensors.ini, explained in the next part.

#!/bin/bash
mosquitto_pub -h 192.168.78.59 -t "house/EG/$2/ATC/temp" -u MQTTusername -P MQTTpw -m "$3"
mosquitto_pub -h 192.168.78.59 -t "house/EG/$2/ATC/humidity" -u MQTTusername -P MQTTpw -m "$4"
mosquitto_pub -h 192.168.78.59 -t "house/EG/$2/ATC/batteryvoltage" -u MQTTusername -P MQTTpw -m "$5"
mosquitto_pub -h 192.168.78.59 -t "house/EG/$2/ATC/batterylevel" -u MQTTusername -P MQTTpw -m "$6"
mosquitto_pub -h 192.168.78.59 -t "house/EG/$2/ATC/timestamp"  -u MQTTusername -P MQTTpw -m "$7"

sensor.ini actually translates only the MAC to a human readable name.

[A4:C1:38:12:34:56]
sensorname=kitchen

[A4:C1:38:78:9A:BC]
sensorname=sleepingroom

[A4:C1:38:DE:12:34]
sensorname=kidsroom

Fhem

Binding in fhem is done via simple mqtt. I use an external mqtt broker mosquitto (sudo apt-get install mosquitto).

ATC_fhem

Source from fhem.cfg

defmod kitchen_ATC MQTT_DEVICE
attr kitchen_ATC IODev myBroker
attr kitchen_ATC stateFormat {sprintf "<table><tr><td align='left'>Temp:</td><td align='right'>%.1f °C</td></tr><tr><td align='left'>Feuchte:</td><td align='right'>%d %%rH</td></tr><tr><td align='left'>Dewpoint:</td><td align='right'>%.1f °C</td></tr><tr><td align='left'>Voltage:</td><td align='right'>%.2f V</td></tr><tr><td align='left'>Batterie:</td><td align='right'>%d %%</td></tr></table>", ReadingsVal($name, "temp", 0),ReadingsVal($name, "humidity", 0),ReadingsVal($name, "dewpoint", 0),ReadingsVal($name, "batteryvoltage", 0),ReadingsVal($name, "batterylevel", 0);;}
attr kitchen_ATC subscribeReading_batterylevel house/EG/kitchen/ATC/batterylevel
attr kitchen_ATC subscribeReading_batteryvoltage house/EG/kitchen/ATC/batteryvoltage
attr kitchen_ATC subscribeReading_humidity house/EG/kitchen/ATC/humidity
attr kitchen_ATC subscribeReading_temp house/EG/kitchen/ATC/temp
attr kitchen_ATC subscribeReading_timestamp house/EG/kitchen/ATC/timestamp
attr room_ATC userReadings temp humidity

FTUI

ATC_FTUI

<li data-row="4" data-col="1" data-sizex="4" data-sizey="2">
    <div class="hbox">
    <div class="vbox fullsize">
        <div data-type="label" data-device="kitchen_ATC" data-get="temp" data-unit="&deg;C" class="left-narrow orange thin" style="margin-top: 10px;"></div>
        <div class="hbox top-narrow">
            <div class="top-narrow-2x mini left-narrow half-transparent compressed" data-type="symbol" data-device="kitchen_ATC" data-get="batterylevel" data-states='["100","([7][5-9])|([8-9][0-9])","([5-6][0-9])|([7][0-4])","([2][5-9])|([3-5][0-9])","([0-9])|([1][0-9])|([2][0-4])"]' data-icons='["fa-battery-4 fa-rotate-270","fa-battery-3 fa-rotate-270","fa-battery-2 fa-rotate-270","fa-battery-1 fa-rotate-270","fa-battery-0 fa-rotate-270"]' data-colors='["darkgray","darkgray","darkgray","darkgray","#ad3333"]'></div>&nbsp;
            <div data-type="label" data-device="kitchen_ATC" data-get="humidity" data-unit="%" data-limits='[30,40,61]' data-colors='["red","green","red"]' class="top-narrow thin"></div>
        </div>
        <div data-type="label" class="left-narrow small" style="margin-top: -3em !important;">Kitchen</div>
    </div>
</li>
⚠️ **GitHub.com Fallback** ⚠️