Error with SPLIT in Label/color setting

I’d like to set an item’s color depending on its value. Since it’s not a straight number but contains text (e.g. 930 ppm) I need the split function, which usually works fine. In the developer sidebar =items.NetatmoEsszimmer_CO2Gehalt.state.split(’ ')[0] gets a number, too.
For some strange reason, I get error messages in the context of ‘color’. Do you have any explanation for this?

          - component: Label
            config:
              text: =(items.NetatmoEsszimmer_CO2Gehalt.state.split(' ')[0])
              style:
                margin-top: 0px
                left: 0px
                font-size: 24px
                color: '=(Math.round(((items.NetatmoEsszimmer_CO2Gehalt.state.split(' ')[0])) < "900") ? "green" :
                  (Math.round((items.NetatmoEsszimmer_CO2Gehalt.state.split(' ')[0])) > "1200") ?
                  "red" : "green"'

Try this:

  color: "=(Math.round(((items.NetatmoEsszimmer_CO2Gehalt.state.split(' ')[0])) < '900') ? 'green' :
                  (Math.round((items.NetatmoEsszimmer_CO2Gehalt.state.split(' ')[0])) > '1200') ?
                  'red' : 'green'"

or this:

color: '=(Math.round(((items.NetatmoEsszimmer_CO2Gehalt.state.split(\' \')[0])) < "900") ? "green" :
                  (Math.round((items.NetatmoEsszimmer_CO2Gehalt.state.split(\' \')[0])) > "1200") ?
                  "red" : "green"'

i believe it is related to this:

Hello Jan,
I’ve tried both versions without success; no errors in the code-window, but the results get ignored. And when I test it in the dev. sidebar, I get errors:

color: ‘=(Math.round(((items.NetatmoEsszimmer_CO2Gehalt.state.split(’ ‘)[0])) < “900”) ? “green” : (Math.round((items.NetatmoEsszimmer_CO2Gehalt.state.split(’ ‘)[0])) > “1200”) ? “red” : “green”’
→ Run-Mode: result igrnored, standard color - Developer Sidebar: Error: Expected comma at character 59

color: “=(Math.round(((items.NetatmoEsszimmer_CO2Gehalt.state.split(’ ‘)[0])) < ‘900’) ? ‘green’ : (Math.round((items.NetatmoEsszimmer_CO2Gehalt.state.split(’ ')[0])) > ‘1200’) ? ‘red’ : ‘green’”
→ Run-Mode: result ignored, standard color - Developer Sidebar: Error: Unclosed ( at character 185

I’ll look deeper in the post by Rainer and check back here in case I’m succuessful. Might be interesting for others since I’d expect this to be a rather common question/problem …

I just remembered to have a CO2 widget as test. for the color i used this:

'=(Number.parseFloat(items[props.itemCo2].state.split(" ")[0]) > 700) ? "red" : "green"'

you need to replace (’ ') by (" ")…

1 Like

Thanks! So obvious in retrospect, after all I used " for “red” and the like as well …

i slashed my head for a second when i remembered to have done such a widget but did not remember :slight_smile:

too obvious indeed…