Help with logic with this template sensor to overcome mising lock reporting

With much help from others here this template sensor has been created and works quite well. One minor issue, not related to the sensor, but more related to my schlage lock not reporting (or open zwave not reading) a few states would be nice to fix. The value for HA controlled is what does not get updated. Manual lock/unlock, gets reported. Keypad Lock/unlock gets reported. Jammed gets reported. I was thinking bascially if the lock state changes to locked or unlocked, but the other values dont change its safe to say that is was HA controlled.

Just trying to think of the best way to add that logic,

Here is the sensor

      front_door_report:
        friendly_name: 'Front Door Report'
        value_template: >
          {% set number = states('sensor.lock_front_door_deadbolt_alarm_level') %}
          {% set alarm_type_value = states('sensor.lock_front_door_deadbolt_alarm_type') %}
          {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
          {% set user = 'Master' if number == '0' else states(entity_id) %}
          {% set alarm_type_general_actions = {
            '0':'No Status Reported',          
            '9':'Lock Jammed',
            '17':'Keypad Lock Jammed',
            '21':'Manual Lock',
            '22':'Manual Unlock',
            '23':'HA Lock Jammed',
            '24':'HA Lock',
            '25':'HA Unlock',
            '26':'Auto Lock Jammed',
            '27':'Auto Lock',
            '32':'All Codes Deleted',
            '161':'Bad Code Entered',
            '167':'Battery Low',
            '168':'Battery Critical',
            '169':'Battery Too Low To Operate Lock' } %}
          {% set alarm_type_lock_actions = {
            '18':'Keypad Lock',
            '19':'Keypad Unlock',
            '162':'Lock Code Attempt Outside of Schedule' } %}
          {% set alarm_type_code_actions = {
            '33':'Code Deleted',
            '112':'Code Changed',
            '113':'Duplicate Code' } %}
          {% if alarm_type_value in alarm_type_code_actions %}
            {{ alarm_type_code_actions[alarm_type_value] }} (Code {{ number}})
          {% elif alarm_type_value in alarm_type_lock_actions  %}
            {{ alarm_type_lock_actions[alarm_type_value] }} with Code {{number }} ({{ user }})
          {% elif alarm_type_value in alarm_type_general_actions %}
            {{ alarm_type_general_actions[alarm_type_value] }}
          {% else %}
            Unknown Alarm Type Value {{ states('sensor.lock_front_door_deadbolt_alarm_type') }}
          {% endif %}

Really just brainstorming at the momement, Also I have a schlage, these values may be working for yale, or other locks, these values are pretty much zwave standard values.

Thanks

I know this is not it, but this is generally what I’m trying to get figured out. Is this possible in a template sensor.

{% set HA_controled = 'HA' if (as_timestamp(now())-as_timestamp(states.lock.lock_front_door_deadbolt.last_changed)) < 15 and (as_timestamp(now())-as_timestamp(states.sensor.lock_front_door_deadbolt_alarm_type.last_changed)) > 15  else 'no' %}

Goal would be that is this was true ie HA, then at the end update the sensor to say the door was “locked/unlocked” by HA.

Answering my own question, Well Mostly anyway.

This does return what I’d want

{% set ha_controled = 'HA' if (as_timestamp(now())-as_timestamp(states.lock.lock_front_door_lock.last_changed)) < 15 and (as_timestamp(now())-as_timestamp(states.sensor.lock_front_door_deadbolt_alarm_type.last_changed)) > 15  else 'no' %}

{{ ha_controled }}

Well it’s close for 15 seconds the value will be HA, then changes to no. I also need to add something to the logic at the end where the sensor actually gets set

          {% if alarm_type_value in alarm_type_code_actions %}
            {{ alarm_type_code_actions[alarm_type_value] }} (Code {{ number}})
          {% elif alarm_type_value in alarm_type_lock_actions and number == '0' %}
            {{ alarm_type_lock_actions[alarm_type_value] }} with Schlage Button
          {% elif alarm_type_value in alarm_type_lock_actions  %}
            {{ alarm_type_lock_actions[alarm_type_value] }} with Code {{ number }} ({{ user }})
          {% elif alarm_type_value in alarm_type_general_actions %}
            {{ alarm_type_general_actions[alarm_type_value] }}
          {% else %}
            Unknown Alarm Type Value {{ states('sensor.lock_front_door_deadbolt_alarm_type') }}
          {% endif %}

EDIT: I think this Should work, probably missing something small. While it does not error, it always returns a value of no

{% set ha_controled = 'HA' if (as_timestamp(now())-as_timestamp(states.lock.lock_front_door_lock.last_changed)) < 30 and (as_timestamp(now())-as_timestamp(states.sensor.lock_front_door_deadbolt_alarm_type.last_changed)) > 30  and (states.lock.lock_front_door_lock) == 'locked 'else 'no' %}
{{ ha_controled }}

My thought is to use this to set the value I want, when the door is locked, and the other time stamps are valid.

I suspect that my state for Locked is not quite right? Any help is appreciated

Ok, here is the solution. I know I’m just talking to myself, but hopefully this sort of logic will prove useful to someone else in the future

        value_template: >
          {% set number = states('sensor.lock_front_door_deadbolt_alarm_level') %}
          {% set alarm_type_value = states('sensor.lock_front_door_deadbolt_alarm_type') %}
          {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
          {% set user = 'Master' if number == '0' else states(entity_id) %}
          {% set alarm_type_value = '24' if (as_timestamp(now())-as_timestamp(states.lock.lock_front_door_lock.last_changed)) < 15 and (as_timestamp(now())-as_timestamp(states.sensor.lock_front_door_deadbolt_alarm_type.last_changed)) > 15  and (states.lock.lock_front_door_lock.state) == 'locked' else alarm_type_value %}
          {% set alarm_type_value = '25' if (as_timestamp(now())-as_timestamp(states.lock.lock_front_door_lock.last_changed)) < 15 and (as_timestamp(now())-as_timestamp(states.sensor.lock_front_door_deadbolt_alarm_type.last_changed)) > 15  and (states.lock.lock_front_door_lock.state) == 'unlocked' else alarm_type_value %}
          {% set alarm_type_general_actions = {
            '0':'No Status Reported',          
            '9':'Lock Jammed',
            '17':'Keypad Lock Jammed',
            '21':'Manual Lock',
            '22':'Manual Unlock',
            '23':'HA Lock Jammed',
            '24':'HA Lock',
            '25':'HA Unlock',
            '26':'Auto Lock Jammed',
            '27':'Auto Lock',
            '32':'All Codes Deleted',
            '161':'Bad Code Entered',
            '167':'Battery Low',
            '168':'Battery Critical',
            '169':'Battery Too Low To Operate Lock' } %}
          {% set alarm_type_lock_actions = {
            '18':'Keypad Lock',
            '19':'Keypad Unlock',
            '162':'Lock Code Attempt Outside of Schedule' } %}
          {% set alarm_type_code_actions = {
            '33':'Code Deleted',
            '112':'Code Changed',
            '113':'Duplicate Code' } %}
          {% if alarm_type_value in alarm_type_code_actions %}
            {{ alarm_type_code_actions[alarm_type_value] }} (Code {{ number}})
          {% elif alarm_type_value in alarm_type_lock_actions and number == '0' %}
            {{ alarm_type_lock_actions[alarm_type_value] }} with Schlage Button
          {% elif alarm_type_value in alarm_type_lock_actions  %}
            {{ alarm_type_lock_actions[alarm_type_value] }} with Code {{ number }} ({{ user }})
          {% elif alarm_type_value in alarm_type_general_actions %}
            {{ alarm_type_general_actions[alarm_type_value] }}
          {% else %}
            Unknown Alarm Type Value {{ states('sensor.lock_front_door_deadbolt_alarm_type') }}
          {% endif %}
1 Like