Skip to content

Instantly share code, notes, and snippets.

@shermozle
Last active December 19, 2024 02:13
Show Gist options
  • Save shermozle/2c8e833d8fe210a8da7d03d372eea734 to your computer and use it in GitHub Desktop.
Save shermozle/2c8e833d8fe210a8da7d03d372eea734 to your computer and use it in GitHub Desktop.
zest.rb change to check battery levels in Home Assistant and only switch if battery is full
diff --git a/zest.rb b/zest.rb
index 5177f85..8492a7b 100755
--- a/zest.rb
+++ b/zest.rb
@@ -65,11 +65,30 @@ enphase_manager = Zest::Enphase::Manager.new(
post_switch_custom_command: ENV.fetch('ZEST_COMMAND_TO_RUN_AFTER_SWITCHING_GRID_PROFILE', nil),
)
+@logger = logger
+def battery_full?
+ # How to use HTTPX:
+ # https://honeyryderchuck.gitlab.io/httpx/wiki/home.html
+ response = HTTPX
+ .with_headers('Accept' => 'application/json')
+ .plugin(:auth)
+ .bearer_auth(ENV.fetch('ZEST_HOME_ASSISTANT_TOKEN', nil))
+ .get(ENV.fetch('ZEST_HOME_ASSISTANT_URL', nil))
+ response.raise_for_status
+ parsed_body = JSON.parse(response.body.to_s)
+ # You can use Pry for debugging:
+ # require 'pry'; binding.pry
+ charge = parsed_body.fetch('state').to_f
+ @logger.info('Battery charge: %.0f%%' % charge)
+ charge > 97
+
+end
+
amber_poll_interval_seconds = Float(ENV.fetch('ZEST_AMBER_POLL_INTERVAL_SECONDS'))
loop do
begin
- if amber_client.costs_me_to_export?
+ if amber_client.costs_me_to_export? && battery_full?
enphase_manager.set_export_limit_to_zero
else
enphase_manager.set_export_limit_to_normal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment