Automation was working, now generates an error

I have an error now showing up, in my logs I’m getting this error:

"Error in 'Turn on safety light' trigger: In 'numeric_state' condition: entity light.lr_stair state '' cannot be processed as a number"

and the automation that’s probably causing it is this:

############################
#                          #
#   SAFETY LIGHT TURN ON   #
#   IF DARK AND OCCUPIED   #
#   AND AFTER SUNDOWN      #
#                          #
############################
# Turn on safety light
- id: "Turn on safety light"
  alias: 'Turn on safety light'
  initial_state: true
  trigger:                        # ( ANY of these - All OR statements for triggers)
    - platform: numeric_state     # less than 48%
      entity_id: light.lr_stair
      value_template: '{{ state.attributes.brightness }}'
      below: '120'
    - platform: state             # or manual trigger
      entity_id: input_boolean.reset_lights
      to: 'on'
    - platform: state             # or lights off
      entity_id: light.lr_stair
      to: 'off'
    - platform: state             # or step gets turned off accidentally 
      entity_id: light.steps
      to: 'off'
    - platform: state             # after sundown
      entity_id: sun.sun
      to: 'below_horizon'         # (normal run) ****
#      to: 'above_horizon'         # (daytime testing)****
    - platform: state             # or come home
      entity_id: input_boolean.occupancy
      to: 'on'
  condition:
    - condition: state
      entity_id: light.steps
      state: 'off'
    - condition: state
      entity_id: input_boolean.occupancy
      state: 'on'
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'      # (normal run) ****
#      state: 'above_horizon'      # (daytime testing)****
    - condition: or
      conditions:
        - condition: state
          entity_id: light.lr_stair
          state: 'off'      
        - condition: numeric_state     # less than 48%
          entity_id: light.lr_stair
          value_template: '{{ state.attributes.brightness }}'
          below: '120'
  action:
    - service: light.turn_on
      data:
        brightness_pct: 26
        color_name: orange
        entity_id: light.steps

Be kind, only just starting to learn YAML - I’ve researched everything I can think of and am at a loss now!

I do have one other automation that could be causing the error, which is basically the opposite of this above one to turn the safety light off, so whatever is wrong with one will be wrong with both. I can work out how to fix the other from the fix for this one. I’m assuming that something changed in a version update and I missed it.

That sensor is probably going unavailable. What does your sensor’s history show?

It’s also best to use the state_attr function, since it give a more consistent result among the different unknown and unavailable states. You’d still need to check for edge cases.

There’s usually no need for a value template as attributes can be accessed directly like this:

        - condition: numeric_state     # less than 48%
          entity_id: light.lr_stair
          attribute: brightness
          below: '120'

Note that the brightness attribute is only available when the light is on. So if you are still getting errors using the above condition you may have to use something like this:

        - condition: template
          value_template: >
            {% if state_attr('light.lr_stair', 'brightness')|is_number %}
              {{ state_attr('light.lr_stair', 'brightness') < 120 }}
            {% else %}
              {{ false }}
            {% endif %}
                

ahhh good point you all made, forgot about when it’s not on… was thinking it would just return brightness =0 …
I’ll have to do some testing for that and see what it returns. Technically it’s always “on” as in power running through the circuit being a wifi bulb, it’s just not always lit.
For example, you can send commands to change the colour etc even when its not lit, and it will be that colour when you eventually tell it to “turn on” (Light up)

I’ll do some more research, THANKS HEAPS!