defineVars

How can I defineVars with a calculated value something like this (on a page):

config:
  label: test
  sidebar: true
  defineVars:
    Var1: =dayjs()
    Var2: '=dayjs()'
    Var3: ='dayjs()'
    Var4: 'dayjs()'

If I use it in a widget I do not get the value, just the text:

...
- component: oh-label-card
      config:
        title: =vars
...

gives me:
{
“Var1”: “=dayjs()”,
“Var2”: “=dayjs()”,
“Var3”: “=‘dayjs()’”,
“Var4”: “dayjs()”
}

What I excect is: “VarXYZ”: “2024-11-11T01:01”

Is that possible?

Thanks

that‘s the correct one. But you need to provide an argument like

dayjs(@'myDateTimeItem').format("HH:mm")

What do you want to achieve?

I have used dayjs in the footer of a cell:

=dayjs(items["Door_laundry_lastupdate"].state).fromNow()

I have not used it in a widget though.

Ahh, I see. But does’n give dayjs() the current date ans time?

I want to have a variable for a daytimepicker filled with the current day (and another one with 1 week into the past) to call a grafana graph.

How do I get the current day than?

for the unformatted current time you can use this:

dayjs().format()

date for next week:

dayjs().add(7, "days").format()

In order to format datetime to your need, just add a formatting string into the brackets as posted above.

sadly none work

Have you seen this grafana thread?

I don’t believe that page vars are passed through the expression parser, so you can’t use expressions in the page variables. If you need a dynamic page variable you have a few other options.

If you are calling this page from a link or widget that you have created, then you can use properties of the calling component to pass variables into the page and those properties will be evaluated by the expression parser.

The other option, which will depend on the structure of the page and where you need this variable is to just wrap the widget the needs the variable in an oh-context component.

- component: oh-grid-col
  config: {}
  slots:
    default:
      - component: oh-context
        config:
          variables:
            test: ="This " + "should work"
        slots:
          default:
            - component: oh-label-card
              config:
                label: =vars.test

This means the variable will only be available to the descendant nodes of the context, so if the use of the variable is very wide-spread this will be more difficult.

@ubeaut: I was inspired by this or something very similar. But I don’t want preset ranges rather individual from and to date/times.
@JustinG: Very good, so I only need to nest everything in a oh-context. I only use it in my widget so it works.

Thanks @all