Template Help - Review

I started this with a very long automation, but as I kept tweaking, I think I’ve got this thing way streamlined and it almost seems too simple. haha

Two questions
1 - Does this look like it should work?
2 - Do I need an {% else %}

  - alias: Keypad One Time Use for codes 16 through 18
    trigger:
      - platform: state
        entity_id:
          - sensor.lock_front_door_deadbolt_alarm_level
          - sensor.lock_back_door_deadbolt_alarm_level
          - sensor.lock_garage_door_lock_alarm_level
    action:
      - service: script.turn_on
        entity_id: >-
          {% if is_state('trigger.entity_id', '16') %}
            script.door_keypad_16_delete
          {% if is_state('trigger.entity_id', '17') %}
            script.door_keypad_17_delete
          {% if is_state('trigger.entity_id', '18') %}
            script.door_keypad_18_delete
          {% endif %}

Should I put a condition in here to only run for values of 16, 17, 18?

2.You don’t actually need the {% else %} but I would normally use it to be safe
1.Not sure if it will work. you may need to use the `to_state", e.g. something like this:

          {% if is_state('trigger.to_state', '16') %}
            script.door_keypad_16_delete
          {% elif is_state('trigger.to_state', '17') %}
            script.door_keypad_17_delete
          {% elif is_state('trigger.to_state', '18') %}
            script.door_keypad_18_delete
          {% endif %}

I started looking at it again, does this look like it would work?

    action:
      - service: script.turn_on
        entity_id: >-
          {% set code_slot = states.trigger.to_state.state %}
          {{ 'script.door_keypad_' ~ code_slot ~ '_delete' }}

Ok, I tweaked it one more time, and added a condition

  - alias: Keypad One Time Use for codes 16 through 18
    trigger:
      - platform: state
        entity_id:
          - sensor.lock_front_door_deadbolt_alarm_level
          - sensor.lock_back_door_deadbolt_alarm_level
          - sensor.lock_garage_door_lock_alarm_level
    condition:
    - condition: template
      value_template: '{{ trigger.to_state.state | int > 15 }}'   
    action:
      - service: script.turn_on
        entity_id: >-
          {% set code_slot = states.trigger.to_state.state %}
          {{ 'script.door_keypad_' ~ code_slot ~ '_delete' }}

I’ve been pulling ideas and scripts from a various posts on here related to zwave door locks. After I test this and a bit more of the package I’ll post it on here.

1 Like

Close, but I think you need to add data_template:

    action:
      - service: script.turn_on
        data_template:
          entity_id: >-
            {% set code_slot = states.trigger.to_state.state %}
            {{ 'script.door_keypad_' ~ code_slot ~ '_delete' }}

Also if you only have scripts for 16 through 18, then I would enhance your condition to:

    condition:
    - condition: template
      value_template: '{{ 16 <= (trigger.to_state.state | int) <= 18 }}'   

Otherwise it looks good.