Packages lastpage and refcount
Package refcount helps to get the page number from the reference LastPage:
\documentclass[a5paper]{article}
\usepackage{lastpage}
\usepackage{hyperref}
\usepackage{refcount}
\usepackage{lipsum}
\newcommand*{\thesecondlastpage}{%
\the\numexpr(\getrefbykeydefault{LastPage}{page}{0})-1\relax
}
\AtBeginDocument{\refused{LastPage}}
\begin{document}
The second last page is \thesecondlastpage.
\lipsum
\end{document}
This produces three pages, the first run reports -1 as second last page, because LastPage is not yet available. The final run reports 2, because the document has three pages.
[Caveat: Does not work like this for version 2023-07-24 v2.0c of the lastpage package. Update to version 2023-10-06 v2.0d of the lastpage package and everything works again.]
Package zref-lastpage
Modul zref-lastpage uses the zref referencing system to set a reference LastPage at the last page. This can also be used to get the page number of the last page to calculate the previous one:
\documentclass[a5paper]{article}
\usepackage{hyperref}
\usepackage{zref-lastpage}
\usepackage{lipsum}
\makeatletter
\newcommand*{\thesecondlastpage}{%
\the\numexpr(\zref@extractdefault{LastPage}{page}{0})-1\relax
}
\AtBeginDocument{\zref@refused{LastPage}}
\makeatother
\begin{document}
The second last page is \thesecondlastpage.
\lipsum
\end{document}
Package zref-totpages
Modul zref-totpages of project zref provides \ztotpages as absolute number of pages. Depending on the document numbering, it can also be used to get the second last page:
\documentclass[a5paper]{article}
\usepackage{hyperref}
\usepackage{zref-totpages}
\usepackage{lipsum}
\newcommand*{\thesecondlastpage}{%
\the\numexpr(\ztotpages)-1\relax
}
\begin{document}
The second last page is \thesecondlastpage.
\lipsum
\end{document}