Optimize a value template with that has a lot of repeats?

I’ve updated it and fixed the if/elif (I think so anyway), now getting

Error rendering template: TemplateSyntaxError: expected token ‘:’, got ‘}’

I’m sure It’s a typo on my part, but not finding it

Tried a change of formatting based on some other recent posts, but still same error

      front_door_report:
        friendly_name: 'Front Door Report'
        value_template: >
          {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
          {% set sensor_codes = {
            '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 sensor_user_codes = {
            '18':'Keypad Lock',
            '19':'Keypad Unlock',
            '162':'Lock Code Attempt Outside of Schedule'} %}
          {% set sensor_lock_codes = {
            '33':'Code Deleted',
            '112':'Code Changed',
            '113':'Duplicate Code'} %}
          {% set sensor_code = "{{ states.sensor[lock_front_door_deadbolt_alarm_type].state }}" %}
          {% if sensor_code in sensor_lock_codes %}
            {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set user = 'Master' if number == '0' else 'User ' + number  %}
            {{ user }} {{ sensor_lock_codes[sensor_code] }}
          {% elif sensor_code in sensor_user_codes  %}
            {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
            {% set user = 'Master' if number == '0' else {{ states(entity_id) }} %}
            {{ sensor_lock_codes[sensor_code] }} by {{ user }}             
          {% elif sensor_code in sensor_codes %}
            {{ sensor_codes[sensor_code] }}
          {% else %}
            {{ states('sensor.front_door_report') }}
          {% endif %}

I’ve got a working “no errors” Template, now to test

      front_door_report:
        friendly_name: 'Front Door Report'
        value_template: >
          {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
          {% set sensor_codes = { 
            '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 sensor_user_codes = {
            '18':'Keypad Lock',
            '19':'Keypad Unlock',
            '162':'Lock Code Attempt Outside of Schedule' } %}
          {% set sensor_lock_codes = {
            '33':'Code Deleted',
            '112':'Code Changed',
            '113':'Duplicate Code' } %}
          {% set sensor_code = "{{ states.sensor[lock_front_door_deadbolt_alarm_type].state }}" %}
          {% if sensor_code in sensor_lock_codes %}
            {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set user = 'Master' if number == '0' else 'User ' + number  %}
            {{ user }} {{ sensor_lock_codes[sensor_code] }}
          {% elif sensor_code in sensor_user_codes  %}
            {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
            {% set user = 'Master' if number == '0' else "{{ states.[entity_id].state }}" %}
            {{ sensor_lock_codes[sensor_code] }} by {{ user }}  
          {% elif sensor_code in sensor_codes %}
            {{ sensor_codes[sensor_code] }}
          {% else %}
            {{ states('sensor.front_door_report') }}
          {% endif %}

Ok, well, the template is not causing errors, but it’s not working either, maybe someone can see what I’ve missed. My previous post has the yaml.

Here are screen shots showing the values

Replying to my own post with a working (testing in progress) template. This is pretty cool, will replace two or three sensors for me

      front_door_report:
        friendly_name: 'Front Door Report'
        value_template: >
          {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
          {% set sensor_codes = { 
            '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 sensor_user_codes = {
            '18':'Keypad Lock',
            '19':'Keypad Unlock',
            '162':'Lock Code Attempt Outside of Schedule' } %}
          {% set sensor_lock_codes = {
            '33':'Code Deleted',
            '112':'Code Changed',
            '113':'Duplicate Code' } %}
          {% set sensor_code = states.sensor.lock_front_door_deadbolt_alarm_type.state %}
          {% if sensor_code in sensor_lock_codes %}
            {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set user = 'Master' if number == '0' else 'User ' + number  %}
            {{ user }} {{ sensor_lock_codes[sensor_code] }}
          {% elif sensor_code in sensor_user_codes  %}
            {% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
            {% set user = 'Master' if number == '0' else "{{ states.[entity_id].state }}" %}
            {{ sensor_user_codes[sensor_code] }} by {{ user }}  
          {% elif sensor_code in sensor_codes %}
            {{ sensor_codes[sensor_code] }}
          {% else %}
            {{ states('sensor.front_door_report') }}
          {% endif %}

Any help with the format of my input select statement would be appreciated

         {% elif sensor_code in sensor_user_codes  %}
            {% set number = states.sensor.lock_front_door_deadbolt_alarm_level.state %}
            {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
            {% set user = 'Master' if number == '0' else states.entity_id.state %}
            {{ sensor_user_codes[sensor_code] }} by {{ user }}

I’m getting the following in the template test window

            Keypad Unlock by None

I got these when checking, so I’m sure it’s just the format for the state of the input text, just cannot figure it out
values {{ entity_id } = input_text.door_keypad_1_name
{{ user }} =None

I feel like I’ve tried every template combination to get USER to be the VALUE of "
‘input_text.door_keypad_’ + number + ‘_name’", but always ends up being the word

input_text.door_keypad_1_name, not the value.

Any tips on what I’m doing wrong here?

Because you have it in quotes.

I’ve tried so many combinations, but cannot seem to get it right. the quotes makes sense, but how do I get it to create the entity ID I want to use without adding the quotes? :expressionless:

what do you want the output to look like?

Sorry was away from my pc.

Correct. Thats why you need to use state(entity_id) to get the state from the ‘word’.

change this to:

        {% set user = 'Master' if number == '0' else states(entity_id) %}

I recommend changing all of these lines from that to this:

states('sensor.lock_front_door_deadbolt_alarm_level')

Try using this instead, see comments:

      front_door_report:
        friendly_name: 'Front Door Report'
        value_template: >
          #You cant encapsulate {{}} inside {%%}, commenting that out.
          #{% set number "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
          {% set number = states('lock_front_door_deadbolt_alarm_level') %}
          {% set sensor_codes = { 
            '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 sensor_user_codes = {
            '18':'Keypad Lock',
            '19':'Keypad Unlock',
            '162':'Lock Code Attempt Outside of Schedule' } %}
          {% set sensor_lock_codes = {
            '33':'Code Deleted',
            '112':'Code Changed',
            '113':'Duplicate Code' } %}
          {% set sensor_code = states('sensor.lock_front_door_deadbolt_alarm_type') %}
          {% if sensor_code in sensor_lock_codes %}
            #Next line isn't needed because it's already declared above.
            #{% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set user = 'Master' if number == '0' else 'User ' + number  %}
            {{ user }} {{ sensor_lock_codes[sensor_code] }}
          {% elif sensor_code in sensor_user_codes  %}
            #Next line isn't needed because it's already declared above.
            #{% set number = "{{ states.sensor[lock_front_door_deadbolt_alarm_level].state }}" %}
            {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
            #You cannot encapsulate {{}} inside {%%}, commenting out that line.
            #{% set user = 'Master' if number == '0' else "{{ states.[entity_id].state }}" %}
            {% set user = 'Master' if number == '0' else states(entity_id) %}
            {{ sensor_user_codes[sensor_code] }} by {{ user }}  
          {% elif sensor_code in sensor_codes %}
            {{ sensor_codes[sensor_code] }}
          {% else %}
            {{ states('sensor.front_door_report') }}
          {% endif %}

so without comments and alittle re-arranging to remove redundant lines (not necessary but makes it slightly shorter):

      front_door_report:
        friendly_name: 'Front Door Report'
        value_template: >
          {% set number = states('lock_front_door_deadbolt_alarm_level') %}
          {% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
          {% set user = 'Master' if number == '0' else states(entity_id) %}
          {% set sensor_codes = { 
            '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 sensor_user_codes = {
            '18':'Keypad Lock',
            '19':'Keypad Unlock',
            '162':'Lock Code Attempt Outside of Schedule' } %}
          {% set sensor_lock_codes = {
            '33':'Code Deleted',
            '112':'Code Changed',
            '113':'Duplicate Code' } %}
          {% set sensor_code = states('sensor.lock_front_door_deadbolt_alarm_type') %}
          {% if sensor_code in sensor_lock_codes %}
            {{ user }} {{ sensor_lock_codes[sensor_code] }}
          {% elif sensor_code in sensor_user_codes  %}
            {{ sensor_user_codes[sensor_code] }} by {{ user }}  
          {% elif sensor_code in sensor_codes %}
            {{ sensor_codes[sensor_code] }}
          {% else %}
            {{ states('sensor.front_door_report') }}
          {% endif %}
2 Likes

I was looking for the value/state of the input_text to be what is output, ie Paul. I’ll test out the template below when I get back home. I thought I had something similiar (well not as streamlined), before, but went through so many variations, I probably missed it. haha

Thank you so very much for your patience and help. I went ahead and just changed a few of the variable names, to help me keep them straight, and then changed the wording a bit based on testing. I added the code slot number as well, just in case there was not input_text for one of the code slots. Figured it would be easier to troubleshoot.

      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 = { 
            '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 %}
            {{ states('sensor.front_door_report') }}
          {% endif %}
1 Like

Do you (or anyone else) have any recommendations on how to best filter this template to use in other automations, scripts, etc? I’m thinking regex_search. This sensor is great, it ends up replacing three sensors with a nice sensor that gives me a nice history of actions on my lock in one place, which I like.

I have a few automations that used the “Keypad Unlock” as a trigger, but with this one the sensor state will include other words, such as Keypad unlock by code 1 (Paul), etc.

I might be over complicating things, and maybe just keeping the other sensor would be better/easier? But if it was not too complex, might be nice to be able to use this for other things as well.

I mean, you don’t need regex. just build your automation off the state change and have a condition that strips the string. Or you can use a value template trigger that does the same. Then use the startswith() method in conjunction with lower() to compare the starting words with a lowercase version of the text you expect. lower() takes an string with uppercase characters and turns them all into lowercase characters. startswith compares a string with a value, if the string starts with the value, it returns true.

condition method:

- alias: blah blah
  trigger:
    - platform: state
      entity_id: sensor.front_door_report
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state.lower().startswith('keypad unlock') }}"
  action:
     ....

trigger method:

- alias: blah blah
  trigger:
    - platform: template
      value_template: "{{ states('sensor.front_door_report').lower().startswith('keypad unlock') }}"
  action:
    ...

Thanks for this… I have a question… these two lines… I think are incorrect as we end up with User containing the passcode?

{% set entity_id = 'input_text.door_keypad_' + number + '_name' %}
{% set user = 'Master' if number == '0' else states(entity_id) %}

In my setup, input_text.door_keypad_’ + number + '_name

is a persons name.

Yah, I was under the impression that input_text.door_keypad_x_name was a user_id or name.

This line just adds a bunch of strings together to end up with an input_text entity id.

{% set entity_id = 'input_text.door_keypad_' + number + '_name' %}

This line checks the number. If the number equals zero, it sets the user equal to “Master”. If the number is any other number, it gets the state from the entity_id variable and sets user equal to that.

{% set user = 'Master' if number == '0' else states(entity_id) %}