Skip to content
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

Add a Home Assistant Entity to Manually Force a Tariff Comparison #1994

Open
stefanwest1 opened this issue Feb 10, 2025 · 2 comments
Open

Add a Home Assistant Entity to Manually Force a Tariff Comparison #1994

stefanwest1 opened this issue Feb 10, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@stefanwest1
Copy link

stefanwest1 commented Feb 10, 2025

**Is your feature request related to a problem? Please describe.
Currently, there is a "Force Comparison" button available in the Web UI. However, for users running PredBat in an AppDaemon container, the Web UI is not accessible. This means there is no easy way to trigger a tariff comparison from within Home Assistant.

Describe the solution you'd like
I would like a dedicated Home Assistant entity (e.g., input_boolean.predbat_force_comparison or button.predbat_force_comparison) that allows manually triggering a tariff comparison.

This entity should be available in Home Assistant so that automations, scripts, and UI interactions can trigger a tariff comparison.
When activated, it should set self.compare_tariffs = True inside PredBat, ensuring the comparison runs in the next cycle.

Describe alternatives you've considered

  • Manually restarting the AppDaemon container (which forces a comparison on startup after manually setting self.compare_tariffs = True on restart, but is inefficient).

  • Using Node-RED to call a custom API (but this requires modifying the AppDaemon script).

  • Using AppDaemon’s Web UI (but it is not accessible for users running PredBat inside a Home Assistant appdaemon container).

Additional context
This feature would be extremely useful for those of us running AppDaemon as a separate container, as it would allow direct control from Home Assistant without relying on external modifications or workarounds.

@stefanwest1
Copy link
Author

Just to add, as a workaround for now. Under def initialize(self) in predbat.py I added

self.listen_state(self.force_comparison_from_ha, "input_button.predbat_force_comparison")

And then at the bottom of predbat.py

    def force_comparison_from_ha(self, entity, attribute, old, new, kwargs):
        """
        When the Home Assistant button is pressed, set self.compare_tariffs = True.
        """
        self.log("Home Assistant button pressed: Forcing tariff comparison")
        self.compare_tariffs = True

And finally I just manually created input_button.predbat_force_comparison in HA and this seems to work well for now

@stefanwest1
Copy link
Author

stefanwest1 commented Feb 10, 2025

Just another little update. In compare.py I also changed

end_record = int((my_predbat.minutes_now + 24 * 60 + 29) / 30) * 30 - my_predbat.minutes_now

to:

# Ensure comparison runs for tomorrow's full day
now = datetime.now()
tomorrow = now + timedelta(days=1)
midnight_tomorrow = datetime(tomorrow.year, tomorrow.month, tomorrow.day, 0, 0)

# The number of minutes from midnight tomorrow until 23:59 tomorrow
minutes_full_day = (24 * 60) - 1  # 1439 minutes (00:00 to 23:59)

# Ensure end_record only covers tomorrow (from 00:00 to 23:59)
end_record = int((minutes_full_day + 29) / 30) * 30


My hope is, that when the comparison runs, it will give me an estimated price from midnight tonight, to 11:59pm tomorrow. My goal is that once tomorrow's agile prices are released, I can have it run a comparison straight away, and then see which tariff is cheaper tomorrow, and switch to it.

This one was just more as a test as well, but under def run_scenario(self, end_record), I added

     self.pb.soc_kw = 0
     self.pb.soc_percent = 0

This is more as a "worst case scenario" type thing, and have it assume the battery is empty at the start of the comparison

springfall2008 added a commit that referenced this issue Feb 15, 2025
@springfall2008 springfall2008 added the enhancement New feature or request label Feb 15, 2025
springfall2008 added a commit that referenced this issue Feb 15, 2025
* Compare active entity

#1994

* [pre-commit.ci lite] apply automatic fixes

* Update compare.md

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants