Solcast sendHttpGetRequest

I am trying to use Solcast to forecast solar production for the day so openHAB can decide whether to run the gas water heating in the morning or allow the solar diverter to heat the water.

I have constructed the appropriate url (which is not repeated here because it contains my personal token) from the api documentation and tested it in a browser and I get back a JSON string from which I can parse the HH values. When I try it in a sendHttpGetRequest in a rules DSL file, it returns an empty string. Similarly if I curl it from the command line, I get a null return.

I have tried changing the url for another api that I use (the Octopus Energy tracker price) and it returns a full string both in curl and in the rule.

Is there a reason why a Get request url that works in a browser wouldn’t work in curl or a rule?

Thanks

It could be that the user-agent is veryfied to prevent automated downloads/request.
You can check that by first going to a page that returns your user-agent string that is used by the browser and then run curl with the option to set the user-agent to the same string.

Thanks, I’ll investigate that.

I’ll need to look at how to specify the user agent with the sendHttpGetRequest as I was only using curl for testing.

Thanks for the pointer. In the end it wasn’t the user agent but I did need to add api_key=xxxxxxxxxx to the end of the url. My browser must have been using another form of authentication from when I was logged into the Solcast website.

In case you allow cookies you may have been “authenticated” by one of them.

I suspect so but your tip got me going in the right direction; whilst I was relooking at the API documentation to see if it said anything about headers, I reread the section on authentication, which I bypassed because I had assumed it used the token, which appeared to work in the browser.

Thanks again.

hi ,

just saw that somebody did integrate the solcast with the HTTP binding.
could you share some thing, channel and item definitions to for getting the data from solcast.
that would be a great help to get started.
thx

I didn’t use things or items, just a rule.

This rule checks the forecast production for the day and if it is low, heats the water by gas.

rule SolarForecast
when
	Time cron "0 0 6 ? * MON,TUE,WED,THU,FRI *" or // 6am weekdays
	Time cron "0 0 7 ? * SUN,SAT *" // 7am weekends
then
	val String forecastJSON = sendHttpGetRequest("https://api.solcast.com.au/rooftop_sites/xxxxx/forecasts?format=json&hours=18&api_key=yyyyy")
//	logWarn("rules.SolarForecast", "Solcast: "+forecastJSON)
	var Number Sum = 0
	for (var i = 0 ; i < (35) ; i++) {	//for each HH forecast
		var Number Power = (Double::parseDouble(transform("JSONPATH", "$.forecasts["+i+"].pv_estimate", forecastJSON)))	//extract the rate and convert to a number
		Sum = Sum + Power // Add the value to the sum
//		logWarn("rules.SolarForecast", "Power: "+Power.toString+" Sum: "+Sum.toString)
		}
	Sum = Sum / 2.11 // Estimated constant to account for periods being HH and correction factor
	logWarn("rules.SolarForecast", "Forecast Sum: "+Sum.toString)
	Solar_forecast.sendCommand(Sum)
	if (Sum < 8 ) {
		Hot_Water_Boost.sendCommand(2) // Set hot water on for two hours
		logWarn("rules.SolarForecast", "Hot water boosted by low solar forecast")
		}
end

If you sum up the average production for each half hour period, you need to half it to get to kWh. I’ve also found having looked at how this is performing, I needed to slightly correct the result so there is a correction factor included, which you may need to adjust.

There are a couple of commented out logging functions that you can use during testing to make sure it’s working as expected.

You need to replace xxxxx and yyyyy in the URL with your site ID and API key respectively. Both can be found by logging into the Solcast web portal.

1 Like

many thanks for sharing, really helpful

I further looked into it and understand that you are not pushing any forecast data to the item .
i saw in another solution that data can be pushed with rules directly to influx.

What i was looking for is a solution to post updates of the data coming from solcast and post the item (forecast) status with a timestamp. I have tried to find info if the postupdate provide interface to include the timestamp. Unfortunately not.
Having the data in the persistence of the item would allow to access the data in other rules.
It would also allow to make the data visualized in the charts oh OH UI.

Anybody know a solution to postupdate of an item with time information?
would the data pushed into influx in the rule be on the same data item as the item defined in OH make it possible to access the data in OH UI charts via the influx persistent?
This all would only work with influx and not necessarily with other persistence in OH.

I am just aggregating the whole day forecast and using it to decide whether to heat the water with gas before the morning use.

I have looked at storing timestamped values for the Octopus Agile electricity prices but I haven’t succeeded. From what I have found so far it seems better to use the InfluxDB API directly rather than try to go through OpenHAB.