Help with an automation action based on Brightness

Using an idea from another post I wanted to create an automation based on the percentage level of a virtual light. I cannot seem to get the action to work. I’ve tried light_level, brightness, brightness_pct, but always am getting a value of “unknown”. I’m sure I’m just doing something wrong. Anyone have any ideas

- alias: "Alexa Report"
  trigger:
  - platform: state
    entity_id: light.alexa_virtual
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  action:
  - service: script.audio_notify
    data_template:
      tts_msg: >-
        {% if states('light.alexa_virtual.attributes.brightness') | int == 1  %}
          ME is away and is approximately {{ states('sensor.my_time_to_home') }} minutes from home.
        {% elif states('light.alexa_virtual.attributes.light_level') | int == 2 %}
          YOU is away and is approximately {{ states('sensor.my2_time_to_home') }} minutes from home.
        {% elif states('light.alexa_virtual_brightness_pct') | int == 3 %}
          Something other than what you've already said
        {% else %}
         {{ states('light.alexa_virtual.attributes.brightness') }}
        {% endif %}
      mplayer: "living_room_echo"
  - service: light.turn_off
    entity_id: light.alexa_virtual

And my configuration yaml for the light

light:
  - platform: template
    lights:
      alexa_virtual:
        friendly_name: "Alexa Dummy Light"
        turn_on:
        turn_off:
        set_level:

I keep getting the ELSE results, with a return of “unknown”

The states() function takes one parameter which is an entity_id. You’re not using it that way. If you want an entity’s attribute, you should use the state_attr() function, which takes two parameters, the first being the entity_id and the second being the attribute name. See details here.

If you’re not sure what attributes an entity has, try entering something like the following in the template editor:

{{ states.light.alexa_virtual.attributes }}

Thanks again for the help. Ended up going with this

(states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 1

This gives me the values from 1 to 100 that I was looking for. Thanks Again for the kick in the right direction

Anyone experience strange behavior since upgrading to 2.12 version 0.98.4 on this automation?

I had to recreate my 0.84.4 system and started with the latest version of Hassio release 2.12. It’s been a long struggle finding things that have changed, but this one has got me stumped.

My automation looks for brightness levels ==1, ==2, ==3, ==4, ==5, and ==6 and worked fine under 0.84.4. The strange behavior (with no modifications to the automation) is that it never compares == to 1, 2, or 3 anymore. The others, 4, 5, and 6 work fine.

When I set it to 1, the automation acts like it is 2. If I set it to 2, it acts on 3, if I set it to 3 it acts on 4.
Now if I set it to 4, 5, or 6, it acts as expected.

Here is the message portion of the automation where all the math occurs:

message: |-
    {% if (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 1 and states('device_tracker.81') == 'not_home' %}
     Joe is away .
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 1 and states('device_tracker.81') == 'home' %}
      Joe is at home.
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 2 and states('device_tracker.82') == 'not_home' %}
      Mark is not home.
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 2 and states('device_tracker.82') == 'home' %}
      Mark is at home.
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 3 and is_state('switch.marks_door', 'on') or is_state('switch.joes_door', 'on') %}
      Marks door is {{states('switch.joes_door')}}. Joes door is {{states('switch.joes_door')}}. 
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 3 and is_state('switch.marks_door', 'off') and is_state('switch.joes_door', 'on') %}
      Marks door is {{states('switch.marks_door')}}. Joes door is {{states('switch.joes_door')}}. 
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 3 and is_state('switch.marks_door', 'off') and is_state('switch.joes_door', 'off') %}
      Garage is closed.        
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 4 %} The house temperature is {{states ('sensor.house_temp')}} degrees. 
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 5 %} Place holder for 5 at the moment. 
    {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 6 %} Place holder for 6 at the moment.  
    {% else %}
      I don't know virtual bulb brightness level {{ states('light.alexa_virtual.attributes.brightness') }}.
    {% endif %}

Like I said, not sure why 1, 2, 3 are seen as 2, 3, 4, but 4, 5, 6 are seen correctly.

Old thread I know, but it seemed like the right place to pose the question.

Thanks