Set input_text from event data with template

I’m trying to make a history sensor for keypad entries with the following template. I was able to get some of the data with the print parts of the template, but when I started with the if part it didn’t work.

I’m not sure if my is defined comparison is correct. That came from another template comparing a variable set with a script field. I tried != "" before and was getting similar errors.

Is this possible, or is there a better approach?

action: input_text.set_value
metadata: {}
data:
  value: >
    {{ trigger.event.data.lockname }} {{ trigger.event.data.action_name }}
    {% if {{ trigger.event.data.code_slot_name }} is defined %}
      by {{ trigger.event.data.code_slot_name }}
    {% elif %}
    {% endif %}
target:
  entity_id: input_text.keypad_history

Mahalo, Mele Kalikimaka!

Don’t nest template delimiters, just use the variables…

Instead of:
{% if {{ trigger.event.data.code_slot_name }} is defined %}

{% if trigger.event.data.code_slot_name is defined %}

Also, if you aren’t using the elif get rid of it.

1 Like

Thank you!

I got it dialed in as far as the template.

I had to switch back to != "" as I I realized
trigger.event.data.code_slot_name is “defined” in the event data (even when it’s empty) once I quit nesting the delimiters. It’s defined as ""

I have a little more work to make the output pretty, but this is a great start, and is saves the important info.

action: input_text.set_value
metadata: {}
data:
  value: >
    {{ trigger.event.data.lockname }} {{ trigger.event.data.action_text }} {% if
    trigger.event.data.code_slot_name != "" %}
      by {{ trigger.event.data.code_slot_name }}
    {% else %} {% endif %}
target:
  entity_id: input_text.keypad_history

Thanks for all your help!

My confidence and ability with templates is gaining traction between your help as well as @taras133 and @petro, and others. Not only direct help, but indirect via all the help you’ve all given others over the years.

Much appreciated.