Background colour based on item state

  • openHABian 3.3.0 on rPi4 with 4GB

I am trying to change the background colour based on the item state.
The item state can have 8 states; however, I can use the one state to catch the colour for them all. Basically, if the item state (which is numeric) is 7, show standard background colour, anythign else show orange-ish.

I use a template widget.

<div ng-style="{
    'background-color': itemValue('spm_AC_Source_Status')=='7' ? '#223344' : '#ff8000'
  <div class="template-contents">Grid</div>

However, it does not change the colour as expected. In fact, it is always orange.

Also, what formatting should a well-formed template take?
Should it look like what I posted above?

Any hints appreciated.

Should this be



Reading the doc says:

itemValue(name) - gets the value (state) of an item

I have another template w/o state which works.

Hmm… Maybe without the ’ at


Because number, not string?

Yep, tried that too… no difference. :frowning:

The item exists and has the value 7:

The item state is a string, so should you not be using “7”

itemValue('spm_AC_Source_Status') === "7"

Hmm, why three equal signs?
Why double quotes?

Adopting your suggestion, actually makes the the background blueish, but does not change when I change the item state to 6.

I have this working:

<div ng-style="{
    'background-color': itemValue('Shed_SLO_Door_External')=='OPEN' ? '#006622' : '#223344'
  <div class="template-contents">Media room external door
  <widget-icon iconset="'eclipse-smarthome-classic'" icon="'door'"
    state="itemValue('Shed_SLO_Door_External')" size="80" center="true" />

Where my current template is a derivative from. I am puzzled.

From my understanding you could use == as well - === is a more strict comparison
“7” because 7 is a string

Well, well…

While the item receives numbers, a map() changes it to strings, like AC Source is in tolerance. Hence, after changing the second line to:

    'background-color': itemValue('spm_AC_Source_Status')=='AC Source is in tolerance' ? '#223344' : '#ff8000'

The b!oody thing works :slight_smile:

I apologise for ‘forgetting’ about this minor, yet very effective, oversight.

Thank you for wrecking your brains on this; at least I did.