Skip to content

threshold sensor new limit type: range_inverted - #177661

Open
matepek wants to merge 6 commits into
home-assistant:devfrom
matepek:feature/threashold_sensor_inverse_range
Open

threshold sensor new limit type: range_inverted#177661
matepek wants to merge 6 commits into
home-assistant:devfrom
matepek:feature/threashold_sensor_inverse_range

Conversation

@matepek

@matepek matepek commented Jul 30, 2026

Copy link
Copy Markdown

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:

Both lower and upper limit configured - Turn on when the input sensor's value is in the range [lower limit .. upper limit].

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 limit field than to the upper limit field 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

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

https://github.com/orgs/home-assistant/discussions/4452

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

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].
Copilot AI review requested due to automatic review settings July 30, 2026 15:21
@home-assistant home-assistant Bot added cla-signed has-tests integration: threshold new-feature small-pr PRs with less than 30 lines. Top 200 Integration is ranked within the top 200 by usage Quality Scale: internal labels Jul 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an inverted threshold mode intended to activate when a sensor value falls outside the configured range.

Changes:

  • Adds range_inverted threshold 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 current lower=10, upper=20 configuration 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]."

Comment thread homeassistant/components/threshold/binary_sensor.py
Comment thread tests/components/threshold/test_binary_sensor.py Outdated
Comment thread homeassistant/components/threshold/strings.json Outdated
Copilot AI review requested due to automatic review settings July 30, 2026 15:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 below upper and above lower, 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 when lower > upper, so this setup remains TYPE_RANGE and 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. Because 10 <= 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_hysteresis and test_sensor_in_range_with_hysteresis almost 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(

Copilot AI review requested due to automatic review settings July 30, 2026 15:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread homeassistant/components/threshold/binary_sensor.py
Copilot AI review requested due to automatic review settings July 30, 2026 16:26
@matepek
matepek marked this pull request as ready for review July 30, 2026 16:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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].",

Copilot AI review requested due to automatic review settings July 30, 2026 16:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 30, 2026 20:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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—was TYPE_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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed has-tests integration: threshold new-feature Quality Scale: internal small-pr PRs with less than 30 lines. Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants