Emulated Hue + Template Light + Alexa Strange Behavior

Hello! First, sorry if this is in the wrong place, but I wasn’t 100% sure where this question fits. I have been doing something very similar to this for a while:

and it has generally worked well. Lately, however, it has started misbehaving in a very specific and repeatable way, but I’m not really sure of the issue.

The short version is that the automation/light/template/Alexa/something seems to remember the last brightness level for the virtual light, and so it will run the wrong script. A second call to an Echo with the same Routine always correctly sets the brightness and thus runs the correct script. It did not “miss” like this until recently (~ the last couple of weeks), though.

For reference sake, my Template Light is very simple and looks like this:

  - platform: template
    lights:
      alexa_virtual:
        friendly_name: "Alexa Virtual Light"
        unique_id: "0e8d7dd2-25d3-4335-9a74-cdad1d48e4d6"
        turn_on:
        turn_off:
        set_level:

The automation is watching for the light to come on for 1 second, checks the brightness of the virtual light, uses a map/dictionary to find out which script to run, and then turns the light back off. I had started off doing a manual calculation very much like the topic above (e.g. Convert 0-255 brightness back to % value), but when it started misbehaving, I set up a dummy Routine with different light percentages just to see what brightness value the automation Trigger was reporting and used those numbers directly.

I have tried a couple of different ways to get the value from the trigger:

  • trigger.to_state.attributes.brightness
  • {{ state_attr('light.alexa_virtual', 'brightness') }}

and neither of them really work reliably at this point, so I’m not really sure what has changed or how to debug it. The values are pulled out like the below. I added a 2nd variable to see both versions of reading the input, because sometimes they are different:

variables:
  action_map: |
    {{
      {
        4: {'param': '', 'script': 'script.dinner_jeopardy'},
        6: {'param': '', 'script': 'script.time_for_bed'},
        9: {'param': 'off', 'script': 'script.den_fan'},
        11: {'param': 'low', 'script': 'script.den_fan'},
        14: {'param': 'medium', 'script': 'script.den_fan'},
        16: {'param': 'high', 'script': 'script.den_fan'},
        19: {'param': 'mailbox', 'script': 'script.entity_state'},
        21: {'param': 'gates', 'script': 'script.entity_state'},
        24: {'param': 'washer', 'script': 'script.entity_state'},
        26: {'param': 'leaving_house', 'script': 'script.leaving_the_house'},
        29: {'param': 'dryer', 'script': 'script.entity_state'}
      }
    }}
  action_id: "{{ trigger.to_state.attributes.brightness }}"
  orig_action_id: "{{ state_attr('light.alexa_virtual', 'brightness') }}"

and the actual action to run the selected script looks like this:

- data:
      entity_id: light.alexa_virtual
      param: |
        {{ action_map.get(action_id, {}).get('param', '') }}
    action: |
      {{ action_map.get(action_id, {}).get('script', 'script.empty') }}

I guess what I’m most interested in at this point is how to debug it? Is there a way I can see the actual HTTP requests coming fro the Echo to Home Assistant? I tried turning on Debug logging for Emulated Hue, but it only seems to log UPNP requests and I never see anything that looks like a payload containing the brightness.

As an example, just now I tried the 1% and 2% values in succession, starting with a “good” result:

  1. Alexa set virtual light to 1% (so should be a brightness of 4) :white_check_mark:
    Trigger:
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: light.alexa_virtual
  from_state:
    entity_id: light.alexa_virtual
    state: 'off'
    attributes:
      supported_color_modes:
        - brightness
      color_mode: null
      brightness: null
      friendly_name: Alexa Virtual Light
      supported_features: 0
    last_changed: '2024-12-09T20:05:59.890118+00:00'
    last_reported: '2024-12-09T20:06:07.876691+00:00'
    last_updated: '2024-12-09T20:05:59.890118+00:00'
    context:
      id: 01JEPGEJDYRJ6A9JG8JA0R5YJD
      parent_id: 01JEPGEJDYNEB4BVJD4767B5KF
      user_id: null
  to_state:
    entity_id: light.alexa_virtual
    state: 'on'
    attributes:
      supported_color_modes:
        - brightness
      color_mode: brightness
      brightness: 4
      friendly_name: Alexa Virtual Light
      supported_features: 0
    last_changed: '2024-12-09T20:06:10.649863+00:00'
    last_reported: '2024-12-09T20:06:10.749727+00:00'
    last_updated: '2024-12-09T20:06:10.649863+00:00'
    context:
      id: 01JEPGF0ES9Y5BN4DERKJHV4M8
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of light.alexa_virtual

