[JavaScript] Why isn't this "switch case" statement working?

I have this switch case statement:

  var selectedSpeaker = items['Lautsprecher']
  var AudioSink;
  logger.info("Lautsprecher ={}", selectedSpeaker);
  logger.info("AudioSink ={}", AudioSink);
  switch  (selectedSpeaker) {
    case "Kueche":
      AudioSink = "sonos:PLAY1:RINCON_B8E937E0C16201400";
      break;
    case "Wohnzimmer":
      AudioSink = "sonos:PLAY1:RINCON_B8E937BDEF0E01400";
      break;
    case "Buero":
      AudioSink = "sonos:PLAY1:RINCON_949F3E7D2EF401400";
      break;
    case "Gaestezimmer":
      AudioSink = "sonos:PLAY1:RINCON_B8E937E0C14201400";
      break;
    default:
      AudioSink = "sonos:PLAY1:RINCON_B8E937BDEF0E01400";
    }
  logger.info("Lautsprecher ={}", selectedSpeaker);
  logger.info("AudioSink ={}", AudioSink);

The log shows:
[INFO ] [org.openhab.rule.SagEtwas ] - Lautsprecher =Buero
[INFO ] [org.openhab.rule.SagEtwas ] - AudioSink =undefined
[INFO ] [org.openhab.rule.SagEtwas ] - Lautsprecher =Buero
[INFO ] [org.openhab.rule.SagEtwas ] - AudioSink =sonos:PLAY1:RINCON_B8E937BDEF0E01400
So the default was used (which is equal to Wohnzimmer)!
The item Lautsprecher has the Metadata stateDescriptions- Options set to
Wohnzimmer
Kueche
Buero
Gaestezimmer
from which Buero was selected.
Is this the case where the strict comparison (===) in the switch case statement doesn’t find a match? If yes, of what variable type is the item metadata?

Hi,

try adding .toString() in your switch statement :slight_smile:

Good hint!
Thanks!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.