This is probably the “hackiest” solution to the lack of a navigate back button, but it does work, is browser agnostic (I think), works on the companion apps and is easy to use once you are set up.
I’m sure there are a bunch of problems with it, but hey ho, none of the other solutions I found was quite right for me. My main requirement is because my wall tablet doesn’t have the header shown and I wanted a way to get back to the previous page when there were several paths to a sub-page. It’s a bit esoteric so I can understand why it doesn’t get much main-stream traction. Like others I also considered using the mushroom back chip, but that didn’t style in with the section tile buttons I’m using all over the place.
How do you get it set up?
Basically, you create a file called nav_back_helper.js and dump it in your /config/www/scripts folder on your HA box.
It should contain this code:
window.hassConnection.then(() => {
let goBackTwice = false;
window.addEventListener("popstate", () => {
// If the first back just occurred, do the second now
if (goBackTwice) {
goBackTwice = false;
history.back();
}
});
setInterval(() => {
if (location.href.endsWith("#BACK")) {
// Remove the #BACK entry from history
history.replaceState(null, "", location.href.replace("#BACK", ""));
// Trigger the first back; the popstate listener will trigger the second one
goBackTwice = true;
history.back();
}
}, 100);
});
console.info(
`%cNAV-BACK-HELPER\n%cVersion: 0.0.2`,
"color: green; font-weight: bold;",
""
);
Then you “Edit your dashboard”, click the “three-dots” menu and click “Manage Resources”.
On this page, click the “+ Add resource” button and put the URL to /local/scripts/nav_back_helper.js?v=0.0.2 and ensure you have selected “JavaScript module”, then click “Create”
Now, refresh your Lovelace page and you’re ready to go.
You can use any UI element to trigger it … I use a Tile card, set the action to “Navigate” and type #BACK into the navigation path
type: tile
entity: input_button.navigate_placeholder
name: Back
icon: mdi:arrow-left-bold
hide_state: true
vertical: false
tap_action:
action: navigate
navigation_path: "#BACK"
icon_tap_action:
action: none
features_position: bottom
Click on the button and watch the browser navigate back to the previous page.
How does it work?
The navigation path puts #BACK onto the existing URL rather than trying to load a full new content from the HA server. The piece of JavaScript that you installed is watching the current location 10 times a second (you can faff with this if you want). If it notices that there is #BACK on the href, it will go back twice in the history, once to skip the navigation to “#BACK” and the second to actually go back to your previous page.
Vile isn’t it? 