HI,
for a button config, I need to set a conditional variable in a template, hope this gives you an idea if what I am trying to establish:
label:
- color: >
[[[ var state = {if (states['sensor.trash_today'].state != 'Geen') return states['sensor.trash_today'].state;
return states['sensor.trash_tomorrow'].state;}
if (state == 'papier') return 'blue';
if (state == 'gft' ) return 'green';
if (state == 'plastic') return 'orange';
if (state == 'restafval') return 'black';
return 'ivory'; ]]]
this obviously doesn’t work, but I am not sure how to proceed. Would appreciate some JS assistance
update
as an intermediary solution, Ive found this to work, but it is less elegant because the hardcoded vars are in all templates, which I would hope to prevent using a better template…
will certainly add the mapper to the toolbox, since I have bigger mappers than this, and it is a fine way of streamlining templates with many states to map…
should be this I think (trash in mapper, not state)?:
If mapper[trash] doesn’t exist it returns undefined which is equivalent to false, which then returns the ivory value. If it’s defined, it just returns the value from the map.
Btw this is called ternary operator if you want to Google for it
yes, thanks, was aware of that, only not that it was also possible setting a variable with this operator.
was still trying to do this:
[[[ if (states['sensor.trash_today'].state != 'Geen' ) return var trash = states['sensor.trash_today'].state;
return trash = states['sensor.trash_tomorrow'].state ;]]]
which is the order I am used to… but that doesnt pass the parser
could try this maybe?:
[[[ var trash = states['sensor.trash_today'].state if (states['sensor.trash_today'].state != 'Geen' );
var trash = states['sensor.trash_tomorrow'].state ;]]]
nope that doesnt do it. this does though:
[[[ var today = states['sensor.trash_today'].state;
var tomorrow = states['sensor.trash_tomorrow'].state;
var trash;
if (today != 'Geen' ) trash = today;
trash = tomorrow;]]]
so its all about the declaring of the variable, which can also be empty and then used in the if else construction.