8000 Baysesian Config Flow by HarvsG · Pull Request #122552 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content

Baysesian Config Flow #122552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 75 commits into
base: dev
Choose a base branch
from
Open

Baysesian Config Flow #122552

wants to merge 75 commits into from

Conversation

HarvsG
Copy link
Contributor
@HarvsG HarvsG commented Jul 24, 2024

Proposed change

Implement a config flow for the Bayesian Helper
Will likely require #117355 Not required
Since this may increase the user base I am keen to merge #119281 first as it is breaking. ✅ DONE

Explanation for reviewers:
The Bayesian helper is used to create a virtual binary sensor that gets its state by from a combination of other sensor states. For example a user could predict whether binary_sensor.bayesian_cooking is on from a variety of correlated, but imperfect sensors sensor.kitchen_humidity, {{(sensor.kitchen_temperature - sensor.avergae_temperature) > 3}}, sensor.gas_usage, sensor.electricity_use_power, binary_sensor.no_one_home
Rather than using "if this, then that" logic, Bayesian uses a statistical/probability technique called Bayes' Rule.
Essentially, the user configures these imperfect sensors and an expected states e.g. binary_sensor.no_one_home == True and provides the probability of these being True if bayesian binary_sensor.bayesian_cooking is on and if it is off. Each of these is known as an observation and in nearly all cases users will specify >1 observation for each Bayesian binary sensor. Observations may be configured as state (checks for exact matching of a state), numeric_state, which checks if a sensor is in a range and template which evaluates the template and coerces it into a Boolean.
There are also variables which apply to the Bayesian binary sensor rather than per-observation; prior and probability_threshold. prior is the baseline probability that sensor should be on, and probability_threshold is the probability at which the sensor would be considered on.

Because of this one-to-many relationship of Bayesian binary sensors to their constituent observations this is a complex ConfigFlow. A similar thing has been achieved before by the 'Scrape' component.

ConfigFlow Example video

OptionsFlow Example video

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

Checklist

  • 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.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@gjohansson-ST
Copy link
Member

I don't think this should use subentries as in managing observations (which I think is the driver here).
It should all be handled by an OptionsFlow perhaps built similar to how scrape is done currently.

@HarvsG
Copy link
Contributor Author
HarvsG commented Jul 25, 2024

I don't think this should use subentries as in managing observations (which I think is the driver here). It should all be handled by an OptionsFlow perhaps built similar to how scrape is done currently.

Thank you for this advice, perhaps you could explain in a little more detail? I have had a look at the scape component.

Each Bayesian sensor may have none, one or many of a template, state or numeric_state. Those themselves have between 3 and 6 config options. How should I allow a user to keep adding more? Should I just allow the addition of multiple entities and templates and then ask them to configure the to_state, prob_given_false for each etc on the next page?

@HarvsG

This comment was marked as outdated.

STATE_SUBSCHEMA = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
selector.EntitySelectorConfig(domain=[SENSOR_DOMAIN, BINARY_SENSOR_DOMAIN])
Copy link
Contributor Author
@HarvsG HarvsG Aug 5, 2024

Choose a reason for hiding this comment

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

Are these the only sensor domains we want to support? Or is this too constraining?

Copy link
Contributor Author
@HarvsG HarvsG Nov 12, 2024

Choose a reason for hiding this comment

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

In 5b0d09f I have enabled many more domains in the absence of a centrally managed list of all possible domains. Broadly I have enabled all domains for state observations that a user could reasonably predict e.g those that are not timestamps
For numeric domains I have included all those that I can find which have a state that is usually a number.

@HarvsG

This comment was marked as resolved.

@home-assistant home-assistant bot marked this pull request as draft November 11, 2024 19:32
@HarvsG HarvsG marked this pull request as ready for review November 11, 2024 23:57
@HarvsG
Copy link
Contributor Author
HarvsG commented Nov 12, 2024

Maybe you can provide some images or a vid in the PR description how this is working so we can see the flow makes sense.

I will do this tomorrow

Edit: Done, videos uploaded to YT as they are too large for github. Links in the Proposed changes.

@HarvsG HarvsG requested a review from black-roland February 10, 2025 19:58
Copy link
@black-roland black-roland left a comment

Choose a reason for hiding this comment

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

Probably I’m not qualified to comment on the code itself, but I’ve tested the latest changes and can confirm that Bayesian sensor creation is working properly. The binary sensors issue has also been resolved. Great work, thank you!

Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Apr 18, 2025
@joostlek
Copy link
Member

Not stale

@github-actions github-actions bot removed the stale label Apr 19, 2025
@HarvsG
Copy link
Contributor Author
HarvsG commented May 30, 2025

#117355 Is merged and subconfig entries are enabled, so I may re-write

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

Successfully merging this pull request may close these issues.

5 participants
0