home-assistant/core · warning · ValueError

Command not found: {cmd!r}

Error message

Command not found: {cmd!r}

What it means

ValueError(f"Command not found: {cmd!r}") from _extract_codes: the command name was not present in self._codes[device], the learned-code dictionary for the specified subdevice. A device was supplied and the dict lookup raised KeyError, which is converted to this explicit error. Nothing is transmitted.

Source

Thrown at homeassistant/components/broadlink/remote.py:154

        device as keys.

        The codes are returned in sublists. For toggle commands, the
        sublist contains two codes that must be sent alternately with
        each call.
        """
        code_list = []
        for cmd in commands:
            if cmd.startswith("b64:"):
                codes = [cmd[4:]]

            else:
                if device is None:
                    raise ValueError("You need to specify a device")

                try:
                    codes = self._codes[device][cmd]
                except KeyError as err:
                    raise ValueError(f"Command not found: {cmd!r}") from err

                if isinstance(codes, list):
                    codes = codes[:]
                else:
                    codes = [codes]

            for idx, code in enumerate(codes):
                try:
                    codes[idx] = data_packet(code)
                except ValueError as err:
                    raise ValueError(f"Invalid code: {code!r}") from err

            code_list.append(codes)
        return code_list

    @callback
    def _get_codes(self):
        """Return a dictionary of codes."""

View on GitHub (pinned to 58a3fdb3ea)

Solutions

  1. Run remote.learn_command to (re)learn the exact command name for that subdevice.
  2. Check spelling and case — lookup is an exact dict key match.
  3. Confirm the command was learned under the same device/subdevice name you pass in the call.
  4. If codes vanished, restore HA .storage or re-learn all commands for that subdevice.

Example fix

# before
data:
  device: tv
  command: powr   # typo

# after
data:
  device: tv
  command: Power   # exact learned name
Defensive patterns

Strategy: validation

Validate before calling

def known_commands(remote_entity) -> set[str]:
    """Return learned command names for exact-match checking before sending."""
    codes = remote_entity.broadlink_codes  # or read the integration's stored codes
    return set(codes.get(subdevice, {}))

if command not in known_commands(remote):
    raise ValueError(f"Command not learned: {command!r} — run remote.learn_command first")

Type guard

def is_known_command(command: object, codes: dict) -> bool:
    """Narrow to commands present in the subdevice's learned set."""
    return isinstance(command, str) and command in codes

Try / catch

try:
    await hass.services.async_call("remote", "send_command", data, blocking=True)
except ValueError as err:
    if "Command not found" in str(err):
        await hass.services.async_call(
            "remote", "learn_command",
            {**data, "command": [missing_cmd]}, blocking=True,
        )
    else:
        raise

Prevention

When it happens

Trigger: Sending remote.send_command with device set correctly but a command name never learned (or learned under a different subdevice, or with different capitalization/spelling).

Common situations: Typo in the command name in an automation, command learned under a different subdevice name, storage file lost/reset (HA database or .storage wipe), case mismatch ('power' vs 'Power').

Related errors


AI-assisted analysis of home-assistant/core@58a3fdb3ea (2026-08-14). Data as JSON: /api/errors/7c4d1ba1cf610417. Report an issue: GitHub.