In case of Moonraker cooperation (instead of Octoprint), it is possible to make something like:
[menu __main __poweroff]
type: command
enable: {printer.idle_timeout.state != "Printing"}
name: Turn off Pi
gcode:
{action_call_remote_method("shutdown_machine")}
Because Moonraker registers remote methods “shutdown_machine” and “reboot_machine”.
It works, although I am not able to replace content of display by e.g. “Shutdown” (original menu stays there).