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

Yes sure. (ok, in order to answer I have to write 10 characters)

I think the “door sensor jammed” problem could come from concurrency between Nuki app and HA “callback solution” (see my post above). If @alexdelprete does not use it that may explain why he does not have the problem. Moreover, a concurrency problem (related to the Nuki app 20" period to refresh door sensor) could explain the difficulty to find a reproducible sequence driving to the problem.

The problem is the trigger. Your templates won’t be evaluated until there is a trigger. In what your propose, you still have only the callback event, so the values will still only be calculated only when a callback arrives.
You could try the solution proposed here: Enhance Trigger-based Template Sensors
Or add a state trigger based on your sensor.nuki_door_state entity (if this is the correct entity). This latter solution will also make this trigger template updates every time your polled sensor updates
Then for your ifs, you should test on the trigger.platform / event_type, I think.

I use it all the time. The bridge should serialize the calls and queue them, I don’t think that is the problem. If they’ve done a good job on the firmware, the bridge should also have a cache for very near requests. It’s strange that I don’t see any sensor warning in my app.

What fw are you on? Bridge and Lock firmware…here are my versions:

image

Yes. I use it for notifications, etc.

So I can have more trigger types for the same sensor? that’s interesting, because if I can filter the trigger I can then use a value in case of the callback and another in case of the polling…

From the docs I didn’t notice you could have more triggers…I’ll have to read it again.

Thanks for the suggestions…if I can’t solve the puzzle I’ll write you, if you don’t mind…I’m still a beginner with HA stuff.

No problem (I’m not an expert either!)

Thank you.

Found this…good start…I have to find some concrete examples to understand better.

Maybe this is what you want:

- trigger:
    - platform: webhook
      webhook_id: !secret nuki_bridge_webhook
    - platform: event
      event_type: event_template_reloaded
    - platform: homeassistant
      event: start
  binary_sensor:
    - name: "Nuki Door State"
      unique_id: nuki_door_state
      device_class: door
      state: >
        {% if trigger.platform == 'webhook' %}
          {{ trigger.json.doorsensorState == 3 }}
        {% else %}
          {{ states('sensor.nuki_door_state') }}
        {% endif %}
  • If it is triggered by the Webhook Trigger, it evaluates the trigger variable.
  • If it is triggered by either the Event Trigger (Template Entities Reloaded) or Home Assistant Trigger (Start), it gets the sensor’s state.

Taras, you saved me hours of researching and experimenting and most of all 350 HA restarts…:smiley:

As soon as I have some time I’ll work on it and let you know if it works.

I was looking since 2 hours for these:

    - platform: homeassistant
      event: start
{% if trigger.platform == 'webhook' %}
    - platform: homeassistant
      event: start

@Friedrieck pointed me this morning to your FR and the event_template_reloaded.

Thank you, I hope I can contact you in case of problems. :slight_smile:

And to save even more time: you do not need to restart HA each time. Go in the config page, server control, you can reload there the templates entities with a single click!

1 Like

Are you joking? Why didn’t you tell me before? last night I didn’t sleep mostly waiting for HA to restart at every try. :rofl:

That’s the price you pay for inexperience I guess…

That’s why the example I posted includes the following trigger:

    - platform: event
      event_type: event_template_reloaded

It will trigger whenever you execute Configuration > Server Controls > Reload Template Entities.

1 Like

You two saved me hours of unpaid work during the night. I owe you a good italian coffee or a good glass of our wine. :slight_smile:

2 Likes

What fw are you on? Bridge and Lock firmware…

Screenshot_20210528_164248

The Nuki app says my smart lock firmware is up to date. No 2.11 proposed. Are you part of a beta program ?

I tried also 192.168.0.xxx:8080/fwupdate?token=XXXXXXXXX without any change.

I was in beta a long time ago…don’t know if that’s a beta versione of the fw. Check on their forum what’s the latest official version. I suspect they fixed some of the issues you are seeing in that version, but I’m not sure.

Going to get some rest now, tomorrow I’ll release last version of the card, it was a long day and I don’t have the energy to do it now.

Talk to you tomorrow.

I worked a few months in Napoli and Roma so I know the coffee is very good there but try to sleep this night :wink:

I will…trust me…:slight_smile:

We’ll talk about your experience here…good night. :slight_smile:

Hi @123 and @Friedrieck,

today I worked on the implementation of the multiple triggers for the sensor: unfortunately it doesn’t work like it’s supposed to. The only trigger that enables the sensor is the webhook, none of the other two (event/event_template_reloaded and homeassistant/start) sets the sensor as available.

I rewrote the sensor so to distinguish among the trigger platforms and have so one sensor that gets data via callback or polling, but if the other triggers don’t work it’s all useless.

Here’s what I see when I restart HA or do a template reload: you will notice 4 sensors, the first one is a template lock linked to the main sensor (Nuki Door State) that is the triggered sensor. Both of them are unavailable after an HA restart or template reload. The other two sensors are the ones that I get through the polling system, that gets launched immediately after the restart obviously, and are the ones whose values I wanted to use to feed the Nuki Door State when it hasn’t yet received a callback so it is not initialized.

image

As soon as I open the door, obviously the callback arrives, so the webhook gets triggered:

image

I also added an attribute to the sensor to show the trigger platform, and it’s always the webhook, obviously:

Here’s the code of Nuki Door State:

- trigger:
    - platform: event
      event_type: event_template_reloaded
    - platform: homeassistant
      event: start
    - platform: webhook
      webhook_id: !secret nuki_bridge_webhook
  binary_sensor:
    - name: "Nuki Door State"
      unique_id: nuki_door_state
      device_class: door
      state: >
        {% if trigger.platform == 'webhook' %}
          {{ trigger.json.doorsensorState == 3 }}
        {% else %}
          {{ is_state('sensor.nuki_door_sensor_state', 'open') }}
        {% endif %}
      icon: >
        {% if trigger.platform == 'webhook' %}
          {% set my_state = {1: 'deactivated', 2: 'closed', 3: 'open', 4: 'unknown', 5: 'calibrating'} %}
          {% set trigdoor = my_state[trigger.json.doorsensorState] %}
          {% set my_state = {0: 'uncalibrated', 1: 'locked', 2:'unlocking', 3: 'unlocked', 4: 'locking', 5: 'unlatched', 6: "unlocked (lock ‘n’ go)", 7: 'unlatching', 254: 'motor blocked', 255: 'undefined'} %}
          {% set triglock = my_state[trigger.json.state] %}
        {% else %}
          {% set trigdoor = sensor.nuki_door_sensor_state %}
          {% set triglock = sensor.nuki_lock_sensor_state %}
        {% endif %}
        {% if (trigdoor == 'open') %}
          mdi:door-open
        {% elif trigdoor == 'closed' and triglock == 'locked' %}
          mdi:door-closed-lock
        {% elif trigdoor == 'closed' and triglock == 'unlocked' %}
          mdi:door-closed
        {% else %}
          mdi:alert-box-outline
        {% endif %}
      availability: >
        {% if trigger.platform == 'webhook' %}
          {{ trigger.json.doorsensorState != None }}
        {% else %}
          {{ (states('sensor.nuki_door_sensor_state') >= 1) and (states('sensor.nuki_door_sensor_state') <= 5) }}
        {% endif %}
      attributes:
        nuki_id: >
          {% if trigger.platform == 'webhook' %}
            {{ trigger.json.nukiId }}
          {% else %}
            {{ sensor.nuki_id }}
          {% endif %}
        door_state: >
          {% set my_state = {1: 'deactivated', 2: 'closed', 3: 'open', 4: 'unknown', 5: 'calibrating'} %}
          {% if trigger.platform == 'webhook' %}
            {{ my_state[trigger.json.doorsensorState] }}
          {% else %}
            {{ sensor.nuki_door_sensor_state }}
          {% endif %}
        lock_state: >
          {% set my_state = {0: 'uncalibrated', 1: 'locked', 2:'unlocking', 3: 'unlocked', 4: 'locking', 5: 'unlatched', 6: "unlocked (lock ‘n’ go)", 7: 'unlatching', 254: 'motor blocked', 255: 'undefined'} %}          
          {% if trigger.platform == 'webhook' %}
            {{ my_state[trigger.json.state] }}
          {% else %}
            {{ sensor.nuki_lock_sensor_state }}
          {% endif %}
        lock_battery: >
          {% if trigger.platform == 'webhook' %}
            {{ trigger.json.batteryChargeState }}%
          {% else %}
            {{ sensor.nuki_id }}
          {% endif %}
        lock_battery_critical: >
          {% if trigger.platform == 'webhook' %}
            {{ is_state('trigger.json.batteryCritical', 'True') }}
          {% else %}
            {{ sensor.nuki_lock_battery_critical }}
          {% endif %}
        keypad_battery_critical: >
          {% if trigger.platform == 'webhook' %}
            {{ is_state('trigger.json.keypadbatteryCritical', 'True') }}
          {% else %}
            {{ sensor.nuki_keypad_battery_critical }}
          {% endif %}
        last_update: "{{ strptime(as_timestamp(now()) | timestamp_local, '%Y%m%d %H:%M:%S') }}"
        trigger: '{{ trigger.platform }}'

In the documentation, I read this:

And in the Automation Trigger docs I read this:

So, at least in theory, it should work, but it doesn’t. I hope it’s a mistake on my side, open to any suggestion to fix this, so I can finally release the new code.

Thanks a lot,

Alessandro