Page 1 of 1

solar power prediction

Posted: Saturday 01 February 2025 17:27
by JanJaap
Hey,

Has anyone already built some integration to fetch predicted solar power? Would be interested in light of controlling home batteries. First step would be to compare prediction vs actuals..... Solarpanel.forecast for example seems like a potential target to pull form.

Re: solar power prediction

Posted: Saturday 01 February 2025 17:47
by psubiaco
I don't remeber where I found this dzVents script, but it works. Try it!

Code: Select all

return {
  on = {
    timer = {
        'at *:50'
    },
    httpResponses = {
      'solarforecastEAST' -- must match with the callback passed to the openURL command
    }
  },
  logging = {
    level = domoticz.LOG_INFO,
    marker = 'get solar forecast',
  },
  execute = function(domoticz, item)
      
      local idxSolarForecastCounter=2481  	-- device with 24h forecast: type 'Managed counter', Energy generated
      local idxSolarForecast=2477  			-- device holding the forecast for the next hour: type 'Custom sensor', axis: 'Wh'


    if (item.isTimer) then
		print("EAST")
      domoticz.openURL({
        url = 'https://api.forecast.solar/estimate/watthours/period/45.8812/12.1833/15/-90/2.7',
        method = 'GET',
        callback = 'solarforecastEAST', -- see httpResponses above.
      })
    end

    if (item.isHTTPResponse) then

      if (item.ok) then
        --domoticz.log('item.data ' .. item.data .. '***************************', domoticz.LOG_INFO)
        
        if (item.isJSON) then
            domoticz.utils.dumpTable(item)
            
            local messagetype=item.json.message["type"]
            domoticz.log("message type" .. messagetype, domoticz.LOG_INFO) 
            
            if messagetype=="success" then
                local currentHR=os.date("%Y-%m-%d %H:00:00")
                local oneHRahead=os.date("%Y-%m-%d %H:00:00",os.time()+1*60*60)
                local twoHRahead=os.date("%Y-%m-%d %H:00:00",os.time()+2*60*60)
                local forecastCurrentHR=tonumber(item.json.result[currentHR])
                if forecastCurrentHR==nil then
                    forecastCurrentHR=0 
                end    
                local forecastOneHR=tonumber(item.json.result[oneHRahead])
                if forecastOneHR==nil then
                    forecastOneHR=0 
                end    
                local forecastTwoHR=tonumber(item.json.result[twoHRahead])    
                if forecastTwoHR==nil then
                    forecastTwoHR=0 
                end    
                domoticz.log("solar forecast for next three hours :" .. forecastCurrentHR .. "+" .. forecastOneHR .. " + " .. forecastTwoHR .. " WattHR", domoticz.LOG_INFO) 
                domoticz.devices(idxSolarForecast).updateCustomSensor(forecastOneHR)
                
                local updateHour=0
                for id = 1, 24 do
                    if id<10 then
                        updateHour="0"..tostring(id)
                    else 
                        updateHour=tostring(id)
                    end    
                    domoticz.devices(idxSolarForecastCounter).updateHistory(os.date("%Y-%m-%d ")..updateHour..":00:00","0;0")
                    domoticz.devices(idxSolarForecastCounter).updateHistory(os.date("%Y-%m-%d ",os.time()+24*60*60)..updateHour..":00:00","0;0")
                end    
                    
                local response=item.json.result
                for datehour,value in pairs(response) do
                    domoticz.log("solar forecast date "..domoticz.utils.stringSplit(datehour)[1].." hour "..domoticz.utils.stringSplit(domoticz.utils.stringSplit(datehour)[2],":")[1].." value "..value,domoticz.LOG_INFO)
                    local previousHour=domoticz.utils.stringSplit(domoticz.utils.stringSplit(datehour)[2],":")[1]-1
                    if previousHour<10 then 
                        previousHour="0"..tostring(previousHour)
                    else
                        previousHour=tostring(previousHour)
                    end    
                    domoticz.log("previousHour "..previousHour)    
                    if value>0 then
                        sensorDateHour=domoticz.utils.stringSplit(datehour)[1].." "..domoticz.utils.stringSplit(domoticz.utils.stringSplit(datehour)[2],":")[1]..":00:00"
                        sValueStr="0;"..value
                        domoticz.log("sensorDateHour "..sensorDateHour.." sValueStr "..sValueStr,domoticz.LOG_INFO)
                        domoticz.devices(idxSolarForecastCounter).updateHistory(sensorDateHour,sValueStr)
                        if sensorDateHour==currentHR then
                            domoticz.devices(idxSolarForecastCounter).updateCounter(value)
                        end    
                    end       
                end
            else
                domoticz.log("no successfull message", domoticz.LOG_INFO)
            end    
        else
            domoticz.log('is not json', domoticz.LOG_INFO) 
        end    
      else
        domoticz.log('There was a problem handling the request', domoticz.LOG_INFO)
        domoticz.log(item, domoticz.LOG_INFO)
      end

    end

  end
}


Re: solar power prediction

Posted: Saturday 01 February 2025 20:02
by JanJaap
Tnx! Looks simple enough, might convert it to plugin.

Re: solar power prediction

Posted: Saturday 01 February 2025 20:09
by psubiaco
JanJaap wrote: Saturday 01 February 2025 20:02 Tnx! Looks simple enough, might convert it to plugin.
Wow... keep us informed if you'll write a plugin doing that! Thanks a lot

Re: solar power prediction

