Repository navigation
Validate sensor state before sending to Home Assistant #2090
Replies: 3 comments 2 replies
|
The proposed validation changes look fine to me. I do have a question about whether we can handle some of this validation on the Home Assistant side rather than maintaining all the validation rules in Node-RED. Off the top of my head, there are a few other examples, like the Select node validating that a selected option exists in the configured list, or the Text and Sensor nodes validating that the state doesn't exceed the 255-character limit. That's not even counting the different cases you've brought up here. Does Home Assistant expose any kind of validation mechanism that we could use on its side and then pass the resulting validation errors back to Node-RED, similar to how errors are currently handled by the Action node when an invalid action is provided? If that's not possible, that's fine. It was just my first thought when considering this, as it'd be nice to avoid maintaining validation rules on our side when Home Assistant can already handle them. As for your questions:
|
|
PR done for the companion side zachowj/hass-node-red#433 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Sensor nodes currently send whatever the State typed input evaluates to. Some values can't work in Home Assistant, but the node still shows success, so the problem is easy to miss. I'd like to propose validating the state in the node, the same way Available has been validated since #2041. I'm happy to open a PR once we agree on the rules.
What happens today
Non-numeric state on a numeric sensor: if the entity has a numeric device class, state class, or unit of measurement, Home Assistant refuses the state when it writes it:
The companion has already acknowledged the
nodered/entitymessage by then (the update is applied through the dispatcher, which logs the exception), so the node shows a green status and the entity keeps its previous value. The only sign is the error in the Home Assistant log.NaNor±Infinity: JSON has no representation for these, so they're sent asnulland the sensor silently becomes unknown.Objects and arrays as state are already rejected by the companion's message schema, and that error is shown on the node, so they aren't part of this.
Proposal
Before sending, reject the message in the node (red status,
InputError, no update sent), as is done for Available, when:NaNor±Infinity.A plain sensor with none of those set keeps accepting any string, as it does now.
Questions
NaN/±Infinitybecomingnull), or should we leave attributes alone?boolrules as Available there too, or is that out of scope?All reactions