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

@Friedrieck tested with the fixes, same thing, it’s unavailable after template reload. But I noticed this now in the logs:

And also this now (I reloaded templates):

Logger: homeassistant.helpers.template
Source: helpers/template.py:1377
First occurred: 1:08:45 (2 occurrences)
Last logged: 1:10:07

Template variable error: 'sensor' is undefined when rendering '{% 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 %}'
1 Like

Hi Alex,
works here as well, I had the Nuki integration removed already the other day.
Great job, thank you for this.

IMO there’s a glitch in the documentation:
Now in rest_commands.yaml file, paste this code, it is required for the lock/unlock action of the previously created lock sensor.
The file should be named rest_command.yaml.

1 Like

I have in config.yaml
rest_command: !include rest_commands.yaml

You can choice any name of file, I think.

1 Like

:roll_eyes:
Sure. I shouldn’t do something like this before having at least the third coffee.
I used
rest_commands: !include rest_commands.yaml
and got an error that this integration doesn’t exist. :rofl:

I just copy&paste the updated templates.yaml and it is not ok (all sensors are unavailable). May be you changed also another file ?

Anyway, to be sure, I copy&paste all v4.2 files and now it is ok. So v4.2 is ok but may be not the description of the diff with v4.1 ?

PS. I finally removed Nuki integration.

I suggest a very small cosmetic modification:

name: "Nuki Friendly Name"
icon: mdi:alpha

instead of mdi:numeric

Is the availability template into locks.yaml file correct ?
(I am not very good in this yaml syntax)

So, the logs are instructive, and tell where to look at: there is a problem with the icon part.
And indeed, I think this may cause troubles:

[...]
        {% 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' %}
[...]

The two first lines should probably be corrected like this:

          {% set trigdoor = states(sensor.nuki_door_sensor_state) %}
          {% set triglock = states(sensor.nuki_lock_sensor_state) %}

And the same goes for your corrections in your post here: Nuki Card with callbacks (official integration not needed) - #6 by alexdelprete

1 Like

I’m at the first coffee, so I better not answer anything now…:smiley:

I removed all unique_id from templates.yaml. So in order to start clean, in case you have orphan entities, you need to remove everything and reconfigure. Sorry for that. But I touched only templates.yaml.

ok, no problem.

Thanks for spotting all those, corrected here:

- 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"
      device_class: door
      state: >
        {% if trigger.platform == 'webhook' %}
          {{ trigger.json.doorsensorState == 3 }}
        {% else %}
          {{ is_state('sensor.nuki_door_sensor_state', 'open') }}
        {% endif %}
      availability: >
        {% if trigger.platform == 'webhook' %}
          {{ trigger.json.doorsensorState != None }}
        {% else %}
          {{ (is_state('sensor.nuki_door_sensor_state', 'open') or is_state('sensor.nuki_door_sensor_state', 'closed')) }}
        {% 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 = states(sensor.nuki_door_sensor_state) %}
          {% set triglock = states(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 %}
      attributes:
        nuki_id: >
          {% if trigger.platform == 'webhook' %}
            {{ trigger.json.nukiId }}
          {% else %}
            {{ states(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 %}
            {{ states(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 %}
            {{ states(sensor.nuki_lock_sensor_state) }}
          {% endif %}
        lock_battery: >
          {% if trigger.platform == 'webhook' %}
            {{ trigger.json.batteryChargeState }}%
          {% else %}
            {{ states(sensor.nuki_lock_battery_level) }}
          {% endif %}
        lock_battery_critical: >
          {% if trigger.platform == 'webhook' %}
            {{ trigger.json.batteryCritical }}
          {% else %}
            {{ states(sensor.nuki_lock_battery_critical_state) }}
          {% endif %}
        keypad_battery_critical: >
          {% if trigger.platform == 'webhook' %}
            {{ trigger.json.keypadBatteryCritical }}
          {% else %}
            {{ states(sensor.nuki_keypad_battery_critical_state) }}
          {% endif %}
        last_update: "{{ strptime(as_timestamp(now()) | timestamp_local, '%Y%m%d %H:%M:%S') }}"
        trigger: '{{ trigger.platform }}'

I just did a template reload, and same result, unfortunately.

image

Here’s what’s in the logs after the reload:

Logger: homeassistant.components.template.binary_sensor
Source: components/template/trigger_entity.py:141
Integration: Template (documentation, issues)
First occurred: 1:59:04 (2 occurrences)
Last logged: 12:00:12

Error rendering icon template for binary_sensor.nuki_door_state: UndefinedError: ‘sensor’ is undefined

Logger: homeassistant.helpers.template
Source: helpers/template.py:1377
First occurred: 1:59:04 (2 occurrences)
Last logged: 12:00:12

Template variable error: ‘sensor’ is undefined when rendering ‘{% 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 %}’
Template variable error: ‘sensor’ is undefined when rendering ‘{% 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 = states(sensor.nuki_door_sensor_state) %} {% set triglock = states(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 %}’

Sorry, my bad (even if at my third coffee already, but not Italian ones!).
I forgot the ' around the sensor object:

          {% set trigdoor = states('sensor.nuki_door_sensor_state') %}
          {% set triglock = states('sensor.nuki_lock_sensor_state') %}

Also to correct in several of the attributes.
Don’t worry, there is some light at the end of the tunnel…

Yes, if you refer to None without quotes it’s because it’s a binary_sensor, I first enclosed it with quotes and it didn’t work, I found out had to put it without quotes. probably there’s a better style to code that state, but I’m not at the stylistic level yet. :smiley:

Doesn’t work without single quotes?

Checked/modified all states() is_state() is_statte_attr() in templates.yaml. v4.4 released. :slight_smile:

@Friedrieck IT WOOOOOOOOOOOOOOOOOOOOOOORKS!!!

I did template reload and I got this:

image

NO ERRORS IN THE LOG ANYMORE AFTER RELOAD!!

So freakiiiiiiiiiiiiiiiiiing happy!!! :rofl:

I’ll do more testing, restarting HA too…if it works, we’re done! :smiley:

Thank you my friend!!!

1 Like

And check this: notice the event? it means it was initialized/refreshed by platform event. :smiley:

I added these Trigger Variables to debug better…

        trigger_platform: "{{ trigger.platform }}"
        trigger_event: "{{ trigger.event }}"
        trigger_event_type: "{{ trigger.event.event_type }}"
        trigger_event_data: "{{ trigger.event.data }}"
1 Like

Excellent! You can now remove your reservations:

[…]

image

Now we can monitor things better. :slight_smile:

Are you sure we’re done? As an italian, I’m a little bit superstitious…:smiley: (joking)

Now tell me the truth: when you said there’s light at the end of the tunnel, you had already tested it and noticed it worked ok, right? :smiley: