Set entity sensor to unavailable / unknown

Hi everybody,

I have a sensor for the internet speed. And I want it to become unknown after 15 minutes, which is easily done with a timer node. But if it gets unavailabl, home assistant throws an error. Is there a “correct” way to do this?

Here is the errors:

Logger: homeassistant.components.sensor
Source: components/sensor/__init__.py:595
Integration: Sensor (documentation, issues)
First occurred: 11:29:21 (2 occurrences)
Last logged: 11:29:21

Error adding entities for domain sensor with platform nodered
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 591, in state
    numerical_value = int(value)
                      ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'unknown'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 510, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 750, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1004, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 746, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 808, in _async_write_ha_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 752, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 595, in state
    raise ValueError(
ValueError: Sensor sensor.breitbandmessung_download has device class 'data_rate', state class 'measurement' unit 'Mbit/s' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'unknown' (<class 'str'>)

Logger: homeassistant
Source: components/sensor/__init__.py:595
First occurred: 11:29:21 (2 occurrences)
Last logged: 11:29:21

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 591, in state
    numerical_value = int(value)
                      ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'unknown'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 510, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 750, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1004, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 746, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 808, in _async_write_ha_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 752, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 595, in state
    raise ValueError(
ValueError: Sensor sensor.breitbandmessung_download has device class 'data_rate', state class 'measurement' unit 'Mbit/s' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'unknown' (<class 'str'>)

You have to change the attribute available to false. Can’t push nonnumeric values to a numeric sensor anymore.

Ah, that make sence, thank you. Can you tell me how to do this? I have a function node which creates the message with the values. I have another function node which sends

msg.payload = {
    available: false
};

But then there is a home assistant error.

InvalidPropertyValueError: Invalid state
    at SensorController.onInput (/config/node-red/node_modules/node-red-contrib-home-assistant-websocket/dist/common/controllers/SensorBaseController.js:80:19)
    at SensorController._InputOutputController_preOnInput (/config/node-red/node_modules/node-red-contrib-home-assistant-websocket/dist/common/controllers/InputOutputController.js:62:76)
    at /opt/node_modules/@node-red/runtime/lib/nodes/Node.js:210:26
    at Object.trigger (/opt/node_modules/@node-red/util/lib/hooks.js:166:13)
    at Node._emitInput (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:202:11)
    at Node.emit (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:186:25)
    at Node.receive (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:485:10)
    at Immediate.<anonymous> (/opt/node_modules/@node-red/runtime/lib/flows/Flow.js:831:52)
    at process.processImmediate (node:internal/timers:476:21)
    at process.topLevelDomainCallback (node:domain:161:15)
    a...

Look into how to push attributes to a sensor node in the Node-RED HA Websocket docs — I don’t recall off the top of my head.

I did it as described in the docs. Input can be:

properties of msg.payload

state
string | number | boolean
The state the entity should be updated to
attributes
Object
Key/Value pair of attributes to update. The key should be a string and the value can be a

So if I want to change the attribute available it has to be part of the payload. That is what I did in the example message above. But that raises an error about the state.

Yes, so msg.payload={attributes: {available: false}} is how you update the attributes. The payload may only have two entries, one is state which is numeric and the other is attributes which is a dictionary.

Thank you, but it is still the same error message. Complaining about a missing state.

InvalidPropertyValueError: Invalid state
    at SensorController.onInput (/config/node-red/node_modules/node-red-contrib-home-assistant-websocket/dist/common/controllers/SensorBaseController.js:80:19)
    at SensorController._InputOutputController_preOnInput (/config/node-red/node_modules/node-red-contrib-home-assistant-websocket/dist/common/controllers/InputOutputController.js:62:76)
    at /opt/node_modules/@node-red/runtime/lib/nodes/Node.js:210:26
    at Object.trigger (/opt/node_modules/@node-red/util/lib/hooks.js:166:13)
    at Node._emitInput (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:202:11)
    at Node.emit (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:186:25)
    at Node.receive (/opt/node_modules/@node-red/runtime/lib/nodes/Node.js:485:10)
    at Immediate.<anonymous> (/opt/node_modules/@node-red/runtime/lib/flows/Flow.js:831:52)
    at process.processImmediate (node:internal/timers:476:21)
    at process.topLevelDomainCallback (node:domain:161:15)
    a...

It’s not currently possible to manually set an entity to unavailable.

I was looking into this and found a solution. Replying here to help others later.

If you want to set a Home Assistant entity to Unknown, send a null value to the entity node in Node RED.
I have tested with both a binary sensor and a sensor.
Using a change node, set msg.payload to js expression null

image