Response from /get or /set

Is it possible to retrieve the zigbee response from an mqtt publish /get or /set command? I can see the response by watching the z2m logs, but I don’t see an obvious way to retrieve this for use programmatically.

My particular use case is trying to retrieve a lock code from a door lock. My script code works to retrieve the pin code, but again I have to watch the z2m log files, whereas I would like to see it end up in a variable that can be used in the script.

I apologize if this has been covered previously, but I was unable to find anything relevant in search results.

Use a MQTT trigger, example:

  - platform: mqtt
    topic: sensor/ikea_switch2_sensor