Posted: Sunday 02 February 2025 17:04
by willemd
here is my dzVents script to load solar forecast for my location (I replaced my location with XXXX an YYYY) and my panel specs (angles and capacity)
Please check the API specification for the other parameters in the call.

Code: Select all

return {
	on = {
		timer = {
		    'every hour'
		},
		httpResponses = {
			'solarforecast' -- must match with the callback passed to the openURL command
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'get solar forecast',
	},
	execute = function(domoticz, item)
	    
	    local idxSolarForecast=106  -- device holding the forecast for the next hour
	    local idxSolarForecastCounter=186

		if (item.isTimer) then
			domoticz.openURL({
				url = 'https://api.forecast.solar/estimate/watthours/period/XXXX/YYYY/55/58/2.75',
				method = 'GET',
				callback = 'solarforecast', -- see httpResponses above.
			})
		end

		if (item.isHTTPResponse) then

			if (item.ok) then
				--domoticz.log('item.data ' .. item.data .. '***************************', domoticz.LOG_INFO)
				
				if (item.isJSON) then
				    domoticz.utils.dumpTable(item)
				    
				    local messagetype=item.json.message["type"]
				    domoticz.log("message type" .. messagetype, domoticz.LOG_INFO) 
				    
				    if messagetype=="success" then
				        local currentHR=os.date("%Y-%m-%d %H:00:00")
				        local oneHRahead=os.date("%Y-%m-%d %H:00:00",os.time()+1*60*60)
    				    local twoHRahead=os.date("%Y-%m-%d %H:00:00",os.time()+2*60*60)
    				    local forecastCurrentHR=tonumber(item.json.result[currentHR])
    				    if forecastCurrentHR==nil then
    				        forecastCurrentHR=0 
    				    end    
    				    local forecastOneHR=tonumber(item.json.result[oneHRahead])
    				    if forecastOneHR==nil then
    				        forecastOneHR=0 
    				    end    
    				    local forecastTwoHR=tonumber(item.json.result[twoHRahead])    
    				    if forecastTwoHR==nil then
    				        forecastTwoHR=0 
    				    end    
    				    domoticz.log("solar forecast for next three hours :" .. forecastCurrentHR .. "+" .. forecastOneHR .. " + " .. forecastTwoHR .. " WattHR", domoticz.LOG_INFO) 
    				    domoticz.devices(idxSolarForecast).updateCustomSensor(forecastOneHR)
    				    
    				    -- set previous forecast to zero on managed counter
    				    local updateHour=0
    				    for id = 1, 24 do
    				        if id<10 then
    				            updateHour="0"..tostring(id)
    				        else 
    				            updateHour=tostring(id)
    				        end    
    				        domoticz.devices(idxSolarForecastCounter).updateHistory(os.date("%Y-%m-%d ")..updateHour..":00:00","0;0")
    				        domoticz.devices(idxSolarForecastCounter).updateHistory(os.date("%Y-%m-%d ",os.time()+24*60*60)..updateHour..":00:00","0;0")
    				    end    
    				    
    				    -- load new forecast onto managed counter    
    				    local response=item.json.result
				        for datehour,value in pairs(response) do
				            domoticz.log("solar forecast date "..domoticz.utils.stringSplit(datehour)[1].." hour "..domoticz.utils.stringSplit(domoticz.utils.stringSplit(datehour)[2],":")[1].." value "..value,domoticz.LOG_INFO)
				            local previousHour=domoticz.utils.stringSplit(domoticz.utils.stringSplit(datehour)[2],":")[1]-1
				            if previousHour<10 then 
				                previousHour="0"..tostring(previousHour)
				            else
				                previousHour=tostring(previousHour)
				            end    
				            domoticz.log("previousHour "..previousHour)    
				            if value>0 then
				                sensorDateHour=domoticz.utils.stringSplit(datehour)[1].." "..domoticz.utils.stringSplit(domoticz.utils.stringSplit(datehour)[2],":")[1]..":00:00"
				                sValueStr="0;"..value
				                domoticz.log("sensorDateHour "..sensorDateHour.." sValueStr "..sValueStr,domoticz.LOG_INFO)
				                domoticz.devices(idxSolarForecastCounter).updateHistory(sensorDateHour,sValueStr)
				                if sensorDateHour==currentHR then
				                    domoticz.devices(idxSolarForecastCounter).updateCounter(value)
				                end    
				            end    
				        end
    				else
    				    domoticz.log("no successfull message", domoticz.LOG_INFO)
    				end    
				else
				    domoticz.log('is not json', domoticz.LOG_INFO) 
				end    
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_INFO)
				domoticz.log(item, domoticz.LOG_INFO)
			end

		end

	end
}


Re: solar power prediction

Posted: Monday 03 February 2025 21:07
by JanJaap
ok I can fetch data, so far so good. As I never use the dzEvents I'm a bit puzzled to how the sensor update works. Is the 24 hr forecast counter updated per hour? So only one hour form now? Or is it always giving 24 hrs ahead but all hours being updated as time passes?

See first part: JanJaapKo/SolarForecast

Re: solar power prediction

Posted: Tuesday 11 March 2025 16:19
by erwindob
@JanJaap
Looks promising,
I see the values in my log, but not yet in the devices. Keeping an eye on updates.

Re: solar power prediction

Posted: Tuesday 11 March 2025 19:48
by Toulon7559
Try this weblink as introduction & explanation for the scripts earlier in this thread.