Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Created April 26, 2020 20:47
Show Gist options
  • Save Mikulas/a02d3c2032fceb793639c03799f296d6 to your computer and use it in GitHub Desktop.
Save Mikulas/a02d3c2032fceb793639c03799f296d6 to your computer and use it in GitHub Desktop.
Home Assistant Lovelace Countdown for Alexa Timer
class CountdownCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
const card = document.createElement('ha-card');
card.header = 'Timer';
this.content = document.createElement('div');
this.content.style.padding = '0 16px 16px';
card.appendChild(this.content);
this.appendChild(card);
}
const entityId = this.config.entity;
const state = hass.states[entityId];
this.stateStr = state ? state.state : 'unavailable';
if (state) {
this.rAF_ID = requestAnimationFrame(this.render.bind(this));
}
}
render(callback) {
if (this.stateStr === 'unavailable') {
this.content.innerHTML = 'no timer';
return;
}
const totalSeconds = (new Date(this.stateStr) - new Date()) / 1000;
const hours = Math.floor(totalSeconds / 3600) + "";
const minutes = Math.floor(totalSeconds / 60) % 60 + "";
const seconds = Math.floor(totalSeconds % 60) + "";
const relative = hours.padStart(2, '0') + ':' + minutes.padStart(2, '0') + ':' + seconds.padStart(2, '0');
this.content.innerHTML = `<h2>${relative}</h2>`;
this.rAF_ID = requestAnimationFrame(this.render.bind(this));
}
setConfig(config) {
if (!config.entity) {
throw new Error('You need to define an entity');
}
this.config = config;
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return 2;
}
}
customElements.define('countdown-card', CountdownCard);
@conorlap
Copy link

Looks v3.10.6 of Alexa Media Player has fixed the issue, there was a problem with Timezone's I believe. (Use timezone aware datetime for timers (alandtse/alexa_media_player@de4a962)

@jazzmonger
Copy link

Any chance this could be adapted to multiple timers? We have up to 3 going at any one time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment