Adding a delay to prevent "Unsafe Shutdowns" for nvme drive

Looking at the S.M.A.R.T data for my nvme drive I notice that the “Unsafe Shutdowns” counter gets incremented with each power cycle.
I was wondering if there’s a way of adding a delay to the shutdown process to wait a few second post halt before powering off?

We just had a conversation on MNT Reform on libera.chat IRC that touched this subject. Sigrid agreed that I copypaste what she said to this post:

Maybe this is also interesting for @nanocodebug as the maintainer of the reform lpc module.

Currently, we use this systemd hack to turn off the unit: Files · main · Reform / MNT Reform Tools · GitLab

The lpc module supports power off commands over SPI as part of the changes I added. Serial command “p 1” will invoke the same power off that xPWR0 triggers.

  if (spi_command == 'p') {
    // toggle system power and/or reset imx
    if (spi_arg1 == 1) {
      turn_som_power_off();
    }
    if (spi_arg1 == 2) {
      turn_som_power_on();
    }
    if (spi_arg1 == 3) {
      reset_som();
    }
    if (spi_arg1 == 4) {
      turn_aux_power_off();
    } 
    if (spi_arg1 == 5) {
      turn_aux_power_on();
    } 

    spiBuf[0] = som_is_powered;
  }

I’m not familiar with pm_power_off, but a quick glance suggests that its another driver interface that needs to be added to the reform lpc kernel module. That would invoke the power off command over SPI on the kernel shutdown signal.
If someone knows the kernel module interface in question that would be great.

I have the nvme drive installed and in use but I’m still booting from sd card and as a workaround, instead of shutting the system down from the operating system, I issue a reboot and when the screen turns off at the early stages of boot, I issue a power off from the keyboard menu (circle button, 0).
I’ve attempted this several times and for me with the WD Black drive, the power cycles counter increments as it is expected to but the unsafe shutdowns counter has stopped incrementing in the output of smartctl -a /dev/nvme0

I have no idea if this would work if you are booting from nvme or how long you would have during that early boot part before the drive is probed and initialised (someone who is booting from nvme try & report back?)