Hello, I am trying to change the name color of a custom button dependent of time.
Here is the code, but it doesn’t work and I’m wondering what I have wrong? It’s the first if statement part that doesn’t work (everything else is good)
It just blanks out the button I had completely and throws this error:
ButtonCardJSTemplateError: ReferenceError: now is not defined in ‘if(now().timestamp() | timestamp_custom(’%H%M’) > 1900){ if(states[‘climate.home’].attributes…’
Sorry, I didn’t really look at the code after seeing the missing “)”…
What you’re doing will not work, as you’re mixing Javascript/Jinja2 code with HA template code. “Now()” is a function, that is only available in HA templates, it is not definied in Javascript.
If you want to use now(), you need to set this up in JS first.
Like I said, no surprise as that’s Jinja, not JavaScript And that’s what the custom-button-card uses for it’s templates. So think you need to do something with a Date-object.
septillion - that code gives me the error of: “ButtonCardJSTemplateError: SyntaxError: Octal literals are not allowed in strict mode. in 'var n = new Date(); var s = new Date(); s.setHours(19,00,0); if(n > s){ if(states[‘clima…’”
septillion - that did it! The problem now is that it doesn’t update without a manual refresh (of the app or webpage). Once I refresh, it does execute correctly and change the color. Any way to have it auto-refresh?
Maybe if you indeed use the a Time & date sensor instead of the Date-object the card may see the update in de sensor and thus refresh. Maybe it even refreshes the template if you just call it. But don’t know when the card will trigger the evaluation of the template.
Ok, I appreciate everyone’s inputs. I’ll have to read up on the Time & date sensor Tom posted above - admittedly, all of this is somewhat over my head and I just clunk my way until I find something that works I never have tried creating my own custom sensor, but I’ll post back if I figure it out, or if any of you could offer a short example/explanation for my specific case.
So I watched it for a few minutes with the code septillion posted and even though it doesn’t update at the exact time, it does update after the app is in the background for a few min and I open it back up.
For my purposes, I’ll call this a win as I’m not staring at my app all the time. All I wanted it to do, ultimately, is to change the label color to show when my HVAC is running - before a certain time, one button (the downstairs sensor) will highlight and at night, I have 2 other buttons (the 2 upstairs sensors) running my ecobee and highlighting those. thank you all, again.