threshold sensor new limit type: range_inverted - #177661
Conversation
Both lower and upper limit configured and lower limit > upper limit - Turn on when the input sensor's value is not in the range [lower limit .. upper limit].
There was a problem hiding this comment.
Pull request overview
Adds an inverted threshold mode intended to activate when a sensor value falls outside the configured range.
Changes:
- Adds
range_invertedthreshold detection and state handling. - Updates configuration descriptions.
- Adds inverted-range tests with and without hysteresis.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
homeassistant/components/threshold/binary_sensor.py |
Implements inverted-range detection and state updates. |
homeassistant/components/threshold/const.py |
Defines the new threshold type. |
homeassistant/components/threshold/strings.json |
Documents inverted-range behavior. |
tests/components/threshold/test_binary_sensor.py |
Adds inverted-range behavior tests. |
Comments suppressed due to low confidence (2)
tests/components/threshold/test_binary_sensor.py:550
- Reverse these test limits so the hysteresis test reaches
TYPE_RANGE_INVERTED. The currentlower=10,upper=20configuration creates a normal range and therefore fails the type assertion instead of validating the new state machine.
CONF_LOWER: "10",
CONF_UPPER: "20",
homeassistant/components/threshold/strings.json:33
- Describe the inverted interval in ascending order as
[upper limit .. lower limit]. The current reversed interval is mathematically empty and therefore gives users an inaccurate explanation of which values turn the sensor on.
"description": "Only lower limit configured - Turn on when the input sensor's value is less than the lower limit.\nOnly upper limit configured - Turn on when the input sensor's value is greater than the upper limit.\nBoth lower and upper limit configured and lower limit <= upper limit - Turn on when the input sensor's value is in the range [lower limit .. upper limit].\nBoth lower and upper limit configured and lower limit > upper limit - Turn on when the input sensor's value is not in the range [lower limit .. upper limit]."
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (6)
homeassistant/components/threshold/binary_sensor.py:338
- Invert the on/off assignments so this mode turns on outside the normalized bounds. With
lower > upper, this branch currently sets the entity off belowupperand abovelower, but on between them, which is the opposite of the documented feature.
if below(self.sensor_value, self._threshold_upper):
self._state_position = POSITION_BELOW
self._attr_is_on = False
elif above(self.sensor_value, self._threshold_lower):
self._state_position = POSITION_ABOVE
tests/components/threshold/test_binary_sensor.py:458
- Swap these limits so this test actually selects
TYPE_RANGE_INVERTED._threshold_type()only returns the inverted type whenlower > upper, so this setup remainsTYPE_RANGEand the assertion at line 475 fails.
CONF_LOWER: "10",
CONF_UPPER: "20",
tests/components/threshold/test_binary_sensor.py:550
- Swap these limits so the hysteresis test exercises
TYPE_RANGE_INVERTED. Because10 <= 20, this setup creates a normal range sensor and fails the type assertion at line 568 before testing the new branch.
CONF_LOWER: "10",
CONF_UPPER: "20",
homeassistant/components/threshold/strings.json:15
- Describe the inverted interval in ascending bound order. When
lower > upper, the finite interval tested by the implementation is[upper limit .. lower limit];[lower limit .. upper limit]denotes an empty/reversed interval and makes the UI guidance misleading.
"description": "Create a binary sensor that turns on and off depending on the value of a sensor\n\nOnly lower limit configured - Turn on when the input sensor's value is less than the lower limit.\nOnly upper limit configured - Turn on when the input sensor's value is greater than the upper limit.\nBoth lower and upper limit configured and lower limit <= upper limit - Turn on when the input sensor's value is in the range [lower limit .. upper limit].\nBoth lower and upper limit configured and lower limit > upper limit - Turn on when the input sensor's value is not in the range [lower limit .. upper limit].",
homeassistant/components/threshold/strings.json:33
- Describe the inverted interval in ascending bound order. For
lower > upper, users are configuring values outside[upper limit .. lower limit], not outside the reversed interval currently shown here.
"description": "Only lower limit configured - Turn on when the input sensor's value is less than the lower limit.\nOnly upper limit configured - Turn on when the input sensor's value is greater than the upper limit.\nBoth lower and upper limit configured and lower limit <= upper limit - Turn on when the input sensor's value is in the range [lower limit .. upper limit].\nBoth lower and upper limit configured and lower limit > upper limit - Turn on when the input sensor's value is not in the range [lower limit .. upper limit]."
tests/components/threshold/test_binary_sensor.py:447
- Parameterize the existing range tests for normal and inverted modes instead of copying their setup and assertions. These new functions duplicate
test_sensor_in_range_no_hysteresisandtest_sensor_in_range_with_hysteresisalmost verbatim, and that duplication has already retained the normal-range limits in both inverted cases.
This issue also appears on line 549 of the same file.
async def test_sensor_in_range_inverted_no_hysteresis(
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
homeassistant/components/threshold/strings.json:15
- Update the Threshold integration documentation for the new reversed-limit behavior. The PR notes that the published documentation still says two limits turn the sensor on inside the range, so changing only the config-flow text leaves YAML users with documentation that contradicts the new behavior.
"description": "Create a binary sensor that turns on and off depending on the value of a sensor\n\nOnly lower limit configured - Turn on when the input sensor's value is less than the lower limit.\nOnly upper limit configured - Turn on when the input sensor's value is greater than the upper limit.\nBoth lower and upper limit configured and lower limit <= upper limit - Turn on when the input sensor's value is in the range [lower limit .. upper limit].\nBoth lower and upper limit configured and lower limit > upper limit - Turn on when the input sensor's value is not in the range [upper limit .. lower limit].",
…sor_inverse_range
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
homeassistant/components/threshold/binary_sensor.py:160
- Introduce an explicit inversion setting instead of reinterpreting existing reversed limits. Before this change every configuration with both limits—including
lower > upper, which both YAML and config-flow schemas accept—wasTYPE_RANGE; these existing entries will now change from an effectively always-off sensor to an outside-range alarm, so the claimed backward-compatible feature changes persisted behavior without migration or opt-in.
if lower <= upper:
return TYPE_RANGE
return TYPE_RANGE_INVERTED
Both lower and upper limit configured and lower limit > upper limit - Turn on when the input sensor's value is not in the range [lower limit .. upper limit].
Proposed change
I'm using two Threshold sensors to define a lower limit for a pressure value and another Threshold sensors to define an upper limit for a pressure value.
These helpers are having the value "Show as" -> "Problem"
I need this because the if I define lower and upper like [100, 200] then I get a problem when the value of the sensor between these values as it is documented:
But for my case that range is the "No Problem" and if the pressure is outside the range then it means problem.
If there would be a
[upper limit .. lower limit]option which would invert the value then it would solve my problem.What do I mean: I put a bigger number to the
lower limitfield than to theupper limitfield it could signal problem when the sensor's value is outside that range.I know I could use template or other magic but this simple backward compatible addition would nicely fit my case.
Type of change
Additional information
https://github.com/orgs/home-assistant/discussions/4452
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: