Hey, so I’m not sure how many people this applies to, but I’ve recently started using MQTT for my lock code management between Vera and HA, since the other methods (rest commands/sensors/etc) proved to be pretty unreliable.
I started with this MQTT plugin for Vera: GitHub - vosmont/vera-mqtt
Then added a multi-string container (I actually have several… likely due to my lack of knowledge on how to work with LUA and LUUP all that well… I created several versions by creating numerous devices, changing the references in all the docs from VContainer1 to VContainer[and some number]): GitHub - dklinkman/vera-multistringcontainer: Original container modified for AltUI
Also, on Vera, I created the following scenes (If you want to turn the same code on and off – I use this to enable my parents lock codes only when I’m gone… otherwise they can knock):
local waittime=30000;
luup.call_action(‘urn:micasaverde-com:serviceId:DoorLock1’, ‘SetPin’, {UserCodeName=‘USERCODENAME’, newPin=PINNUMBER, user=USERCODESLOT},LOCKDEVICENUMBER)
luup.sleep(waittime)
luup.call_action(‘urn:micasaverde-com:serviceId:HomeAutomationGateway1’, “RunScene”, {SceneNum = “SCENENUMBERTOCHECKLOCKCODES”}, 0)
SCENENUMBERTOCHECKLOCKCODES (MQTT on Vera Publishes the Containers to HA… you need to reset the values to 0 so it will republish it):
local waittime=10000;
luup.variable_set(“urn:upnp-org:serviceId:VContainer3”, “Variable1”,“”, 68)
luup.variable_set(“urn:upnp-org:serviceId:VContainer1”, “Variable1”,“”, 24)luup.sleep(waittime)
local lockcodes= luup.variable_get(“urn:micasaverde-com:serviceId:DoorLock1”,“PinCodes”,LOCKDEVICENUMBER)
if string.find(lockcodes, “Parents”) then
luup.variable_set(“urn:upnp-org:serviceId:VContainer3”, “Variable1”,“On”, 68)
else
luup.variable_set(“urn:upnp-org:serviceId:VContainer3”, “Variable1”,“Off”,68)
end
if string.find(lockcodes, “Temporary”) then
luup.variable_set(“urn:upnp-org:serviceId:VContainer1”, “Variable1”,“On”, 24)
else
luup.variable_set(“urn:upnp-org:serviceId:VContainer1”, “Variable1”,“Off”, 24)
end
To clear that code:
local waittime=30000;
luup.call_action(‘urn:micasaverde-com:serviceId:DoorLock1’, ‘ClearPin’, {UserCode=‘1’}, LOCKDEVICENUMBER)
luup.sleep(waittime)
luup.call_action(‘urn:micasaverde-com:serviceId:HomeAutomationGateway1’, “RunScene”, {SceneNum = “SCENENUMBERTOCHECKLOCKCODES”}, 0)
For a variable code (this took me a while to figure out), first you need to use the VContainer, with the parent as the MQTT client. Use whatever MQTT Topic as whatever you want, and the target as
urn:upnp-org:serviceId:VContainer4,Variable1=
Then a scene for when that is greater than 0:
local waittime=15000;
local tempcode = luup.variable_get(“urn:upnp-org:serviceId:VContainer4”, “Variable1”, 76)
luup.call_action(‘urn:micasaverde-com:serviceId:DoorLock1’, ‘SetPin’, {UserCodeName=‘Temporary’, newPin=tempcode, user=9}, LOCKDEVICENUMBER)
luup.sleep(waittime)
luup.sleep(waittime)
luup.call_action(‘urn:micasaverde-com:serviceId:HomeAutomationGateway1’, “RunScene”, {SceneNum = “SCENENUMBERTOCHECKLOCKCODES”}, 0)
luup.sleep(waittime)
luup.variable_set(“urn:upnp-org:serviceId:VContainer4”, “Variable1”,“0”, 76) # So this resets it back to 0
To get the last Usercode entered (MQTT on Vera Publishes the Container):
local locklastusercode = luup.variable_get(“urn:micasaverde-com:serviceId:DoorLock1”, “sl_UserCode”, LOCKDEVICENUMBER)
if string.find(locklastusercode, “USERCODENAME1”) then
luup.variable_set(“urn:upnp-org:serviceId:VContainer2”, “Variable1”,“USERCODENAME1”, 63)
elseif string.find(locklastusercode, “USERCODENAME2”) then
luup.variable_set(“urn:upnp-org:serviceId:VContainer2”, “Variable1”,“USERCODENAME2”, 63)
elseif string.find(locklastusercode, “USERCODENAME3”) then
luup.variable_set(“urn:upnp-org:serviceId:VContainer2”, “Variable1”,“USERCODENAME3”, 63)
elseif string.find(locklastusercode, “USERCODENAME4”) then
luup.variable_set(“urn:upnp-org:serviceId:VContainer2”, “Variable1”,“USERCODENAME4”, 63)
elseif string.find(locklastusercode, “USERCODENAME5”) then
luup.variable_set(“urn:upnp-org:serviceId:VContainer2”, “Variable1”,“USERCODENAME5”, 63)
else
luup.variable_set(“urn:upnp-org:serviceId:VContainer2”, “Variable1”,“Error”, 63)
end
Now on HA, I have input booleans for the lock codes I want to control. To clear it, I simply call the scenes from HA, which then updates the Input Boolean, or the Input Boolean will trigger the automation to call the scene, like this:
- id: mqtt_lock_temp_code_on
alias: MQTT Lock Temp Code On
initial_state: true
trigger:-
platform: mqtt
topic: “Vera/Events/Temp Lock Code”
condition:
condition: template
value_template: “{{ trigger.payload_json.Variable1 == ‘On’ }}”
action: -
service: input_boolean.turn_on
data:
entity_id: input_boolean.temp_lock_code_set -
id: mqtt_lock_temp_code_off
alias: MQTT Lock Temp Code Off
initial_state: true
trigger:- platform: mqtt
topic: “Vera/Events/Temp Lock Code”
condition:
condition: template
value_template: “{{ trigger.payload_json.Variable1 == ‘Off’ }}”
action: - service: automation.turn_off
entity_id: automation.lock_temp_lock_code_used - delay: 00:00:02
- service: input_boolean.turn_off
data:
entity_id: input_boolean.temp_lock_code_set - delay: 00:00:30
- service: automation.turn_on
entity_id: automation.lock_temp_lock_code_used
- platform: mqtt
-
And to set a variable lock code, with input text (you can do this with an input number as well), I have this in an automation:
- id: lock_set_temp_lock_code
alias: ‘Lock Set Temporary Lock Code’
trigger:- platform: state
entity_id: input_text.lock_code_temp
condition: - condition: template
value_template: “{{ not is_state(‘input_text.lock_code_temp’, ‘Code’) }}” - condition: state
entity_id: input_boolean.restart_complete
state: ‘on’
action:
- platform: state
- service: mqtt.publish
data_template:
topic: “Vera/Events/SetTempLockCode”
payload_template: “{{ states.input_text.lock_code_temp.state }}” - delay: 00:00:10
- service: input_text.set_value
data:
entity_id: input_text.lock_code_temp
value: ‘Code’ - delay: 00:00:60
- condition: state
entity_id: input_boolean.temp_lock_code_set
state: ‘off’ - service: notify.html5
data_template:
message: “Temporary Lock Code Did Not Update”
title: “Temporary Lock Code Not On”
data:
tag: ‘lock_not_updated’
renotify: 0
icon: ‘/local/lock.jpg’
badge: ‘/local/lock-badge.png’
I really hope this helps some people trying to do the same thing… took me FOREVER to figure out. I’m sorry about the formatting btw, the preformatted text doesn’t seem to be working…
If anyone wants to take a look at my config, if it’ll help, it’s up here: GitHub - mrneilix/Home-AssistantConfig