YAML use variables in page calls

Hi guys,

short question…

is there a way to load a page with the name of a string item?
this is the yaml of my label-card but I want to load different pages based on an itemstate…

component: oh-label-card
config:
  action: navigate
  actionPage: page:TV
  actionPageTransition: f7-fade
  fontSize: "-6"
  title: Harmony
  actionItem: HQHub_CurrentActivity
  item: HQHub_CurrentActivity

i tried it the following ways but without luck :neutral_face:

actionPage: page:[items[HQHub_CurrentActivity].state
actionPage: page:=[items[HQHub_CurrentActivity].state
actionPage: page:[=[items[HQHub_CurrentActivity].state]

I’m not really familar with the syntax of yaml and don’t even know if that would be possible this way…

thanks

To construct a string, the entire value must be an expression (that is, it must start with the =) and then you use string concatenation to build the page reference you want. In this case, you also need to put quotes around the entire expression so that the interpreter doesn’t get confused by the colon. The following should work:

actionPage: '="page:" + items[HQHub_CurrentActivity].state'

sorry that’s not working is there a typo maybe??
I will try some other ways following this scheme…

but in fact it tries to load something but I get an empty page with a “back arrow”…

edit:
this is silly… the web ui lets me enter your code, but after saving and reopen it looks like this

actionPage: ="page:" + items[HQHub_CurrentActivity].state

without the quotes

That’s OK. This is a known hiccup with the webui. With the colon inside the first quotes it’s not a huge deal, the expression will work anyway.

OH doesn’t do any validation on the page names. If you load a blank page this just means that the page you requested doesn’t exist. Double check the value of your item and make sure that it has the exact correct value to identify the page you are looking for.

Hmm there must be another problem I double checked that before

as you can see the page exist with the right ID and name
and it is not empty…

thanks for your help i appreciate that :slight_smile:

edit:


this is the actual yaml

I have to check the item states…

Sorry, there is one problem with the command which I didn’t catch when I copied one of your initial attempts. The reference to the items dict is wrong, so the expression is not getting the correct state. When you lookup a key in the items dict you can either use a direct call

items.key.state

or an indirect call

items["key"].state

The difference is that the bracketed version allows you to use string variables that hold the name of the key you are looking up. In this case you have [HQHub_CurrentActivity] so the expression is trying to find the value of the variable HQHub_CurrentActivity which doesn’t exist. That is the actual string you want to use. So either you need to use:

items.HQHub_CurrentActivity.state

or

items["HQHub_CurrentActivity"].state
2 Likes

Wow! It’s working :slight_smile: :slight_smile: :slight_smile:

thanks a lot for your explaination on that…you made my day!

I was searching for other errors but couldn’t find any …my frustration level was rising and rising :confounded:
until now :sweat_smile: …it’s a good feeling if something is working in the long run…