mjmccans / airthings-mqtt-ha

Python script for Airthings devices that uses mqtt to integrate with Home Assistant via mqtt discovery.
MIT License
41 stars 3 forks source link

Multiple Devices - 5 #7

Closed dwlyles closed 2 years ago

dwlyles commented 2 years ago

I really appreciate your script here and trying to use it to its fullest ability. So thank you 1st and foremost.

Currently able to send 4/5 devices to HA Broker. airthings-mqtt-ha.py seem to see and pulls data for all 4 devices.

[2022-06-07 15:03:35] INFO: Setting up Airthings sensors...

[2022-06-07 15:04:05] INFO: d8:71:4d:aa:0a:f0: Manufacturer: Airthings AS Model: 2920 Serial: 045589 Device:Airthings Wave Mini

[2022-06-07 15:04:05] INFO: d8:71:4d:a9:a3:e1: Manufacturer: Airthings AS Model: 2920 Serial: 047816 Device:Airthings Wave Mini

[2022-06-07 15:04:05] INFO: d8:71:4d:a9:ac:5b: Manufacturer: Airthings AS Model: 2920 Serial: 047318 Device:Airthings Wave Mini

[2022-06-07 15:04:05] INFO: d8:71:4d:af:a7:4d: Manufacturer: Airthings AS Model: 2920 Serial: 046544 Device:Airthings Wave Mini

[2022-06-07 15:04:15] INFO: Done Airthings setup.

[2022-06-07 15:04:33] INFO: Sending HA mqtt discovery configuration messages...

[2022-06-07 15:04:33] ERROR: Failed while creating HA mqtt discovery messages.

Traceback (most recent call last):

  File "/home/pi/airthings-mqtt-ha/src/./airthings-mqtt-ha.py", line 341, in <module>

    config["name"] = s["name"]+" "+SENSORS[name]["name"]

KeyError: 'name'

[2022-06-07 15:04:33] ERROR: Failed while creating HA mqtt discovery messages.

Traceback (most recent call last):

  File "/home/pi/airthings-mqtt-ha/src/./airthings-mqtt-ha.py", line 341, in <module>

    config["name"] = s["name"]+" "+SENSORS[name]["name"]

KeyError: 'name'

[2022-06-07 15:04:33] ERROR: Failed while creating HA mqtt discovery messages.

Traceback (most recent call last):

  File "/home/pi/airthings-mqtt-ha/src/./airthings-mqtt-ha.py", line 341, in <module>

    config["name"] = s["name"]+" "+SENSORS[name]["name"]

KeyError: 'name'

[2022-06-07 15:04:33] INFO: Sending messages to mqtt broker...

[2022-06-07 15:04:33] INFO: Done sending messages to mqtt broker.

[2022-06-07 15:04:33] INFO: Done sending HA mqtt discovery configuration messages.

After this error, The scripts reports all the values for the temp, hum, and voc. but it just stops there, the last device in my config will not show up in HA. If this an HA issue or can i tweak the script? I have very little python experience, but i tend to have good success though google resarch. Although, im stuck here lol

[2022-06-07 15:18:46] INFO: Waiting 150 seconds.
[2022-06-07 15:21:16] INFO: Collecting sensor value messages...
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:aa:0a:f0/temperature = 27.1
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:aa:0a:f0/humidity = 38
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:aa:0a:f0/voc = 88
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:a9:a3:e1/temperature = 29.1
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:a9:a3:e1/humidity = 37
[2022-06-07 15:21:16] INFO: airthings/dd8:71:4d:a9:a3:e1/voc = 157
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:a9:ac:5b/temperature = 38.7
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:a9:ac:5b/humidity = 19
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:a9:ac:5b/voc = 339
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:af:a7:4d/temperature = 27.6
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:af:a7:4d/humidity = 39
[2022-06-07 15:21:16] INFO: airthings/d8:71:4d:af:a7:4d/voc = 957
[2022-06-07 15:21:16] INFO: Sending messages to mqtt broker...
[2022-06-07 15:21:16] INFO: Done sending messages to mqtt broker.
mjmccans commented 2 years ago

Looks like I did not properly check for the existence of values somewhere, but it is not immediately apparent where I went wrong. Can you please add the following print statements after the line 340 at the start of the if block (i.e., indented right under "if name in SENSORS:"):

print("s: "+str(s))
print("name: "+name)
print("sensors: "+str(SENSORS[name]))

I believe the output from that will help me figure out where I went wrong. Thank you.

mjmccans commented 2 years ago

It would also be helpful if you could share the "devices" section of your options.json file (please make sure not to include any passwords). Thank you.

dwlyles commented 2 years ago

Welp it looks like the bluetooth on my rpi-3 wont place nice tonight. Just trying to run the airthings find_wave command starts to scan and then for some reason fails. I'm going to keep trying though.

Traceback (most recent call last):
  File "/home/pi/airthings-mqtt-ha/src/find_wave.py", line 43, in <module>
    devices = scanner.scan(2.0)
  File "/home/pi/.local/lib/python3.9/site-packages/bluepy/btle.py", line 853, in scan
    self.process(timeout)
  File "/home/pi/.local/lib/python3.9/site-packages/bluepy/btle.py", line 829, in process
    self._mgmtCmd(self._cmd())
  File "/home/pi/.local/lib/python3.9/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'scan'
dwlyles commented 2 years ago

Here's the config you asked for as well.

{
    "devices": [
       {
        "mac": "d8:71:4d:af:0a:e2",
        "name": "Master Bedroom"
    }, {
        "mac": "d8:71:4d:af:0a:f0",
        "name": "Boy's Room"
    }, {
        "mac": "d8:71:4d:a9:d3:e2",
        "name": "Kitchen"
    }, {
        "mac": "d8:71:4d:a9:cc:2b",
        "name": "Studio"
        },{
                "mac": "d8:71:4d:af:07:2d",
                "Name": "Girl's Room"
        }],
    "refresh_interval": 150,
    "retry_count": 10,
    "retry_wait": 3,
    "log_level": "INFO",
    "mqtt_discovery": false,
    "mqtt_retain": false,
    "mqtt_host": "",
    "mqtt_username": "",
    "mqtt_password": ""
}
mjmccans commented 2 years ago

I think I see the problem. In your configuration file you last device uses "Name" with a capital N instead of "name" with a lower case N. I think that is why you are getting the key error. Please give that change a try and see if it fixes the issue. Perhaps I should add some checks for the config file format to give more helpful errors in the future.

dwlyles commented 2 years ago

I've added the

print ("s: "+str(s))
print ("name: "+name)
print ("sensors: "+str(SENSORS[name]))

but i dont see any additional output.

Also, updating the config "Name" to "name" also didnt change the results of the scan.

mjmccans commented 2 years ago

Those changes will not help with your scanning issue, which appears to be something else. I know others have had issues with the integrated bluetooth on the rasberrypi (particularly the rpi4) but I have not had any issues when I occasionally use an rpi3 that I have doing other things. If you have not already, consider rebooting the pi to see if that helps with the scanning issue.

dwlyles commented 2 years ago

After installing the some bluetooth visual software. Making sure bluetooth stays "enabled" reboot and presto! All 5 devices look to be pulling values and reporting to HA. Thank you again!

mjmccans commented 2 years ago

Good to hear; glad it is working for you (and good to know that it will work with 5 devices).