Hi all. Anyone find any bug in this code. It works perfect when I verify the data in the console. But it breaks when I want to execute hass.callService
If anyone sees something wrong please help me correct it.
Thanks a lot
The error is not clear but it only occurs when I run the service
button-card.js:425 ButtonCardJSTemplateError: SyntaxError: Unexpected identifier in 'if(entity) {
let mediasName = {"BDV-N9200W":"BDV-N9200w","Spotify":"Spotify","Gooble Mini":"go...'
at new Function (<anonymous>)
at Je._evalTemplate (button-card.js:425:9295)
at Je._getTemplateOrValue (button-card.js:425:9964)
at button-card.js:425:9857
at Array.forEach (<anonymous>)
at Je._getTemplateOrValue (button-card.js:425:9834)
at Je._objectEvalTemplate (button-card.js:425:9673)
at Je.render (button-card.js:425:6845)
at Je.update (button-card.js:1:19896)
at Je.performUpdate (button-card.js:1:16782)
Code
[[[
if(entity) {
let mediasName = {"BDV-N9200W":"BDV-N9200w","Spotify":"Spotify","Gooble Mini":"google_mini"};
let media = "";
let activesMedia = [];
let cant = 0;
let cantMedias = states['input_select.players_home'].attributes.options.length;
for(var x = 0; x < cantMedias; x++) {
media = ('media_player.' + states['input_select.players_home'].attributes.options[x]);
if(states[media] != undefined && states[media].state == 'playing')
activesMedia[cant++] = states[media];
}
let max = activesMedia.length;
let rnd = Math.floor(Math.random() * max);
entity = activesMedia[rnd];
if(activesMedia.length != 0) {
console.log(rnd);
console.log(activesMedia[rnd].attributes.friendly_name);
console.log(mediasName[activesMedia[rnd].attributes.friendly_name]);
hass.callService('input_select', 'select_option', {
entity_id: input_select.conditional_media
option: mediasName[activesMedia[rnd].attributes.friendly_name];
});
}
return rnd;
}
]]]