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

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.

That’s not the issue you were having, you had connection issues. :slight_smile:

If you remove the double quotes it would work anyway. If double quotes were needed, none of the values would have been retrieved.

Error fetching data: http://192.168.0.84:8080/list?token=xxxxxxxxxx failed with Server disconnected without sending a response.

HA is telling you here that the server (the bridge) called through that URL disconnected with no response.

Thank you Alex but my connection seem to be reliable. During the errors I tried to get the values manually giong with my browser to the “list” and “info” pages and the bridge always returned the values flawlessly (while the HA card was continuing to show “unknown”).

I will remove the quotes to check if the errors will reappear.

If it was, you wouldn’t see this in the logs:

“Error fetching data: http://192.168.0.84:8080/list?token=xxxxxxxxxx failed with Server disconnected without sending a response.”

Your connection to the bridge is not reliable. The code is ok, if double quotes were needed, I’d have the same problem and you would have all values to Unknown. :slight_smile:

Fidati. :slight_smile:

Actually the log doesn’t lie. Maybe some interferences which I noticed in some hours of the day since my neighbor installed an ezviz system.
Btw thanks for this useful code!

I just realised I have a similar error


TemplateError('UndefinedError: 'None' has no attribute 'attributes'') 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'
1:32:53 PM – (ERROR) Template - message first occurred at 1:32:53 PM and shows up 2 times
Template variable error: 'None' has no attribute 'attributes' when rendering '{{ (as_timestamp(states.sensor.nuki_lock_status.attributes.timestamp)) | timestamp_custom("%H:%M (%b %d)") }}'
1:32:53 PM – (ERROR) helpers/template.py - message first occurred at 1:32:53 PM and shows up 2 times

But everything works.

It does that on HA startup because variables used in the template are empty.

Ah okay. Thanks and sorry when you explained it before.

Not my code, I just modified/polished @HITMIK and @Joerg code. :slight_smile: