button-card
button-card copied to clipboard
Issue with IndexOf for a string with two words
Checklist
- [x ] I updated the card to the latest version available
- [ x] I cleared the cache of my browser
Describe the bug The state of the entity is "Peu nuageux"
I am filtering the label based on the state:
label: |
[[[
var valeur = states[entity.entity_id].state.indexOf("nuageux")
return valeur;
]]]
In this case valeur = 4
If I use the following filter
label: |
[[[
var valeur = states[entity.entity_id].state.indexOf("nuageux")
return valeur;
]]]
In this case, valeur seems to be empty. Nothing is shown in the label.
If I use the indexof javascript, valeur = 0 Is this normal ?
Version of the card Version: last one V4.1.1
To Reproduce This is the configuration I used:
Screenshots If applicable, add screenshots to help explain your problem.
Expected behavior I should have valeur = 0
Desktop (please complete the following information):
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
Smartphone (please complete the following information):
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
Additional context Add any other context about the problem here.
The state value is not localized in French, it's just displayed in French,you can check that in the developer tools. So it should be indexOf("cloudy").
Thanks for the reply, but I don't understand it.
For me it's not a matter of localization. The state is provided in french, because the weather report provides a number for the weather condition and I translate this number in a french sentence. In this case, the weather condition is 1 and the sentence is 'peu nuageux'. See https://api.meteo-concept.com/api/forecast
Then I display the sentence in the label. The value of the state is in french.
Even in French, if you search for the word 'nuageux', indexof provides the result of 4 which is correct. The issue is to search for a key word with two words.
Have you tried to reproduce it ? Thanks for your help.
Try this instead of returning the result directly:
return `${valeur}`;
Hello
It works (the correct icon is shown) if I use it in the card
- type: custom:button-card template: bouton_meteo entity: sensor.meteoconcepthourlyforecast_h0_meteo name: Heure icon: | [[[ var valeur = states[entity.entity_id].state.indexOf("Peu nuageux")*2 if (
${valeur}` == 0) return 'mdi:weather-partly-cloudy';
]]]
` It doesn't work (no Icon is displayed) if I use it in a template
bouton_meteo: template: bouton show_label: true show_last_changed: false icon: | [[[ if (states[entity.entity_id].state.indexOf("Soleil") != -1) {return 'mdi:weather-sunny';} else if (states[entity.entity_id].state.indexOf("nuageux") != -1) {return 'mdi:weather-cloudy';} else if (states[entity.entity_id].state.indexOf("Nuageux") != -1) {return 'mdi:weather-cloudy';} else if (
${states[entity.entity_id].state.indexOf("Peu nuageux")}== 0) {return 'mdi:weather-partly-cloudy';} else if (states[entity.entity_id].state.indexOf("Couvert") != -1) {return 'mdi:weather-cloudy';} else if (states[entity.entity_id].state.indexOf("Brouillard") != -1) {return 'mdi:weather-frog';} else if (states[entity.entity_id].state.indexOf("Pluie") != -1) {return 'mdi:weather-rain';} else if (states[entity.entity_id].state.indexOf("Bruine") != -1) {return 'mdi:weather-rain';} else if (states[entity.entity_id].state.indexOf("Orage") != -1) {return 'mdi:weather-rain';} else if (states[entity.entity_id].state.indexOf("Averse") != -1) {return 'mdi:weather-snow';} {return 'mdi:alert';} ]]]
What is the difference between the instruction
return ${valeur}
;
and that one return valeur;
Thanks and regard