Nuki Smart Lock 2.0 - support all available API actions (i.e. add lock/unlock)

Don’t worry, all good.
Fingerscrossed the update will work.

@Joerg again thanks for helping , it was the old firmware indeed , everything is working now , even scripts , thanks again !!!

1 Like

Good to hear and you’re welcome.

Thanks for this, it’s working fine. Status updates are not immediate, because of polling I guess, but it works.

@steinerl is working on major upgrade to the integration: Use callbacks instead of polling in nuki by steinerl · Pull Request #44310 · home-assistant/core (github.com)

it will include the door status (made by @pree) and callbacks vs polling. Nice stuff. Can’t wait to try it.

Hello to all

Anyone knows what is needed to add a second nuki with this script , because it uses the deviceType": 0, on both locks , and would like to add a second one … and have the battery and sensor status , the comands with script work fine … just need to see how with script i can reach the second lock in the bridge !!! ???

The Nuki Card was moved here: Nuki Card with callbacks (official integration not needed)

5 Likes

Hello Alex,
thanks for sharing your code with the icons, but its not my code - the original code was created and shared by @HITMIK there
I just “translated” it to english and tried to help some others in here.

sorry about not mentioning @HITMIK, credit to both of you then, I amended the post.

1 Like

I don’t have more than one lock, but I guess you need to setup another three shellcommands and use the ID of the second lock in IDNUKISMARTLOCK like I told there →

Configuration.yaml

nuki_lock:
  alias: nuki-lock
  sequence:
  - service: shell_command.nuki_lock
  mode: single
nuki1_lock:
  alias: nuki1-lock
  sequence:
  - service: shell_command.nuki1_lock
  mode: single

shell_commands.yaml

nuki_lock: "curl -s 'IP:PORT/lockAction?nukiId=ID_1st_lock&deviceType=0&action=2&token=token'"
nuki1_lock: "curl -s 'IP:PORT/lockAction?nukiId=ID_2nd_lock&deviceType=0&action=2&token=token'"

Hi, thanks for tweaking :slight_smile:

Callback use HTTP and HA need used HTTPS (webhook) = hard work :slight_smile: (i thinking)
Maybe work with NodeRed.

Good job @alexdelprete

From what I read here, HTTPS is mandatory only if you have HA working in SSL/TLS only.

I’m trying @alexdelprete sensors package but I get few errors:

The BT Status and BT RSSI sensors report Unknown/Sconosciuto very often as the following screenshot
nuki_sensors

And the following errors appear in the HA log:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:72
Integration: Template (documentation, issues)
First occurred: 18 maggio 2021, 15:09:44 (4 occurrences)
Last logged: 5:21:37

TemplateError('UndefinedError: 'mappingproxy object' has no attribute 'timestamp'') while processing template 'Template("{{ (as_timestamp(states.sensor.nuki_lock_status.attributes.timestamp)) | timestamp_custom("%H:%M (%b %d)") }}")' for attribute '_state' in entity 'sensor.nuki_last_activity'
Logger: homeassistant.helpers.template
Source: helpers/template.py:1378
First occurred: 18 maggio 2021, 15:09:44 (16 occurrences)
Last logged: 5:22:37

Template variable error: 'mappingproxy object' has no attribute 'timestamp' when rendering '{{ (as_timestamp(states.sensor.nuki_lock_status.attributes.timestamp)) | timestamp_custom("%H:%M (%b %d)") }}'
Logger: homeassistant.components.rest.data
Source: components/rest/data.py:69
Integration: RESTful (documentation, issues)
First occurred: 18 maggio 2021, 15:09:44 (491 occurrences)
Last logged: 7:59:35

Error fetching data: http://192.168.0.84:8080/list?token=xxxxxxxxxx failed with
Error fetching data: http://192.168.0.84:8080/info?token=xxxxxxxxxx failed with Server disconnected without sending a response.
Error fetching data: http://192.168.0.84:8080/list?token=xxxxxxxxxx failed with Server disconnected without sending a response.
Logger: homeassistant.components.rest.sensor
Source: components/rest/sensor.py:181
Integration: RESTful (documentation, issues)
First occurred: 18 maggio 2021, 15:09:44 (491 occurrences)
Last logged: 7:59:35

