import { css, html, LitElement, } from "https://unpkg.com/lit-element@2.0.1/lit-element.js?module"; import dayjs from "https://unpkg.com/dayjs@1.8.24/esm/index.js?module"; import localizedFormat from "https://unpkg.com/dayjs@1.8.24/esm/plugin/localizedFormat/index.js"; import relativeTime from "https://unpkg.com/dayjs@1.8.24/esm/plugin/relativeTime/index.js"; dayjs.extend(relativeTime); dayjs.extend(localizedFormat); class NonowAge extends LitElement { static get properties() { return { hass: {}, config: {}, }; } render() { const date = dayjs(this.hass.states[this.config.entity].state); const now = dayjs(); const years = `${Math.floor(now.diff(date, "year"))}`.padStart(2, "0"); const month = `${Math.floor( now.diff(date.add(years, "year"), "month") )}`.padStart(2, "0"); const days = `${Math.floor( now.diff(date.add(years, "year").add(month, "month"), "day") )}`.padStart(2, "0"); const hours = `${Math.floor( now.diff( date.add(years, "year").add(month, "month").add(days, "day"), "hour" ) )}`.padStart(2, "0"); const minutes = `${Math.floor( now.diff( date .add(years, "year") .add(month, "month") .add(days, "day") .add(hours, "hour"), "minute" ) )}`.padStart(2, "0"); const absoluteMonth = `${Math.floor(now.diff(date, "month"))}`.padStart( 2, "0" ); const absoluteDays = `${Math.floor(now.diff(date, "day"))}`.padStart( 2, "0" ); const yearString = years > 1 ? html`
${years} ans
` : years > 0 ? html`
1 an
` : ""; const monthString = month > 0 ? html`
${month} mois
` : ""; const dayString = days > 1 ? html`
${days} jours
` : days > 0 ? html`
1 jour
` : ""; const hourString = hours > 1 ? html`
${hours} heures
` : hours > 0 ? html`
1 heure
` : ""; const minuteString = minutes > 1 ? html`
${minutes} minutes
` : minutes > 0 ? html`
1 minute
` : ""; const absoluteMonthString = years > 1 && absoluteMonth > 0 ? html`
${absoluteMonth} mois ${dayString}
` : ""; const absoluteDayString = month > 1 && absoluteDays > 1 ? html`
${absoluteDays} jours
` : absoluteDays > 0 ? html`
1 jour
` : ""; return html`
${yearString} ${monthString} ${dayString} ${hourString} ${minuteString} ${absoluteMonthString} ${absoluteDayString}
`; } setConfig(config) { const { entity, title } = config; if (!entity) { throw new Error("You must define an entity"); } this.config = { entity, title }; } static get styles() { return css` ha-card { padding: 0 6px; } .content { padding: 0 24px 24px 24px; display: flex; flex-wrap: wrap; justify-content: center; font-size: 25px; } .figure-sentence { font-weight: 300; font-size: 1em; display: flex; align-items: center; margin: 0.16em 0.32em 0.16em 0.16em; } .figure { line-height: 1em; font-size: 1.6em; font-weight: 500; margin: 0 0.16em; } .figure-absolute-content { border-top: 1px solid var(--background-color); margin-top: 16px; padding-top: 16px; width: 100%; font-size: 0.5em; display: flex; line-height: initial; align-items: flex-end; justify-content: center; } `; } } customElements.define("lovelace-nonow-age", NonowAge);