Trying to submit a PR for forecast.solar for location setting and updates

Hey @Forecast.Solar and @klaasnicolaas ! I’m using the API for use in my RV, I have about 3,000W of solar on my rig that tilts! I subscribed to the API as well as I want to support the project

I live update my location via automation, so my “Zones” latitude and longitude is updated hourly. However, location is statically set during setup and location can’t even be updated via config.

I opened an issue: Forecast.Solar should pull latitude, longitude from HA instead of setting it statically in core.config_entries · Issue #85079 · home-assistant/core · GitHub

I also set up a dev environment and I’m struggling through a PR. It seems that core integrations aren’t well documented, nor is the internal api call that I can retrieve the zone latitude and longitude from.

I figured that at least allowing a user to update the lat, long manually would be nice after initial setup. At most, it would be nice to have a radio button or dropdown to select pulling from HA’s core lat and long from zone (and be updated when it changes).

I don’t mind doing the dev work, I’ll continue struggling along and try to get a PR in, but I’m open to any guidance on how to pull in the lat, long from HA’s zones

I’m also running into the API call limit when rapidly restarting the debugger for testing, a dev key could be nice until I get the PR in if no one else wants to assign

ChatGPT to the rescue!

Ha!

image
Eyyy, it works! Not bad for never having worked on HA!

Now I just need to work on the config flow to allow hass.config.latitude, long as an option

@zorrobyte It looks like your PR was rejected. Did you ever find a long term solution for this?

@zorrobyte I added a new PR at Dynamic Forecast.Solar location option by bencorrado · Pull Request #102616 · home-assistant/core · GitHub

Any luck with this? I read the PR comments and found the insistence that it wouldn’t be useful without azimuth and declination frustrating. As you said; RV’s generally have flat mounted panels; so it’s irrelevant.

Even using zone.home would work; because that’s easy to update in real-time using GPS and most RV’ers who use HA do that anyway.

This is my hacky workaround. I’m sharing it but should probably preface it with “Don’t do this”, since it involves modifying files in .storage which is sort of a no-no. But uh; it works? And… nothing has caught fire yet?

Figured I’d share it though since this thread comes up when you search for this functionality.

I’m using AppDaemon for this script but I’m sure you can modify and apply as needed.

update_forecast_solar.py

import hassapi as hass
import json
import os

class UpdateForecastSolar(hass.Hass):

    def initialize(self):
        self.run_every(self.update_config, "now", 3600)  # Run every hour

    def update_config(self, kwargs):
        latitude = float(self.get_state(self.args["latitude_sensor"]))
        longitude = float(self.get_state(self.args["longitude_sensor"]))
        
        if latitude and longitude:
            self.log(f"Updating forecast.solar config with latitude: {latitude}, longitude: {longitude}")
            config_path = "/homeassistant/.storage/core.config_entries"
            
            try:
                with open(config_path, "r") as file:
                    config = json.load(file)
                
                updated = False
                for entry in config['data']['entries']:
                    if entry['domain'] == 'forecast_solar':
                        current_latitude = entry['data']['latitude']
                        current_longitude = entry['data']['longitude']
                        
                        if current_latitude != latitude or current_longitude != longitude:
                            entry['data']['latitude'] = latitude
                            entry['data']['longitude'] = longitude
                            updated = True
                            self.log(f"Updated entry: {entry['entry_id']} with new coordinates.")
                
                if updated:
                    backup_path = f"{config_path}.bak"
                    os.rename(config_path, backup_path)
                    with open(config_path, "w") as file:
                        json.dump(config, file, indent=4)
                    
                    self.log("Successfully updated forecast.solar config. Please manually restart Home Assistant to apply the changes.")
                else:
                    self.log("No updates made to forecast.solar config.")
                    
            except Exception as e:
                self.log(f"Failed to update forecast.solar config: {str(e)}")

Add this to apps.yaml

update_forecast_solar:
  module: update_forecast_solar
  class: UpdateForecastSolar
  latitude_sensor: sensor.gps_latitude
  longitude_sensor: sensor.gps_longitude

Set the latitude and longitude sensors appropriately.

The script runs once per hour and updates the config file directly. The one thing it doesn’t do is reload the integration and I can’t figure out how to do that any other way than restarting Home Assistant. Hopefully someone smarter than me (It’s a low bar) can come along and share how that might be done efficiently. I have an automation to restart HA every day anyway, and realistically a mid-day change in location is going to throw off solar calculations anyway. So this means that before the start of the next day, if the RV has moved, the Solar Forecast integration will have the correct latitude and longitude.

Hi Ben! Did you give up on the PR? :sweat_smile:
This feature would be great, I think you should mark the PR as “ready for review” as someone pointed out in the comments.