custom-cards / button-card

❇️ Lovelace button-card for home assistant
MIT License
1.89k stars 232 forks source link

Issue with IndexOf for a string with two words #758

Open Merangle opened 1 year ago

Merangle commented 1 year ago

Checklist

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):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

RomRider commented 1 year ago

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").

Merangle commented 1 year ago

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.

RomRider commented 1 year ago

Try this instead of returning the result directly:

return `${valeur}`;
Merangle commented 1 year ago

Hello

It works (the correct icon is shown) if I use it in the card `

` 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