zachowj / hass-node-red

Companion Component for node-red-contrib-home-assistant-websocket to help integrate Node-RED with Home Assistant Core
MIT License
460 stars 182 forks source link

how to set null to timestamp class #102

Closed michalk-k closed 2 years ago

michalk-k commented 2 years ago

Configuration

HA core-2021.12.7 NR 10.3.0 Node-RED Companion 1.0.5

Describe the bug

I need to be able to set "unknown" or "unavailable" state to the sensor which is of timestamp class.

Unfortunatelly whatever I pass to state of the sensor, HA complains (while NR shows no error) Here is exemplary flow which NR executes without problem:

[{"id":"72d4b4b71adf49c9","type":"inject","z":"a74a1247.24c79","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":580,"y":50,"wires":[["c0bb1d4d54cfa8c4"]]},{"id":"c0bb1d4d54cfa8c4","type":"function","z":"a74a1247.24c79","name":"","func":"msg.data = { \"attributes\": { \"cue\": \"N/A\", \"days_diff\": \"N/A\",\n\"cue_weekday\": \"\"\n}};\nmsg.payload = 'Unknown';\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":130,"wires":[["5f082d7a.61a1ec"]]},{"id":"5f082d7a.61a1ec","type":"ha-entity","z":"a74a1247.24c79","name":"Waste High Volume Sensor","server":"6cdd0bc8.b8e434","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Waste High Volume"},{"property":"device_class","value":"timestamp"},{"property":"icon","value":"mdi:trash-can-outline"},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[{"property":"cue","value":"data.attributes.cue","valueType":"msg"},{"property":"cue_weekday","value":"data.attributes.cue_weekday","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":740,"y":310,"wires":[[]]},{"id":"6cdd0bc8.b8e434","type":"server","name":"HA MaXyM","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

And here snippet from HA log in response of executing flow above

2022-01-04 15:38:38 ERROR (MainThread) [homeassistant.util.logging] Exception in handle_entity_update when dispatching 'nodered_entity_6cdd0bc8.b8e434_5f082d7a.61a1ec': ({'type': 'nodered/entity', 'server_id': '6cdd0bc8.b8e434', 'node_id': '5f082d7a.61a1ec', 'state': 'Unknown', 'attributes': {'cue': 'N/A', 'cue_weekday': ''}, 'id': 12782},)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 388, in state
    value = ciso8601.parse_datetime(str(value))
ValueError: Invalid character while parsing year ('U', Index: 0)

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

Traceback (most recent call last):
  File "/config/custom_components/nodered/__init__.py", line 135, in handle_entity_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 505, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 538, in _async_write_ha_state
    state = self._stringify_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 511, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 390, in state
    raise ValueError(
ValueError: Invalid date/datetime: sensor.waste_high_volume provide state 'Unknown', while it has device class 'timestamp'

I tried to pass null value, 'unavailable' (which btw is reported as Unknown later), and 'unknown' from example above

What is the valid way to set unavailable state to timestamp sensor?