Home Assistant MQTT auto. discovery

Hi Everyone,

I have created a python script for Home Assistant which uses the MQTT auto. discovery feature to create a device with all the necessary entities in Home Assistant. The repository can be found here: GitHub - MattDog06/Nuki-MQTT-auto.-Discovery: Python Script creating HA auto. discovery topics for you Nuki Lock 3 Pro with MQTT enabled (Beta Firmware)

I hope it helps some of you.

3 Likes

Hi,

I tried to install your script. Python script is installed in HA but after run I see in HA logs these errors:
Could you help me, please?

Logger: homeassistant.components.python_script.nuki_mqtt_discovery.py
Source: components/python_script/init.py:224
Integration: Python Scripts (documentation, issues)
First occurred: 12:51:58 (1 occurrences)
Last logged: 12:51:58

Error executing script: can only concatenate str (not “NoneType”) to str

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/python_script/init.py”, line 224, in execute exec(compiled.code, restricted_globals)
File “nuki_mqtt_discovery.py”, line 303, in
File “nuki_mqtt_discovery.py”, line 229, in main
File “nuki_mqtt_discovery.py”, line 57, in get_error_message
TypeError: can only concatenate str (not “NoneType”) to str

Hi Petr,

This has to do with the device id being an integer instead of a string,

if you look at line 221 in the script it says “device_id = data.get(DEVICE_ID)” change it to “device_id = str(data.get(DEVICE_ID))”
This would solve you’re problem.

if matthias could change this would be great.

Hi,

fixed this issue in the repo.

Hi Matthias,

I checked and downloaded your new version and there is still
“device_id = data.get(DEVICE_ID)” instead of “device_id = str(data.get(DEVICE_ID))” on line 221 and I´ve got the same error with 1.0.1 script.

If I changed the line 221 manually, I´ve got different error:

Logger: homeassistant.components.python_script.nuki_mqtt_discovery.py
Source: components/python_script/init.py:224
Integration: Python Scripts
First occurred: 21:01:04 (1 occurrences)
Last logged: 21:01:04

Parameter None is required!

+1 same here
I can see the messages of the lock coming in when I’m listening manually.
But when I run this script as service, I see the same errors in the logs.

EDIT:
I managed to get it working.
The first exception comes from get_error_message, where two strings are concatenated with variable “parameter”. Since “parameter” is None when the required data doesn’t contain a device_id, this exception is thrown.
Then when the proposed change is applied, get_error_message outputs “Parameter None is required”, because in lines 233, 237 and 241 call get_error_message(device_id) (and device_id doesn’t exist).

I changed the code from line 232-242 to the following, to get better error messages (quotes + correct names):
if device_id == None or device_id == “”:
logger.error(get_error_message(“device_id”))
return

if device_name == None or device_name == “”:
logger.error(get_error_message(“device_name”))
return

if device_model == None or device_model == “”:
logger.error(get_error_message(“device_model”))
return

And then I realised that I was calling the service without appending the required data, as described on the Github page.
To get it working, execute the following in Developer tools:

  • Service: Python Scripts: nuki_mqtt_discovery
  • Go to YAML mode and make sure you add the required fields:
    service: python_script.nuki_mqtt_discovery
    data:
    device_id: [ID OF YOUR LOCK IN HEX]
    device_name: [NAME OF YOUR LOCK]
    device_model: Smart Lock 3.0 Pro

Run the service, and the device will appear under your MQTT devices.

Hi,

I changed also lines 222 and 223 to:
device_name = str(data.get(DEVICE_NAME))
device_model = str(data.get(DEVICE_MODEL))

so now I´am able to add device to MQTT devices without using data: in service: python_script.nuki_mqtt_discovery command BUT the script does not forward my device ID to the MQTT, so in MQTT topics I see string “None” instead of my Device ID. Maybe is is due my Device ID a string with letters, not only integers. The result is that data is not presented from Nuki device.

Could someone help me with this probably last issue?

Edit: changing these lines:
device_id = str(data.get(DEVICE_ID))
device_name = str(data.get(DEVICE_NAME))
device_model = str(data.get(DEVICE_MODEL))

to:
device_id = DEVICE_ID
device_name = DEVICE_NAME
device_model = DEVICE_MODEL

and use python script without the data: helped me. But there are missing some entities which are published by the lock like as Firmware, connection status etc. It will be good if developer of this script apply all available values from Nuki to HA.

@Petr_Maizner
I do not use Home Assistant so i cannot check by myself, but the changes look like you are using the script wrong or in a wrong environment.

@Petr_Maizner your are using the script wrong. Please follow the instruction on the github page. I also released version 1.0.2, which fixes the error messages.

1 Like

I hope Nuki devs will implement HA discovery integration in the firmware, without requiring external scripts/apps.

1 Like

I also hop.

We should open a separate topic to say what we expect on this.
For example another setting for the final stage because the MQTT topic for auto-discovery can be changed IIRC, by default it is home-assistant be can be set differently.

Yes, please. I was already asking for it, but without luck so far:

1 Like

Hi !
I have managed to get it to work with HA, and got all of the sensors and functionality, cool!

However, in the 2 weeks I had it working, my battery level dropped from 100% to 20%! normally this takes me 6-10 weeks.
I’ve noticed suspicious activity on the LED indicated quite frequently - as frequently as once a minutes. Could this configuration somehow cause the lock to abandon normal power saving methods and run through the battery on always-on WIFI connectivity?
Hilik

Anyone else experiencing this? In the full week since removing the MQTT entity created by the autodiscovery script my battery merely went from 100% to 90% - perfectly fine…
Would really love to restore the batery indicator functionality though…

Read the MQTT spec. Page 4 says:

When the debug mode of the Smart Lock and “LED signal on the Smart Lock” are
both active, the red LED of the Smart Lock acts as a traffic indicator for incoming
WIFI packets, similar to a traffic indicator of a network switch. Turn off “LED signal on
the Smart Lock” to disable this diagnostic feature.

And yes, this can drain your Smart Locks battery very quickly.

What drains the battery - the LED indication or the actual debug mode?
BTW - my LED flashes mostly white, just very rarely red.

1 Like

Yes, the LED. Debug itself has no impact on battery life.

1 Like

Thanks a lot for your work.
Because I sometimes prefer complicated things :laughing: I have created a YAML configuration for my Smartlock and your script helped me to confirm I understood the documentation correctly.

To be honest, it looks like more state are supported by MQTT auto-discovery I.E. locking, unlocking and opening state are not available according to the Home Assistant documentation so I will miss some information so looks like using auto-discovery makes the integration more complete.

EDIT: after reading homeassistant/components/mqtt/lock.py, it looks like these states are not managed at all so YAML configuration and auto-discovery both provide exactly the same features.

Hi Patrick,

I’m still a little bit stuck in integrating the lock to HA because I cannot use the script in HACS.

May you be so kind and share your yaml config?

Thanks a lot!

Dominik

To be honest, I’ve succesfully checked it by comparing with the script and syntactically but I’m still procrastinating the migration from the actual working integration using my bridge to the new with WI-Fi :wink:

I plan to do it tomorrow (I know, it looks like another procrastination :laughing:) and as soon as I will be sure it is OK I will share it for sure.

If you really need it before I test it I can share it with you but I prefer waiting to be sure it works before sharing anything :slight_smile: