How to get something to execute on context entry

I have been looking at his problem for too long and am sure I am missing the obvious answer.

(Asterisk 11.4 on Fedora)

I want to set some CDR variables with info about all outgoing calls.

in sip.conf
[device]
context = context1

in extensions.conf

[context1]
exten => s,1,SET(CDR(var1)=localdial)
exten => s,n,SET(CDR(var2)=device1)
exten => 800,1,Dial(This extension)
include anothercontext

[anothercontext]
exten => s,1,SET(CDR(var1)=pstndial)
exten => _XXXXXXX, 1, Dial(SIP/${EXTEN}@blabla)

The objective is when [Device] dials a number the CDR value is set correctly so it must set Var1 to localdial as it traverses Context1, but then change Var1 to pstndial when (and if) it has to go there to dial out. var2 would be set and remain set to identify the calling device.

What is the recommended way to do this? All suggestions appreciated. Thank you.

Use _X. rather than s, in the first context, but a subroutine may be better here, as well.

The easiest way for the second one is probably to call a subroutine.

This is not a support forum.