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!

Did you figure this out? I am seeing the same behavior.

Actually, I think I fixed it, although my automation has been working for a long time without this. I’m not sure if something changed.

Instead of triggering from ‘off’ to ‘on’ for the light, I qualify it with at least 1 second. My thinking is that there is a instant where the light has turned on but the new brightness value hasn’t. I was seeing a ‘1’ for the brightness when I created an automation to read the value and write it to the log. And I was also seeing this behavior where the value was sometimes the previous value as noted.

This seemed to fix it:

description: Debug Alexa Bridge Light
triggers:

  • entity_id:
    • light.alexa_bridge_light
      from: “off”
      to: “on”
      for:
      hours: 0
      minutes: 0
      seconds: 2
      trigger: state

Hi @tomballgithub, thanks for the reply and for the sanity check. I hadn’t solved it yet, but I think you just did!

I was starting to think I needed to just give up and use separate lights or something similar. I also would have sworn that my automation was set up to delay like yours, but I’d recently retooled it with the maps/dictionaries I mentioned above, so it’s entirely possible I made a change to the trigger and simply forgot.

I want to say that my original trigger was something like 1 second and I’d walked it back to 750ms, but when I just checked, it was definitely just “on” without any kind of delay.

I will play with the delay, but my spot-checking a few minutes ago with 2 seconds seems to be working exactly like it used to, so hard to say if I created the bug by inadvertently removing the delay or if something changed, but I’ll take it. Thanks so much!

1 Like

I’m glad it’s working for you. I noticed your example didn’t have the time qualifier but mine never used to either.

Another way to trigger it would be off of any change to the brightness level, but maybe there’s a downside to that?