Script generates an error

Hi All,

I have a very simple script, that send an IR remote command.
It’s working, and the command is sent to the IR transmitter, however it generates an error:

Failed to perform the action script/lv_fan_off. 
Enum RemoteKeyCode has no value defined for name ‘KEYCODE_fan_off

The YAML code of the script is:

alias: lv_fan_off
sequence:
  - action: remote.send_command
    data:
      device: living_room_fan
      command: fan_off
    target:
      area_id: living_room
description: ""
icon: mdi:fan-off

I’ve found the following in the HA logs:

Logger: homeassistant.helpers.script.living_room_fan
Source: helpers/script.py:525
First occurred: 10:30:39 (1 occurrences)
Last logged: 10:30:39

Living Room Fan: Error executing script. Unexpected error for call_service at pos 1: Enum RemoteKeyCode has no value defined for name 'KEYCODE_fan_off'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 525, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 763, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 726, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2761, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2804, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 718, in _service_handler
    response = await self._async_start_run(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 674, in _async_start_run
    script_result = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 707, in _async_run
    return await self.script.async_run(script_vars, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1795, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 463, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 527, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 557, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 525, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 763, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 726, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2761, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2804, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1021, in entity_service_call
    raise result from None
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1610, in async_request_call
    return await coro
           ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1068, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/androidtv_remote/remote.py", line 109, in async_send_command
    self._send_key_command(single_command, "SHORT")
  File "/usr/src/homeassistant/homeassistant/components/androidtv_remote/entity.py", line 73, in _send_key_command
    self._api.send_key_command(key_code, direction)
  File "/usr/local/lib/python3.12/site-packages/androidtvremote2/androidtv_remote.py", line 395, in send_key_command
    self._remote_message_protocol.send_key_command(key_code, direction)
  File "/usr/local/lib/python3.12/site-packages/androidtvremote2/remote.py", line 106, in send_key_command
    key_code = RemoteKeyCode.Value(key_code)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/google/protobuf/internal/enum_type_wrapper.py", line 59, in Value
    raise ValueError('Enum {} has no value defined for name {!r}'.format(
ValueError: Enum RemoteKeyCode has no value defined for name 'KEYCODE_fan_off'

The same code implemented as a button on a dashboard, is working without generating the error.
Does anyone know where the error is coming from, and how to solve it?

Cheers.

Hi pda,

In the home assistant storage area, there has to be a corresponding stored code for this. It appears that the code you requested is not found. These are put in using the remote learn process and will result in entries in a JSON file somewhere similar to this for the script to interpret and send to the IR device. (It can’t find it…)

    "data": {
        "vizio": {
            "av": "JgBgAAABKJIUERQRFDYUERQRFBIUERQRFDYUNRQSFDUUNhQ2FDYUNhQ1FBIUERQRFDYUERQ2FBEUERQ2FDUUNhQRFDYUERQ2FAAFGgABKUgUAAxTAAEoSBUADFIAAShJFAANBQAAAAAAAAAA",

That’s one of my stored codes for:

vizio_j_av:
  sequence:
    - action: remote.send_command
      data:
        entity_id: remote.downstairs_mini_remote
        device: vizio
        command: av

Thank you for the reply.
I’m not sure exactly how it helps me.

I’d like to emphasis few points:

  1. When using the “Remote: Learn Command”, the IR command was stored in “config/.storage/broadlink_remote_#########_codes” file, under the code fan_off, hence the “command” under “action” in the YAML code.
  2. The script IS WORKING, so it does get the right IR command from that file.
  3. using the same YAML action, with a button on a dashboard, is working with no error message.

OK.
These are the scripts that I call to push buttons…
Home-Assistant-Config/script2/broadlink_script.yaml at 10f693f120bb64604667d4fbfd7646f5187f5321 · SirGoodenough/Home-Assistant-Config · GitHub.

I just call then as an action (service) when I need them, either from the dashboard or an automation somewhere.

Home-Assistant-Config/script2/jennifer_script.yaml at 10f693f120bb64604667d4fbfd7646f5187f5321 · SirGoodenough/Home-Assistant-Config · GitHub.

In lovelace:

                  "type": "button",
                  "tap_action": {
                    "action": "call-service",
                    "service": "script.vizio_j_av",
                    "service_data": {},
                    "target": {}

I found it !!

After your messages, I carefully compared the script’s YAML and the button’s YAML, and I’ve found a difference.
The script’s target was the area:

target:
  area_id: living_room

While the button’s target was the IR device in that area:

target:
  device_id: 734b2a16fce00f1052feaed5a421b65f

I’ve changed the script’s target to be the device, and now it’s working without the error.
The funny thing that it worked before, without specifying the actual IR device.

Anyway, It’s working now and I’m happy.
I appreciate your replies, they made me checked the code again, and find the issue.
Thank you.

1 Like

I was going to tell you that you had omitted to tell your automation which remote to use to send the command.

Your device_id fills that gap, but in all honesty I find it easier to use an entity_id as it’s easier to understand what it going on (there are also other good reasons why not use a device_Id)

My version of your script would look more like this"

alias: lv_fan_off
sequence:
  - action: remote.send_command
    data:
      device: living_room_fan
      command: fan_off
    target:
      # Using an entity_id here as I can both ...
      # - understand what is being targeted 
      # - update my YAML more easily if I ever replace the device
      entity_id: remote.broadlink_livingroom_01 
description: ""
icon: mdi:fan-off
1 Like

Thank you for the idea.
I changed my configuration to include entity_id and not device_id.
It’s working.

As you said, entity_id is more readable than device_id, and also can be update when a device is replaced.

The weird thing, was that even without specifying a device/entity the script WAS working, just with an error.
I would have expected it not to be working without a device/entity.

Yeah, well it was certainly “running”, but not to completion, and it was not actually achieving anything :slight_smile:

The script was ‘valid’, so it compiles, and even runs, but it was not ‘working’ as it produced nothing

Not true.
It was working 100%, the command was sent via the IR device to the fan, just by setting the area as the target.

OK, so then HomeAssistant must have been using that area_id to find all remotes assigned to that area (all remotes in your living_room) and iterated over them in order to sent the “fan_off” command

I wonder if it found, for instance, your TV remote too and tried to send the command “fan_off” to the TV ?

That might account for the logged error

That’s possible.

The Broadlink IR transmitter is the only IR device in the area.
Even my TV remote is not IR.