Widget background color 'negative values'

I’m have a widget that show my electricity consumption. This goes between fe -5000 to +5000. Consumption vs solar production.

I would like to have a background as indication. But It seems that the widget isn’t taking into account if the value is positive or negative? Any suggestion?

 background: '= 
      (items.fluksoElecA.state > 4000) ? "red" :
      (items.fluksoElecA.state > 1500) ? "lightyellow" : 
      (items.fluksoElecA.state > 1) ? "lightblue" : 
      (items.fluksoElecA.state < 1) ? "green" : 
      "" '

The item:

Number fluksoElecA   "Verbruik Algemeen [%s W]" 				<watt>	(gNuts, energieAlgemeen)	{ channel="mqtt:topic:snuffel:snuffel:eleca" }

Current result, should be green in my eyes:

Schermafdruk van 2022-04-28 10.00.00

items.X.state always returns a string, even if the item is some form of number type. In order to get a numerical comparison in widget expressions, you need to convert the state string value to a numerical value using Number(items.X.state).

Just tried it, but seems not working for me. He still ignores the negative numbers.

config:
  background: '= 
    Number(items.fluksoElecA.state) > 4000 ? "red" :
    Number(items.fluksoElecA.state) > 2000 ? "lightyellow" :
    Number(items.fluksoElecA.state) > 0 ? "" : 
    Number(items.fluksoElecA.state) < 0 ? "lightgreen" : 
    ""'

Sorry, I missed that your item state had a unit. In that case, Number(items.X.state) will fail but Number.parseInt(items.X.state) will correctly extract the number portion of the state.

3 Likes

Works almost great. Currently:

  background: '=
    Number.parseInt(items.pvVerschil.state) > 2000 ? "indianred" :
    Number.parseInt(items.pvVerschil.state) > 500 ? "mistyrose" :
    Number.parseInt(items.pvVerschil.state) > 0 ? "seashell" :
    Number.parseInt(items.pvVerschil.state) < 0 ? "lightyellow" :
    Number.parseInt(items.pvVerschil.state) < -1000 ? "darkseagreen" : 
    ""'

Schermafdruk van 2022-04-29 10.53.32

Just the number below -1000 doesn’t react? Tried with “-1000”, but same result.
Schermafdruk van 2022-04-29 10.56.29

The problem here is the logic of your expression, not the syntax. -1000 is already less than 0 so it triggers that ternary expression first and returns “lightyellow”. It never even gets to the the less than -1000 test.

Either change the < 0 to `> -1000’ or just swap the order of the last two tests so that the more-restrictive less than -1000 comes before the less-restrictive less than 0.

1 Like

lol, when you’re just so focused on that one detail, and don’t see the total.
As you said, works great when it’s in the right order:

 background: '= 
    Number.parseInt(items.pvVerschil.state) > 2000 ? "indianred" :
    Number.parseInt(items.pvVerschil.state) > 500 ? "mistyrose" :
    Number.parseInt(items.pvVerschil.state) > 0 ? "seashell" :
    Number.parseInt(items.pvVerschil.state) < -1000 ? "darkseagreen" :
    Number.parseInt(items.pvVerschil.state) < 0 ? "lightyellow" : 
    "gray"'