Trigger one sensor value to another but updating too fast - use history?

Hey,
Been slowly getting to grips with HA and I’ve got a Yale (keyfree) Z-wave lock.
It presents in (via Z-wave JS) two alarm states to determine what it’s doing.

When one state (Alarm type) is either 18 or 19 the door has been unlocked with a code & the second state (Alarm Level) changes to the number of the code slot used.

So far so good

The challenge is that within ~1 second the lock returns to default state and the Alarm Level defaults back down to value 1.

I figured out using a template with a trigger that looks like:

  - trigger:
      - trigger: state
        entity_id: sensor.front_door_lock_alarm_type
        to:
          - "18"
          - "19"
    sensor:
      - name: "Front Door Lock Last Used Code"
        unique_id: 6basdasddc3-d4e5-4148-979b-8a98waasdasd59bb
        state: "{{ states('sensor.front_door_lock_alarm_level') }}"

This worked great a few times & still does work now when I manually manipuate the alarm states using dev tools.

However in every day practical use, when the template trigger is hit the Alarm Level seems to either be not updated yet, or I susspect has reverted back to it’s default “1” state.

Is there any way I can control the speed of execution, or prehaps more robustly access the history of the Level sensor to look at +0.5 seconds to record the last used key value?

Any pointers or thoughts welcome please!

So I thought of a quick way to do a test & believe that actually it is processing too fast. The value of the alarm_level hasn’t updated quite yet.

The test was set “last used code” to 2, set level to 4 and then enter code 3 into the lock. The value stored into 2 changed to 4.

Any way to pause for a split second before updating the value?

Try adding an action block with a delay? As I understand it the actions should execute before the state, attributes etc. are updated (so that you can run actions with a response_variable that can be used in the state templates).

Even better than using a delay would probably be to add a suitable wait_for_trigger or wait_template that explicitly waits until the sensor.front_door_lock_alarm_level value has been updated.

Thank you, I’ll explor the wait_template and see if that’s a better option for me.

For now I’ve found I can modify my trigger to include a milliseconds delay:

  - trigger:
      - trigger: state
        entity_id: sensor.front_door_lock_alarm_type
        to:
          - "18"
          - "19"
        for:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 20