Hi Allessandro,
Again a big thank you for your work. I have extended it by inserting a possibility to translate some of the sensors. To achieve the possibility for translation, I made these changes:
- Sensor states are now based on numerical values instead of alpha numerical values where needed
- Added a input_select variable for selecting the language. There are now 3 possibilities, English, German and Dutch. Unfortunately not Italian because I don’t know that language, but it can be added. Please leave “en” as the first option in the selection list, because if this input select isn’t known, it will default to the first option in the list. Languages can be added bij searching for “{% elif states(‘input_select.nuki_language’) ==”. Just adding another “{% elif states(‘input_select.nuki_language’) == ‘’ %}” followed by a string in the new language will do the job. After a restart of HA, the (new) language can be set from “Configuration → Helpers → Nuki language for HA UI”.
So here is the code:
########################
# Nuki Card v8.0 #
########################
#######################################################################################################################
### ###
### Automations ###
### ###
#######################################################################################################################
automation:
- id: "nuki_card_callback"
alias: Nuki Card Callback
description: Automation for the Nuki doorlock
trigger:
- platform: webhook
webhook_id: !secret nuki_bridge_webhook
- platform: event
event_type:
- automation_reloaded
- event_template_reloaded
- platform: state
entity_id: sensor.nuki_door_sensor_state
- platform: state
entity_id: sensor.nuki_lock_sensor_state
- platform: state
entity_id: binary_sensor.nuki_bridge_callback
to: "off"
condition: []
mode: single
action:
- choose:
- conditions: >
{{ trigger.platform == 'webhook' }}
sequence:
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_door_sensor_state
data:
value: >
{{ trigger.json.doorsensorState }}
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_lock_sensor_state
data:
value: >
{{ trigger.json.state }}
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_last_activity
data:
value: >
{{ as_timestamp(now()) | timestamp_custom("%H:%M:%S (%b %d)") }}
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_trigger_platform
data:
value: >
{{ trigger.platform }}
- conditions: >
{{ trigger.platform == 'state' or trigger.platform == 'event' }}
sequence:
- service: script.nuki_bridge_check_callback
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_door_sensor
data:
value: >
{{ states('sensor.nuki_door_sensor_state') }}
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_lock_sensor
data:
value: >
{{ states('sensor.nuki_lock_sensor_state') }}
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_last_activity
data:
value: >
{{ states('sensor.nuki_last_activity') }}
- service: input_text.set_value
target:
entity_id: input_text.nuki_bridge_trigger_platform
data:
value: >
{{ trigger.platform }}
default: []
#######################################################################################################################
### ###
### Input Selects ###
### ###
#######################################################################################################################
input_select:
nuki_language:
name: "Nuki language for HA UI"
options:
- en
- de
- nl
icon: mdi:blur-radial
#######################################################################################################################
### ###
### Input Texts & Numbers ###
### ###
#######################################################################################################################
input_text:
nuki_bridge_url:
name: "Nuki Card (bridge url)"
initial: !secret nuki_bridge_url
nuki_bridge_token:
name: "Nuki Card (bridge token)"
initial: !secret nuki_bridge_token
nuki_bridge_webhook:
name: "Nuki Card (bridge webhook)"
initial: !secret nuki_bridge_webhook
max: 255
nuki_ha_internal_url:
name: "Nuki Card (HA internal URL)"
initial: !secret nuki_ha_internal_url
nuki_bridge_door_sensor:
name: "Nuki Card (door sensor)"
nuki_bridge_door_sensor_state:
name: "Nuki Card (door sensor state)"
nuki_bridge_lock_sensor:
name: "Nuki Card (lock sensor)"
nuki_bridge_lock_sensor_state:
name: "Nuki Card (lock sensor state)"
nuki_bridge_last_activity:
name: "Nuki Card (last activity)"
nuki_bridge_trigger_platform:
name: "Nuki Card (trigger platform)"
#######################################################################################################################
### ###
### Binary sensors ###
### ###
#######################################################################################################################
binary_sensor:
- platform: template
sensors:
nuki_door_state:
unique_id: nuki_door_state
friendly_name: "Nuki Door State"
device_class: door
value_template: >
{{ is_state_attr('sensor.nuki_door_sensor_state','numeric_state', 3) }}
availability_template: >
{{ is_state_attr('sensor.nuki_door_sensor_state','numeric_state', 3) or is_state_attr('sensor.nuki_door_sensor_state','numeric_state', 2) }}
icon_template: >
{% if is_state_attr('sensor.nuki_door_sensor_state','numeric_state', 3) %}
mdi:door-open
{% elif is_state_attr('sensor.nuki_door_sensor_state','numeric_state', 2) and is_state_attr('sensor.nuki_lock_sensor_state','numeric_state', 1) %}
mdi:door-closed-lock
{% elif is_state_attr('sensor.nuki_door_sensor_state','numeric_state', 2) and is_state_attr('sensor.nuki_lock_sensor_state','numeric_state', 3) %}
mdi:door-closed
{% else %}
mdi:alert-box-outline
{% endif %}
attribute_templates:
trigger_platform: >
{{ states('input_text.nuki_bridge_trigger_platform') }}
bridge_callback: >
{% if state_attr('binary_sensor.nuki_bridge_callback','callback_id')|int > -1 %}
Configured (ID: {{ state_attr('binary_sensor.nuki_bridge_callback','callback_id') }})
{% else %}
Not configured (ID: {{ state_attr('binary_sensor.nuki_bridge_callback','callback_id') }})
{% endif %}
nuki_id: >
{{ states('sensor.nuki_id') }}
door_state: >
{{ states('input_text.nuki_bridge_door_sensor') }}
lock_state: >
{{ states('input_text.nuki_bridge_lock_sensor') }}
lock_battery: >
{{ states('sensor.nuki_lock_battery_level') }}
lock_battery_critical: >
{{ states('sensor.nuki_lock_battery_critical_state') }}
keypad_battery_critical: >
{% if states('sensor.nuki_keypad_battery_critical_state') != 'unknown' %}
{{ states('sensor.nuki_keypad_battery_critical_state') }}
{% else %}
not installed
{% endif %}
last_update_callback: >
{{ states('input_text.nuki_bridge_last_activity') }}
last_update_polled: >
{{ states('sensor.nuki_last_activity') }}
door_sensor_polled: >
{{ states('sensor.nuki_door_sensor_state') }}
lock_sensor_polled: >
{{ states('sensor.nuki_lock_sensor_state') }}
nuki_bridge_callback:
unique_id: nuki_bridge_callback
friendly_name: "Nuki Bridge Callback"
icon_template: mdi:arrow-vertical-lock
value_template: >
{% if states('sensor.nuki_bridge_callback_list') == 'OK' %}
{% for callback in state_attr('sensor.nuki_bridge_callback_list','callbacks') %}
{% if states('input_text.nuki_bridge_webhook') in callback.url %}
on
{% else %}
off
{% endif %}
{% endfor %}
{% else %}
off
{% endif %}
attribute_templates:
callback_id: >
{% if states('sensor.nuki_bridge_callback_list') == 'OK' %}
{% for callback in state_attr('sensor.nuki_bridge_callback_list','callbacks') %}
{% if states('input_text.nuki_bridge_webhook') in callback.url %}
{{ callback.id }}
{% else %}
{{ -1 }}
{% endif %}
{% endfor %}
{% else %}
{{ -1 }}
{% endif %}
#######################################################################################################################
### ###
### Locks ###
### ###
#######################################################################################################################
lock:
- platform: template
name: "Nuki Lock Action"
unique_id: nuki_lock_action
value_template: >
{{ is_state_attr('sensor.nuki_lock_sensor_state', 'numeric_state', 1) }}
availability_template: >
{{ is_state_attr('sensor.nuki_lock_sensor_state','numeric_state', 1) or is_state_attr('sensor.nuki_lock_sensor_state','numeric_state', 3) }}
lock:
service: rest_command.nuki_lock_action
data:
action: 2
unlock:
service: rest_command.nuki_lock_action
data:
action: 1
#######################################################################################################################
### ###
### rest commands ###
### ###
#######################################################################################################################
rest_command:
nuki_lock_action:
url: "{{ states('input_text.nuki_bridge_url') }}/lockAction?nukiId={{ states('sensor.nuki_id') }}&action={{ action }}&token={{ states('input_text.nuki_bridge_token') | urlencode }}"
nuki_bridge_add_callback:
url: "{{ states('input_text.nuki_bridge_url') }}/callback/add?url={{ states('input_text.nuki_ha_internal_url') | urlencode }}/api/webhook/{{ states('input_text.nuki_bridge_webhook') | urlencode }}&token={{ states('input_text.nuki_bridge_token') | urlencode }}"
nuki_bridge_del_callback:
url: "{{ states('input_text.nuki_bridge_url') }}/callback/remove?id={{ state_attr('sensor.nuki_bridge_callback','callback_id') }}&token={{ states('input_text.nuki_bridge_token') | urlencode }}"
#######################################################################################################################
### ###
### scripts ###
### ###
#######################################################################################################################
script:
nuki_bridge_check_callback:
sequence:
- service: homeassistant.update_entity
target:
entity_id: sensor.nuki_bridge_callback_list
- service: homeassistant.update_entity
target:
entity_id: binary_sensor.nuki_bridge_callback
- alias: "Repeat the sequence UNTIL the conditions are true"
repeat:
while: "{{ states('binary_sensor.nuki_bridge_callback') == 'off' and repeat.index < 5 }}"
sequence:
- service: rest_command.nuki_bridge_add_callback
- service: homeassistant.update_entity
target:
entity_id: sensor.nuki_bridge_callback_list
- service: homeassistant.update_entity
target:
entity_id: binary_sensor.nuki_bridge_callback
- delay:
seconds: 5
- service: persistent_notification.create
data:
notification_id: "nuki_card_persistent"
title: Nuki Card (bridge configuration)
message: >
* [**Script nuki_bridge_check_callback**] ran at {{ as_timestamp(now()) | timestamp_custom("%H:%M:%S (%b %d)") }}
* [**Bridge Callback ID:**] "{{ state_attr('binary_sensor.nuki_bridge_callback','callback_id') }}"
* [**Callback List:**] {{ state_attr('sensor.nuki_bridge_callback_list','callbacks') }}
#######################################################################################################################
### ###
### sensors ###
### ###
#######################################################################################################################
sensor:
- platform: rest
scan_interval: 120
resource_template: "{{ states('input_text.nuki_bridge_url') }}/callback/list?&token={{ states('input_text.nuki_bridge_token') | urlencode }}"
name: "Nuki Bridge Callback List"
value_template: >
{% if (state_attr('sensor.nuki_bridge_callback_list','callbacks') | trim | length) >= 2 %}
OK
{% else %}
{% if states('input_select.nuki_language') == 'de' %}
Nicht konfiguriert
{% elif states('input_select.nuki_language') == 'nl' %}
Niet gecofigureerd
{% else %}
Not configured
{% endif%}
{% endif %}
json_attributes:
- callbacks
- platform: rest
scan_interval: 120
resource_template: "{{ states('input_text.nuki_bridge_url') }}/list?&token={{ states('input_text.nuki_bridge_token') | urlencode }}"
name: "Nuki Bridge Endpoint List"
value_template: "OK"
json_attributes:
- lastKnownState
- firmwareVersion
- nukiId
- name
- platform: rest
scan_interval: 120
resource_template: "{{ states('input_text.nuki_bridge_url') }}/info?&token={{ states('input_text.nuki_bridge_token') | urlencode }}"
name: "Nuki Bridge Endpoint Info"
value_template: "OK"
json_attributes:
- versions
- scanResults
- wlanConnected
- serverConnected
- platform: template
sensors:
nuki_device_name:
unique_id: nuki_device_name
friendly_name: "Nuki Device Name"
icon_template: mdi:alphabetical-variant
value_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_info','scanResults')[0]['name'] }}
{% endif %}
nuki_bridge_fw_version:
unique_id: nuki_bridge_fw_version
friendly_name: "Nuki Bridge FW Version"
icon_template: mdi:numeric
value_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_info','versions')['firmwareVersion'] }}
{% endif %}
nuki_bridge_lock_bt_rssi:
unique_id: nuki_bridge_lock_bt_rssi
friendly_name: "Nuki Bridge<->Lock BT RSSI"
icon_template: mdi:signal-distance-variant
value_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_info','scanResults')[0]['rssi'] }}
{% endif %}
nuki_bridge_wifi_connected:
unique_id: nuki_bridge_wifi_connected
friendly_name: "Nuki Bridge WiFi Connected"
icon_template: mdi:wifi-cog
value_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_info','wlanConnected') }}
{% endif %}
nuki_bridge_wifi_fw_version:
unique_id: nuki_bridge_wifi_fw_version
friendly_name: "Nuki Bridge WiFi FW Version"
icon_template: mdi:numeric
value_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_info','versions')['wifiFirmwareVersion'] }}
{% endif %}
nuki_bridge_cloud_connected:
unique_id: nuki_bridge_cloud_connected
friendly_name: "Nuki Bridge Cloud Connected"
icon_template: mdi:server-network
value_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_info','serverConnected') }}
{% endif %}
nuki_bridge_lock_bt_state:
unique_id: nuki_bridge_lock_bt_state
friendly_name: "Nuki Bridge<->Lock BT State"
icon_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{% if state_attr('sensor.nuki_bridge_endpoint_info','scanResults')[0]['paired'] %}
mdi:bluetooth-connect
{% elif not state_attr('sensor.nuki_bridge_endpoint_info','scanResults')[0]['paired'] %}
mdi:bluetooth-off
{% else %}
mdi:bluetooth-audio
{% endif %}
{% endif %}
value_template: >
{% if states('sensor.nuki_bridge_endpoint_info') == "OK" %}
{% if state_attr('sensor.nuki_bridge_endpoint_info','scanResults')[0]['paired'] %}
{% if states('input_select.nuki_language') == 'de' %}
angebunden
{% elif states('input_select.nuki_language') == 'nl' %}
verbonden
{% else %}
connected
{% endif%}
{% elif not state_attr('sensor.nuki_bridge_endpoint_info','scanResults')[0]['paired'] %}
{% if states('input_select.nuki_language') == 'de' %}
getrennt
{% elif states('input_select.nuki_language') == 'nl' %}
verbroken
{% else %}
disconnected
{% endif%}
{% else %}
{% if states('input_select.nuki_language') == 'de' %}
unbekannt
{% elif states('input_select.nuki_language') == 'nl' %}
onbekend
{% else %}
unknown
{% endif%}
{% endif %}
{% endif %}
nuki_id:
unique_id: nuki_id
friendly_name: "Nuki ID"
icon_template: mdi:numeric
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_list','nukiId') }}
{% endif %}
nuki_lock_fw_version:
unique_id: nuki_lock_fw_version
friendly_name: "Nuki Lock FW Version"
icon_template: mdi:numeric
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_list','firmwareVersion') }}
{% endif %}
nuki_lock_battery_critical_state:
unique_id: nuki_lock_battery_critical_state
friendly_name: "Nuki Lock Battery Critical State"
icon_template: mdi:battery-alert-variant-outline
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_list','lastKnownState')['batteryCritical'] }}
{% endif %}
nuki_friendly_name:
unique_id: nuki_friendly_name
friendly_name: "Nuki Friendly Name"
icon_template: mdi:numeric
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{{ state_attr('sensor.nuki_bridge_endpoint_list','name') }}
{% endif %}
nuki_last_activity:
unique_id: nuki_last_activity
friendly_name: "Nuki Last Activity"
icon_template: mdi:clock-check-outline
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{{ (as_timestamp(state_attr('sensor.nuki_bridge_endpoint_list','lastKnownState')['timestamp'])) | timestamp_custom("%H:%M:%S (%b %d)") }}
{% endif %}
nuki_lock_battery_level:
unique_id: nuki_lock_battery_level
friendly_name: "Nuki Lock Battery Level"
device_class: "battery"
unit_of_measurement: "%"
icon_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{% set battery_level = state_attr('sensor.nuki_bridge_endpoint_list', 'lastKnownState')['batteryChargeState'] | default(0) | int %}
{% set battery_charging = state_attr('sensor.nuki_bridge_endpoint_list', 'lastKnownState')['batteryCharging'] %}
{% set battery_round = (battery_level / 10) | int * 10 %}
{% if battery_round >= 100 and not battery_charging %}
mdi:battery
{% elif battery_round >= 100 and battery_charging %}
mdi:battery-charging
{% elif battery_round > 0 and not battery_charging %}
mdi:battery-{{ battery_round }}
{% elif battery_round > 0 and battery_charging %}
mdi:battery-charging-{{ battery_round }}
{% else %}
mdi:battery-alert-variant-outline
{% endif %}
{% endif %}
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{% set battery_level = state_attr('sensor.nuki_bridge_endpoint_list', 'lastKnownState')['batteryChargeState'] | default(0) | int %}
{% if state_attr('sensor.nuki_bridge_endpoint_list', 'lastKnownState')['batteryCharging'] %}
{{ battery_level }}
{% else %}
{{ battery_level }}
{% endif %}
{% endif %}
nuki_door_sensor_state:
unique_id: nuki_door_sensor_state
friendly_name: "Nuki Door Sensor State"
icon_template: >
{% if is_state('input_text.nuki_bridge_door_sensor_state', '2') and is_state('input_text.nuki_bridge_lock_sensor_state', '3') %}
mdi:door-closed
{% elif is_state('input_text.nuki_bridge_door_sensor_state', '2') and is_state('input_text.nuki_bridge_lock_sensor_state', '1') %}
mdi:door-closed-lock
{% elif is_state('input_text.nuki_bridge_door_sensor_state', '3') %}
mdi:door-open
{% endif %}
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{% if states('input_select.nuki_language') == 'de' %}
{% set my_state = {1: 'deactiviert', 2: 'gesperrt', 3: 'geöffnet', 4: 'unbekannt', 5: 'kalibrierend'} %}
{% elif states('input_select.nuki_language') == 'nl' %}
{% set my_state = {1: 'gedeactiveerd', 2: 'gesloten', 3: 'open', 4: 'onbekend', 5: 'aan het calibreren'} %}
{% else %}
{% set my_state = {1: 'deactivated', 2: 'closed', 3: 'open', 4: 'unknown', 5: 'calibrating'} %}
{% endif%}
{{ my_state[states('input_text.nuki_bridge_door_sensor_state') | int] }}
{% endif %}
attribute_templates:
numeric_state: >
{{ states('input_text.nuki_bridge_door_sensor_state') | int }}
nuki_lock_sensor_state:
unique_id: nuki_lock_sensor_state
friendly_name: "Nuki Lock Sensor State"
icon_template: >
{% if is_state('input_text.nuki_bridge_lock_sensor_state', '1') %}
mdi:lock-outline
{% elif is_state('input_text.nuki_bridge_lock_sensor_state', '3') %}
mdi:lock-open-outline
{% endif %}
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{% if states('input_select.nuki_language') == 'de' %}
{% set my_state = {0: 'nict kalibriert', 1: 'gesperrt', 2:'aufmachen', 3: 'geöffnet', 4: 'sperren', 5: 'aufgeklinkt', 6: "geöffnet (lock ‘n’ go)", 7: 'aufklinkend', 254: 'Antrieb blockiert', 255: 'undefiniert'} %}
{% elif states('input_select.nuki_language') == 'nl' %}
{% set my_state = {0: 'niet gecalibreerd', 1: 'gesloten', 2:'openen', 3: 'open', 4: 'sluiten', 5: 'ontgrendeld', 6: "open (lock ‘n’ go)", 7: 'ontgrendelen', 254: 'moter geblokkeerd', 255: 'onbekend'} %}
{% else %}
{% 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'} %}
{% endif%}
{{ my_state[states('input_text.nuki_bridge_lock_sensor_state') | int] }}
{% endif %}
attribute_templates:
numeric_state: >
{{ states('input_text.nuki_bridge_lock_sensor_state') | int }}
nuki_keypad_battery_critical_state:
unique_id: nuki_keypad_battery_critical_state
friendly_name: "Nuki Keypad Battery Critical State"
icon_template: mdi:battery-alert-variant-outline
value_template: >
{% if states('sensor.nuki_bridge_endpoint_list') == "OK" %}
{% if state_attr('sensor.nuki_bridge_endpoint_list', 'lastKnownState')['keypadBatteryCritical'] != null %}
{{ state_attr('sensor.nuki_bridge_endpoint_list', 'lastKnownState')['keypadBatteryCritical'] }}
{% else %}
{% if states('input_select.nuki_language') == 'de' %}
nicht installiert
{% elif states('input_select.nuki_language') == 'nl' %}
niet geïnstalleerd
{% else %}
not installed
{% endif%}
{% endif %}
{% endif %}