New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trigger refresh based on time #436
Comments
I understand the use case, but let me ask you why not use However, until a feature like this makes it officially into the card, there's a way to hack it to refresh on an interval. I'll put an example later, as I'm on my phone right now. 😊 |
Nice, I had actually not seen that one 🙈 Thanks for pointing that out!
So natively would still be the perfect way in this case, but I'll just go ahead and create a time sensor if you don't feel this is worthwile adding. It just felt like this could be something that others would be interested in as well. Anyway, waiting for your hack example as well 👍🙇 |
That should work (it's a bit hacky, but who cares 😊 ): - type: 'custom:button-card'
entity: switch.biltak_motorvarmare
name: Motorvärmare
icon: 'mdi:car'
show_state: true
state_display: |
[[[
if (entity.state === 'on') {
if (this._myTimer === undefined) {
this._myTimer = window.setInterval(() => { this.update() }, 1000 * 10) // That value 10*1000 is 10000 ms = 10 sec
}
function humanReadableDuration(duration) {
var hours = Math.floor(duration / 1000 / 60 / 60);
var minutes = Math.floor((duration / 1000 / 60 / 60 - hours) * 60);
var result = '';
if (hours > 0) {
result += hours + ' h ';
}
return result + minutes + ' min';
}
var timeDiff = new Date().getTime() - new Date(entity.last_changed).getTime();
return 'Stäng av (på i ' + humanReadableDuration(Math.abs(timeDiff)) + ')';
}
else {
if (this._myTimer !== undefined) {
window.clearInterval(this._myTimer);
delete this._myTimer;
}
return 'Lägg på';
}
]]] Edit: some code update |
Nice, thanks a lot! I'll definitely try this one out. Will this work when defined in a button card template as well? 🤔 I mean, |
It refers to the button-card DOM element, it's unrelated to templates, you can put that anywhere ;) |
Works very well, awesome. I'll live with this until or if the native support is ever added 👍🙇 |
+1 |
Feel free to dismiss this if you feel it's out of scope for this custom card.
Is your feature request related to a problem? Please describe.
I have a button card for a switch entity where I want to show how long the switch has been on in the
state_display
. Showing the time was not hard, but getting it to refresh itself regularly isn't as easy. This is what I have at the moment:Describe the solution you'd like
Native support for refreshing a card regularly. I would like to e.g. trigger a refresh every 10th second.
I'm intentionally not getting into suggestion how it should be configured here.
Describe alternatives you've considered
It should be possible to create some kind of time entity myself, and add it to
triggers_update
, but for example the built-in Time & Date sensor does not support entities that updates more often than once a minute.The text was updated successfully, but these errors were encountered: