hi,
I am trying to compare value of a DateTime item with current time in a widget. I found that there is a DayJS tool to work with dates, but it doesn’t really work for me. I think I do something wrong. I would really appreciate your support in this.
text: "=items[props.updated].state" prints out “2023-10-29T14:54:20.938759875+0100” or text: "=items[props.updated].displayState" prints out “2023-10-29 14:55”
so the item has value, but when I try to compare that with current time, I get “undefined”
text: "=dayjs(items[props.updated].displayState).isAfter(dayjs(new Date())) ? after : before"
I don’t insist on daysJS, but I also failed with simple JS.
dayjs may be able to handle the formatted display state, but I’m not sure. However, there’s no reason to use it, because the state will be sufficient.
The widget expressions do not support the new objectType format. Fortunately, you don’t need it a simple call of dayjs() returns a date-time of now.
Your expression has to return strings, but because you are working in an expression, chunks not enclosed in quotes are interpreted as variables. Even if you ternanry expression worked, it would return the variable after or before, which are, of course, undefined.
Dear @JustinG,
Thank you for your help. I am trying to improve this code to fine tune my widget. I am trying to check if last update an item is before now minus 1hour or not. As I can see, based on the documentation my code should work, but doesn’t. Do you have any idea what I do wrong?
Following visibility check doesn’t work for me visible: "=dayjs(items[props.updated].state).isBefore(dayjs().subtract(1, 'hour')) ? true : false"
You don’t need the extra ternary statement part. That syntax means (test) ? (result if true) : (result if false) so it is redundant to put (test) ? (true if true) : (false if false). You should just be able to use:
The syntax you have would also work except that the yaml property is ultimately expecting a string and true and false. So you would use (test) ? 'true' : 'false' but again that’s just not necessary. You only need to add those if you want some other strings based on the true or false values such as (test) ? 'green' : 'red' to set a color property based on the result of the test.