Empty reply found when expecting JSON data

BTW I tied to fetch data manually (multiple times) from http://192.168.0.84:8080/list?token=xxxxxxxxxx or http://192.168.0.84:8080/info?token=xxxxxxxxxx and everytime the bridge reports data flawlessly.

If I restart HA, BT values appear again.

Hi,
seems something is wrong with the definition of timestamp.
You might show us the code you use.

The code is the one posted by @alexdelprete in this post and reported in the log error:

value_template: >-
          {{ (as_timestamp(states.sensor.nuki_lock_status.attributes.timestamp)) | timestamp_custom("%H:%M (%b %d)") }}

But I think that is not related to BT Status and rssi values.

I asked you to do so, to see if the indentations are okay or if there are typing errors.
You change as least Unknow to Sconosciuto :wink:

I did not change anything. Just copied and pasted Alex’s code.
The sensor.nuki_lock_bt_rssi is a signal_strenght device_class sensor. It expects a db value and reports Unknown in the HA system language if that value is not found (as does the Nuki official integration binary sensor). My HA system language is italian, Unknown = Sconosciuto :wink:

Okay, if you don’t want to show :wink:
The only problem I can imagine aside wrong indentations are old firmware (see this thread above) but since your battery level is shown, this might not the case.

So sometimes you do see the values correctly right? It’s just a matter of connection to the bridge, even if you tried manually, it happens that sometimes the bridge doesn’t answer, it happened to me when I was polling it too frequently. That’s why I changed the scan_interval to 120, I found that polling too frequently led to unreliable results.

How many times to you see those 2 values to unknown? You can see those two values throught info url (in your case: http://192.168.0.84:8080/info?token=xxxxxxxxxx).

Try calling the url from shell repeatedly and see if it always answers correctly.

When you restart HA, if values are ok, it means the code is fine, it’s just that when it refreshes the sensors calling the url again, those 2 values are not ok, so Unknown comes out.

The errors you showed indicate you are having connection issues, probably wifi disconnections or routing issues.

Error fetching data: http://192.168.0.84:8080/list?token=xxxxxxxxxx failed with
Error fetching data: http://192.168.0.84:8080/info?token=xxxxxxxxxx failed with Server disconnected without sending a response.
Error fetching data: http://192.168.0.84:8080/list?token=xxxxxxxxxx failed with Server disconnected without sending a response.
Empty reply found when expecting JSON data

Nothing to do with the code, if the connection is good, the JSON data is returned from the bridge, and the code retrieves and shows the proper values.

Let me know your findings…

I think I figured out. I had to put double quotes in json_attributes in “patform: rest” sections. After two hours no more log errors or “unknown” values.

  - platform: rest
    scan_interval: 120   # polling every 120 seconds
    resource: !secret nuki_bridge_list_url
    name: "Nuki Lock Status"
    value_template: "{{ value_json[0]['lastKnownState']['stateName'] }}"
    json_attributes_path: "$[0].lastKnownState"
    json_attributes:
      - "state"
      - "timestamp"
      - "doorsensorState"
      - "batteryCharging"
      - "batteryChargeState"
  
  - platform: rest
    scan_interval: 120  # polling every 120 seconds
    resource: !secret nuki_bridge_info_url
    name: "Nuki Connection"
    value_template: "{{ value_json['scanResults'][0]['paired'] }}"
    json_attributes_path: "$.scanResults[0]"
    json_attributes:
      - "rssi"
      - "paired"

If the errors will appear again I will edit this post.

He says that when restarting HA the 2 values are ok. If you see the errors at the end of his post, it clearly shows he’s having connection problems when calling the bridge URLs. Either the wifi is not reliable or something else.