"None" as a state for a numeric trigger-based template sensor?

Hello,

I understand that a trigger-based sensor that expects a numeric state (e.g., device_class ‘illuminance’ and state_class 'measurement) requires either a numeric state OR “None.” The docs for the state field say:

If the sensor is numeric, i.e. it has a state_class or a unit_of_measurement , the state template must render to a number or to none .

When I try this, though, I’m getting errors in the log such as:

ValueError: Sensor sensor.dt_lux_stairsup_illum has device class ‘illuminance’, state class ‘measurement’ unit ‘lx’ and suggested precision ‘None’ thus indicating it has a numeric value; however, it has the non-numeric value: ‘None’ (<class ‘str’>)

I was avoiding even using the string “none” and instead wrapping that word in jinja print braces, in case Home Assistant could somehow distinguish between the string “none” and whatever jinja hands HA when something has the Jinja value none, but I still get the error.

Am I misunderstanding the docs, or doing something wrong? I am not posting code right now because it’s not the easiest to put into a post, but if this behavior seems wrong I can simplify to an easier test case.

Thank you.

Post the template.

Thanks for following up. I can reproduce the problem with this simple test case, which is followed below by an excerpt of the logs (let me know if the full output is needed–it’s pretty long).

- trigger:

  - platform:       event
    event_type:     event_template_reloaded
  - platform:       homeassistant
    event:          start

  sensor:
    - unique_id:            "test_trigger_template_sensor"
      state_class:          "measurement"
      unit_of_measurement:  "lx"
      device_class:         "illuminance"
      state:                >
        {{ none }}

Excerpt of log:

ValueError: Sensor sensor.template_test_trigger_template_sensor has device class ‘illuminance’, state class ‘measurement’ unit ‘lx’ and suggested precision ‘None’ thus indicating it has a numeric value; however, it has the non-numeric value: ‘None’ (<class ‘str’>)
2024-04-16 19:55:11.650 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/sensor/init.py”, line 621, in state
numerical_value = float(value) # type:ignore[arg-type]
^^^^^^^^^^^^
ValueError: could not convert string to float: ‘None’