Nuki Card with Callback support (supports both Lock & Opener, it replaces the official integration)

Dear Alessandro,

thank you for your feedback. You are right - a reed sensor is a nice add on.

I just mentioned it because in your automation you are analyzing the POST-request-payload (callback/webhook) which comes in three different versions according to the official Nuki Bridge documentation (https://developer.nuki.io/page/nuki-bridge-http-api-1-12/4)
(normal, with sensor, with opener).
Your code works with sensor only because you read out doorsensorState (which is not available in the json map if you do not have the sensor).

{{ my_state[trigger.json.doorsensorState] }}

A “no sensor-version” of your automation would be fine.

And by the way: Forza squadra azzurra!

Walter

Yes, I always gave for granted that everybody had the doorsensor. I didn’t consider it like another device, like the keypad, the fob key, etc. :slight_smile:

A no door-sensor version would be focused only on the lock itself, but if you noticed, for me the door state is central, and it’s actually the main sensor.

I have to think about it…

Forza Azzurri!! :it:

Hi all,
Firstly, @alexdelprete thanks for your work on this.

I’ve just got my Nuki Smart Lock v2 and it’s all up and running. I’ve gone straight for this, rather than the official integration, and thought I’d done everything correctly, but alas I’ve messed something up along the way. :confused:

Some of the entities are working but others, including the state of the lock aren’t.

Logs are showing:

2021-07-07 19:12:26 WARNING (MainThread) [homeassistant.components.sensor] Platform rest not ready yet: Unsupported URL protocol ''; Retrying in background in 30 seconds
2021-07-07 19:12:26 WARNING (MainThread) [homeassistant.components.sensor] Platform rest not ready yet: Unsupported URL protocol ''; Retrying in background in 30 seconds
2021-07-07 19:12:52 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element Undefined when rendering '{% if states('sensor.nuki_endpoint_list') == "OK" %}
{% set my_state = {1: 'deactivated', 2: 'closed', 3: 'open', 4: 'unknown', 5: 'calibrating'} %}
{{ my_state[state_attr('sensor.nuki_endpoint_list', 'lastKnownState')['doorsensorState']] }}
{% endif %}'
2021-07-07 19:12:52 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'batteryCharging' when rendering '{% if states('sensor.nuki_endpoint_list') == "OK" %}
{% set battery_level = state_attr('sensor.nuki_endpoint_list', 'lastKnownState')['batteryChargeState'] | default(0) | int %}
{% if state_attr('sensor.nuki_endpoint_list', 'lastKnownState')['batteryCharging'] %}
{{ battery_level }}
{% else %}
{{ battery_level }}
{% endif %}
{% endif %}'

Any ideas on where I should be looking or what I’ve missed?

Thanks!

Ciao,

what version of the Nuki Card are you using? the one in the first post or the automation version 6.0-rc1?

I think the problem is the fw version of the lock and bridge (specifically, the bridge). These are my versions, yours are really old. Upgrade firmware of all devices and it will work. :slight_smile:

image

I’ve used everything from the first post, should I update to 6.0-rc1?

Weird, I can only find a way to force a FW update check on the lock and it’s claiming that it’s up to date. :confused: I’ll leave it overnight and see if it has changed in the morning, if not I’ll get on to Nuki support and see if they can push the latest versions for me.

Grazie

I said “specifically the bridge”. :slight_smile:

All the info that you are missing in the integration are available only from a specific version of the bridge firmware. These are the minimum versions required for Bridge API v1.12 that this integration is based upon:

image

You can update the bridge quickly this way:

1 Like

@Sym0nd0 after you upgrade the bridge firmware, try the packaged version, it’s much easier to install so to avoid any possible copy&paste error.

Obviously you have to clean the previous integration split on multiple files.

If it works for you, I think I’ll release it, replacing the version of the first post.

1 Like

Boom! The FW update call did the trick, now have v2.9.3 for bridge and v2.2.0 wifi. :raised_hands:t2:

Now I know that it’s working, I’ll roll back to before setting this up and give the packaged version a go in the morning. If all goes to plan, as I’m sure it will, I’ll let you know.

Thanks again for all your help and work.

Glad you got it working. It’s my fault, I didn’t specify the prerequisites in the first post, when I’ll release the packaged version I’ll write a specific paragraph so to make sure that the environment is ok before installing the card.

Did you test locking/unlocking, opening/closing the door and reaction on the lovelace card? Is it fast enough?

I’ll wait for your feedback before releasing it.

I’ve just reinstalled using the packaged method and it’s all gone through smoothly, working identically the same as the split method.

Have a few warnings in the log, but doesn’t appear to be causing issues:

2021-07-08 11:12:58 WARNING (MainThread) [homeassistant.components.sensor] Platform rest not ready yet: Unsupported URL protocol ''; Retrying in background in 30 seconds
2021-07-08 11:12:58 WARNING (MainThread) [homeassistant.components.sensor] Platform rest not ready yet: Unsupported URL protocol ''; Retrying in background in 30 seconds
2021-07-08 11:13:19 WARNING (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Already running

The lovelace card isn’t responding to locking/unlocking or the door state. The lock/unlock triggers and works as expected but the state of the entities on the cards don’t update.

Enabled debug logging (I’ve tried to pick out what’s relevant below), restarted my instance, unlocked via lovelace and let it auto-lock:

2021-07-08 11:22:35 INFO (MainThread) [homeassistant.components.binary_sensor] Setting up binary_sensor.template
2021-07-08 11:22:35 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.template
2021-07-08 11:22:35 INFO (MainThread) [homeassistant.setup] Setting up rest
2021-07-08 11:22:35 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.rest
2021-07-08 11:22:35 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.rest
2021-07-08 11:22:35 WARNING (MainThread) [homeassistant.components.sensor] Platform rest not ready yet: Unsupported URL protocol ''; Retrying in background in 30 seconds
2021-07-08 11:22:35 WARNING (MainThread) [homeassistant.components.sensor] Platform rest not ready yet: Unsupported URL protocol ''; Retrying in background in 30 seconds
2021-07-08 11:22:35 INFO (MainThread) [homeassistant.bootstrap] Setting up stage 2: {'ffmpeg', 'group', 'script', 'input_number', 'cast', 'input_select', 'zone', 'persistent_notification', 'blueprint', 'met', 'mqtt', 'counter', 'network', 'wled', 'tag', 'media_source', 'nest', 'sun', 'default_config', 'my', 'tts', 'mobile_app', 'logbook', 'ssdp', 'trace', 'dhcp', 'lock', 'updater', 'yeelight', 'hive', 'tasmota', 'rpi_power', 'google_home', 'binary_sensor', 'scene', 'timer', 'input_text', 'rest_command', 'zeroconf', 'map', 'input_boolean', 'input_datetime', 'hacs', 'system_health', 'automation', 'sensor', 'history', 'upnp'}
2021-07-08 11:22:54 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.rest
2021-07-08 11:22:54 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.rest
2021-07-08 11:22:54 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Initialized trigger Nuki Card Callback
2021-07-08 11:22:56 WARNING (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Already running
2021-07-08 11:22:56 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Running automation actions
2021-07-08 11:22:56 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Running automation actions
2021-07-08 11:22:56 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:22:57 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:22:57 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:22:57 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:24:06 INFO (MainThread) [homeassistant.helpers.script.nuki_lock_action] Nuki Lock Action: Running template script
2021-07-08 11:24:06 INFO (MainThread) [homeassistant.helpers.script.nuki_lock_action] Nuki Lock Action: Executing step call service
2021-07-08 11:25:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Running automation actions
2021-07-08 11:25:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Running automation actions
2021-07-08 11:25:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:25:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:25:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:25:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:30:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Running automation actions
2021-07-08 11:30:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Running automation actions
2021-07-08 11:30:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:30:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:30:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service
2021-07-08 11:30:26 INFO (MainThread) [homeassistant.components.automation.nuki_card_callback] Nuki Card Callback: Choose at step 1: choice 2: Executing step call service

After some delay, a couple of minutes maybe, the lock state updated both to show it unlocked and then locked on the lovelace card.

Let me know if you need anything more.

I think you have leftovers from the split configuration to the packaged one.

The package has been tested by several people with no particular issues, apart some warnings in log due to REST sensors being started after some others etc, but nothing special.

When you restart HA, you should see IMMEDIATELY the status of the lock and door sensor, and all those info sensors updated. If it takes time (minutes??), you can’t lock/unlock from lovelace, or else, it means you are still having install issues. Take into account that the REST calls to update info are being made at startup and then every 150 secs.

Clean everything and just use the packaged version, it should work straight-away pretty easily, but make sure you start from a clean known state. The REST sensors are important at startup time, make sure you don’t have the split ones when you transition to the packaged version. The fact that you mention it takes minutes to see some status updates leads me to think that the REST polling is not working.

Hey there,

I followed the instructions on post 1 and can see all the entities but most of them do not show any values and I cannot lock/unlock the door using the button.

Btw: I renamed the door sensor and lock action but this is not the reason for my problems. It was the same before renaming.

Anything I can do to figure out the reason for this?
Thanks!

I suspect you have an old firmware on the bridge. The Nuki Card uses Nuki Bridge API v1.12 and requires at least v2.5.0 of the firmware.

Check this post of a couple of days ago.

A request to /info says the firmware is up to date:
“firmwareVersion”: “2.9.3”

I did not use the card code but added the entities manually to lovelace. Won’t this work?

If the fw is ok, it means all the REST calls to the bridge are not working. Check HA logs for errors and report them here.

The lock is actioned through this REST command:

#######################################################################################################################
###                                                                                                                 ###
### rest commands                                                                                                   ###
###                                                                                                                 ###
#######################################################################################################################

rest_command:
  nuki_lock_action:
    url: "http://{{ states('input_text.nuki_bridge_host') }}:{{ states('input_text.nuki_bridge_port') }}/lockAction?nukiId={{ states('sensor.nuki_id') }}&token={{ states('input_text.nuki_bridge_token') }}&action={{ action }}"

The info sensors are fed by these two REST sensors:

#######################################################################################################################
###                                                                                                                 ###
### sensors                                                                                                         ###
###                                                                                                                 ###
#######################################################################################################################

sensor:
  - platform: rest
    scan_interval: 150
    resource_template: "http://{{ states('input_text.nuki_bridge_host') }}:{{ states('input_text.nuki_bridge_port') }}/list?&token={{ states('input_text.nuki_bridge_token') }}"
    name: "Nuki Endpoint List"
    value_template: "OK"
    json_attributes:
      - lastKnownState
      - firmwareVersion
      - nukiId
      - name

  - platform: rest
    scan_interval: 150
    resource_template: "http://{{ states('input_text.nuki_bridge_host') }}:{{ states('input_text.nuki_bridge_port') }}/info?&token={{ states('input_text.nuki_bridge_token') }}"
    name: "Nuki Endpoint Info"
    value_template: "OK"
    json_attributes:
      - versions
      - scanResults
      - wlanConnected
      - serverConnected

So the REST calls in your case are not working. Check the logs.

Ah, thanks for reminding me. I checked the logs and there was actually a rest error logged.
Obviously it is because the token is not url-encoded. I have special chars in my token which makes the rest call fail. Is there a way to escape url special chars like ‘&’ in the token?
Do I have to escape them manually in the secrets.yaml or can this be done programmatically?

& is %26. Try to use https://www.urlencoder.net/

I didn’t think of this potential issue, thanks for reporting it, I will try to handle it in the code.

Yes, I know. The problem ist that the parameters are sent via the url as get parameters. If there was a way to send them via post, this should fix the problem, shouldn’t it?

We could use

method: POST
payload: '{"token" : "{{ states('sensor.nuki_bridge_token') }}"}'

or similar, right?

You forget that we use an API of the bridge, and unfortunately it uses URL-parameters:

image
image

Yes, you are right. POST seems to be allowed for the callback url only…
OK, so we neet to url-encode the token within yaml.