Default value for optional entity

So I’ve created a Blueprint that uses an optional entity selector.

light_sensor_entity:
  name: Light Sensor 
  description: Select a light sensor
  default: []
   selector:
     entity:
       domain: sensor
       device_class: illuminance

The problem is that if this field is kept empty, the blueprint doesn’t run and spits out an error: Stopped because of unknown reason “null” at January 8, 2024 at 11:22:43 (runtime: 0.00 seconds)

Have I misconfigured this? My understanding is that by stating “default ”, it’s given an empty array of entities, but it shouldn’t read null.

    light_sensor_entity:
      name: Light Sensor 
      description: Select a light sensor
      default:
      selector:
        entity:
          filter:
            - domain: sensor
              device_class: illuminance

→ EDITED in case someone else comes around so that sensor and iluminance both filter… ←

Thanks for the reply, but it made no difference to the behaviour of the automation.

In fact, it introduced another issue:
It seems that the filter list only looks at the first item, because with your proposed configuration it allowed the selector to select all sensors instead of only the light sensors.

Although my configuration worked fine in that regard, I ammended it with the filter variable to correctly reflect the documentation:

light_sensor_entity:
  name: Light Sensor 
  description: Select a light sensor
  default:
   selector:
     entity:
       filter:
         - device_class: illuminance

But I’m still left with the issue that the blueprint doesn’t work unless a light sensor entity is selected.

I’ve tried the default variable in multiple ways:

default:
default: []
default: {}
default: ' '

But no changes in behaviour…

This then might be better

    light_sensor_entity:
      name: Light Sensor 
      description: Select a light sensor
      default:
      selector:
        entity:
          filter:
            - domain: sensor
              device_class: illuminance

then for the filter.

This passes syntax and as far as I can test works in one of my BP’s.
Are you sure the error is with that input, or possible something else is complaining and not that one?

If the variable generated by the input is required in the conditions or actions, it will error if default is empty and there is no input there.

Hi man,

Thanks for the lightbulb moment:

The issue was that I had defined the variable and was using it to calculate a value regardless of whether it was defined or not.
I’ve added a template to catch the exception:

variables:
  light_sensor: !input light_sensor_entity

  dynamic_brightness_pct: >
    {% if mode == "dynamic" %}
      {{ (( slope * states(light_sensor)|int ) + constant)|round }}
    {% else %}
      0
    {% endif %}