Skip to content

Instantly share code, notes, and snippets.

@d-alleyne
Last active June 12, 2021 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-alleyne/f987a722411d78b0d54e217cfc44b520 to your computer and use it in GitHub Desktop.
Save d-alleyne/f987a722411d78b0d54e217cfc44b520 to your computer and use it in GitHub Desktop.
Convert Home Assistant Supervisor Logs from UTC to Local Time
// ==UserScript==
// @name Home Assistant Supervisor Logs UTC to local time
// @namespace https://github.com/d-alleyne/
// @version 0.3
// @description A script to convert Home Assistant Supervisor logs from UTC to local time. Now supports Chrome.
// @author Damien Alleyne
// @match http://homeassistant.local:8123/hassio/system
// @grant none
// ==/UserScript==
const convertUTCToLocal = (entry) => {
const dateRegex = /^(\d{2})\-(\d+)\-(\d+) (\d+):(\d+):(\d+)/;
let utcTime = entry.innerText.match(dateRegex);
if (utcTime?.length) {
const [_, year, month, day, hour, minute, second] = utcTime;
// + operator to convert string to number. See https://javascript.info/operators#numeric-conversion-unary
const date = new Date(
Date.UTC(+year + 2000, +month - 1, day, hour, minute, second)
);
const localDate = new Intl.DateTimeFormat("en", {
dateStyle: "medium",
timeStyle: "short",
}).format(date);
entry.innerText = entry.innerText.replace(dateRegex, localDate);
}
};
(function () {
//the JS Path for the Supervisor logs is different between Chromium-based and Gecko-based browsers
const isGecko = window.navigator.userAgent.match(/Gecko\/\d{8}/);
let counter = 0;
let mainTimer = setInterval(() => {
try {
const mainBox = isGecko
? document.querySelector("body > hassio-main")
: document
.querySelector("body > home-assistant")
.shadowRoot.querySelector("home-assistant-main")
.shadowRoot.querySelector(
"app-drawer-layout > partial-panel-resolver > ha-panel-custom > iframe"
)
.contentDocument.querySelector("body > hassio-main");
const supervisorLog = mainBox?.shadowRoot
.querySelector("hassio-router > hassio-panel")
?.shadowRoot.querySelector("hassio-panel-router > hassio-system")
?.shadowRoot.querySelector(
"hass-tabs-subpage > div > hassio-supervisor-log"
);
if (supervisorLog) {
const htmlBox = supervisorLog.shadowRoot.querySelector(
"#content > hassio-ansi-to-html"
);
const logBox = htmlBox?.shadowRoot.querySelectorAll("pre > span");
if (logBox) {
clearInterval(mainTimer);
logBox.forEach(convertUTCToLocal);
} //if (logBox)
}
} catch (err) {
// element doesn't exist yet?
console.log(err);
// we should have found the element by now
if (++counter > 50) {
clearInterval(mainTimer);
}
}
}, 300);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment