Hue integration API error when dimming lamp via brightness_pct

I have a problem with an automation that take the value from an ESPHome rotary encoder, and sends it to a hue lamp as % brightness.
This same thing happens if I use HA automation - e.g. below or a node-red flow.

- id: '1554409646088'
  alias: Office Dimmer
  trigger:
  - entity_id: sensor.dimmer_rotary_encoder
    platform: state
  condition: []
  action:
    service: light.turn_on
    entity_id: light.hue_color_lamp
    data_template:
      brightness_pct: '{{states.sensor.dimmer_rotary_encoder | int}}'

The error I see in HA logs is as follows:

Sat Apr 06 2019 15:55:53 GMT+0100 (British Summer Time)
Error while executing automation automation.office_dimmer. Unknown error for call_service at pos 1: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/automation/__init__.py", line 380, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 88, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1138, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1160, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/light/__init__.py", line 285, in async_handle_light_on_service
    await light.async_turn_on(**pars)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/hue/light.py", line 389, in async_turn_on
    await self.light.set_state(**command)
  File "/usr/local/lib/python3.7/site-packages/aiohue/lights.py", line 117, in set_state
    json=data)
  File "/usr/local/lib/python3.7/site-packages/aiohue/bridge.py", line 64, in request
    _raise_on_error(data)
  File "/usr/local/lib/python3.7/site-packages/aiohue/bridge.py", line 78, in _raise_on_error
    raise_error(data['error'])
  File "/usr/local/lib/python3.7/site-packages/aiohue/errors.py", line 39, in raise_error
    raise cls("{}: {}".format(type, error['description']))
aiohue.errors.AiohueException: 901: Internal error, 404

Any ideas what is causing this?
I speculate I might be pushing too many API calls to hue bridge since the rotary encoder value is changing quickly, but this used to work fine before (see here - I have been using this same automation on and off for a few years).
Seems the exception is thrown by the python API rather than being surfaced from the hue bridge though.

I was really disappointed that this didn’t get a response because I was trying to do something similar using the REST API and have the same problem. I also think it may have to do with the flood of messages.

Yeah, I ran in to this too and this is the only thread on Google with the error. The message does happen when you flood the bridge with requests. I’m hitting it 75 times with a script, and it throws that error quite a few times.

Edit: Missed the last line of OP, but it seems like the aiohue lib is passing the requests on to the bridge, so while aiohue is throwing the error, it’s throwing an error because the local bridge api is returning a 404. I don’t have a developer login for hue, or I’d check to see if they have some kind of rate limiting and what that limit is.

I’m seeing this same error when running an automation which turns off a whole bunch of lights for the evening.

Did anyone ever find a work-around to implementing these kind of patterns?

You may be able to solve this by implementing “debounce” functionality, so that the API is only called once after the state has stopped changing. Try the following:

- id: '1554409646088'
  alias: Office Dimmer
  trigger:
  - entity_id: sensor.dimmer_rotary_encoder
    platform: state
###add the following two lines
    for:
      seconds: 1
  condition: []
  action:
    service: light.turn_on
    entity_id: light.hue_color_lamp
    data_template:
      brightness_pct: '{{states.sensor.dimmer_rotary_encoder | int}}'

This should wait to call the API until the state has been stable for one second. It will introduce a slight delay in response but should prevent the API from getting slammed.