Variables:

action_id: 4
orig_action_id: 4
  1. Alexa set virtual light to 2% (so should be a brightness of 6) :x:
    Trigger:
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: light.alexa_virtual
  from_state:
    entity_id: light.alexa_virtual
    state: 'off'
    attributes:
      supported_color_modes:
        - brightness
      color_mode: null
      brightness: null
      friendly_name: Alexa Virtual Light
      supported_features: 0
    last_changed: '2024-12-09T20:06:14.255338+00:00'
    last_reported: '2024-12-09T20:06:20.763279+00:00'
    last_updated: '2024-12-09T20:06:14.255338+00:00'
    context:
      id: 01JEPGF0ETW146S4JFQ8BPWTJ7
      parent_id: 01JEPGF0ES9Y5BN4DERKJHV4M8
      user_id: null
  to_state:
    entity_id: light.alexa_virtual
    state: 'on'
    attributes:
      supported_color_modes:
        - brightness
      color_mode: brightness
      brightness: 4
      friendly_name: Alexa Virtual Light
      supported_features: 0
    last_changed: '2024-12-09T20:13:23.238687+00:00'
    last_reported: '2024-12-09T20:13:23.238687+00:00'
    last_updated: '2024-12-09T20:13:23.238687+00:00'
    context:
      id: 01JEPGW6X6N7PG6SWPDCN3W603
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of light.alexa_virtual

Variables:

action_id: 4
orig_action_id: 4
  1. Run routine with brightness set to 2% again (so brightness should be 6) :white_check_mark: :
    Trigger:
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: light.alexa_virtual
  from_state:
    entity_id: light.alexa_virtual
    state: 'off'
    attributes:
      supported_color_modes:
        - brightness
      color_mode: null
      brightness: null
      friendly_name: Alexa Virtual Light
      supported_features: 0
    last_changed: '2024-12-09T20:13:26.843056+00:00'
    last_reported: '2024-12-09T20:13:33.421504+00:00'
    last_updated: '2024-12-09T20:13:26.843056+00:00'
    context:
      id: 01JEPGW6X696ESCXGMPP52QVAQ
      parent_id: 01JEPGW6X6N7PG6SWPDCN3W603
      user_id: null
  to_state:
    entity_id: light.alexa_virtual
    state: 'on'
    attributes:
      supported_color_modes:
        - brightness
      color_mode: brightness
      brightness: 6
      friendly_name: Alexa Virtual Light
      supported_features: 0
    last_changed: '2024-12-09T20:15:14.012417+00:00'
    last_reported: '2024-12-09T20:15:14.112882+00:00'
    last_updated: '2024-12-09T20:15:14.012417+00:00'
    context:
      id: 01JEPGZK2V0BP1SF50Q26A3GM9
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of light.alexa_virtual

Variables:

action_id: 6
orig_action_id: 6

I’m guessing it’s somewhere in the Emulated Hue / Alexa side of things, because manually calling the action light.turn_on with a brightness value works as expected, and using the same 1% value ends up as a 3 (rather than Alexa’s 4), but otherwise sends the same brightness value every run.

One other oddity that did not happen in this run but I have seen in the traces a few different times playing with this is that occasionally action_id has the wrong (old) value and orig_action_id will have the correct (new) one, which is weird but also feels relevant.

For anyone who got to the bottom of this giant post, thank you, and also to anyone who might know what is going on. I think if I could see what Alexa was actually sending that would help a lot in terms of knowing where to debug further (e.g. is it in Emulated Hue, is the Routine sending the wrong value, is HA internally messing up the percentage value, etc.). Thanks!