Automatic Shading with Astro, OpenWeather and MqTT (for Shelly 2.5)

here you are all englished files. hope the translation is approx ok :wink:
this should be okay, i’ve formatted datetime and corrected some lines around here and there.

shading.items

Group		gRolloautomatik				"Gruppo Tapparelle Ovest"

Switch		Rolloautomatik																					//"Automatic blinds closing"
Switch		Rolloautomatik_Open																				//"Inverts blinds Closing with opening"

Number		Rolloautomatik_Closure																			//"Blinds closed at"
Number		Rolloautomatik_Temperature																		//"Temperature higher than: [%1$.0f]"
Number		Rolloautomatik_Cloudiness																		//"Cloudiness higher than: [%d]"
Number		Rolloautomatik_Azimuth																			//"Sun Azimuth higher or equal to: [%d]"
Number		Rolloautomatik_Elevation																		//"Sun Elevation higher or equal to: [%d]"

DateTime	Rolloautomatik_Last_Start																		//"Last run (Close) [%1$tH:%1$tM %1$td/%1$tm/%1$tY]"
DateTime	Rolloautomatik_Last_End																			//"Last run (Open) [%1$tH:%1$tM %1$td/%1$tm/%1$tY]"

Number		External_Temperature		{weather="locationId=home, type=temperature, property=current"}		//"Temperature [%.0f]"
Number		Local_Cloudiness			{weather="locationId=home, type=clouds, property=percent"}			//"Clouds [%.0f]"
Number		Azimuth						{channel="astro:sun:home:position#azimuth"}							//"Azimuth [%.0f]"
Number		Elevation					{channel="astro:sun:home:position#elevation"}						//"Elevation [%.0f]"

shading.rules

var boolean log = true
rule "Blinds closure"
when Item Azimuth changed
then
    val String logPrefix = 'Autoblinds (Blinds closure) - '
    if (log) logInfo('rules', logPrefix + 'Rule loaded!')
    
    var String timeLast = 'xxxx-xx-xx'
    if (Rolloautomatik_Last_Start.state == NULL) 
		{
			if (log) logInfo('rules', logPrefix + 'First execution on this system.')
		} 
	else 
		{
			timeLast = Rolloautomatik_Last_Start.state.toString().substring(0,10)
		}

var String timeNow = now.toString().substring(0,10)

    if (Rolloautomatik.state == ON) 
		{
			if (timeNow != timeLast) 
				{
					if (Azimuth.state > Integer::parseInt(Rolloautomatik_Azimuth.state.toString())) 
						{
							if (External_Temperature.state > Integer::parseInt(Rolloautomatik_Temperature.state.toString())) 
								{
									if (Local_Cloudiness.state  >= Integer::parseInt(Rolloautomatik_Cloudiness.state.toString())) 
										{
											if (Elevation.state > Integer::parseInt(Rolloautomatik_Elevation.state.toString())) 
												{
													if (log) logInfo('rules', logPrefix + 'Blinds have been closed.') gRolloautomatik.members.forEach
													[i| if (i.state <= Integer::parseInt(Rolloautomatik_Closure.state.toString())) {if (log) logInfo('rules', logPrefix + 'Blind set to ' + Rolloautomatik_Closure.state.toString() + '%: ' + i.name) i.sendCommand(Integer::parseInt(Rolloautomatik_Closure.state.toString()))} else {if (log) logInfo('rules', logPrefix + 'Blind already closed (' + i.state.toString() + '%) so rule will be skipped')}]
													sendBroadcastNotification("Auto closure enabled!")
													Rolloautomatik_Last_Start.postUpdate(now.toString())
												}
											else 
												{
													if (log) logInfo('rules', logPrefix + 'Sun elevation (' + Elevation.state.toString() + ') has not reached the value set (' + Rolloautomatik_Elevation.state.toString() + ')')
												}
										}
									else
										{
											if (log) logInfo('rules', logPrefix + 'Cloudiness (' + Local_Cloudiness.state.toString() + ') has not reached the value set (' + Rolloautomatik_Cloudiness.state.toString() + ')')
										}
								}
							else
								{
									if (log) logInfo('rules', logPrefix + 'Temperature (' + External_Temperature.state.toString() + ') has not reached the value set (' + Rolloautomatik_Temperature.state.toString() + ')')
								}
						}
					else
						{
							if (log) logInfo('rules', logPrefix + 'Azimuth (' + Azimuth.state.toString() + ') has not reached the value set (' + Rolloautomatik_Azimuth.state.toString() + ')')
						}
				}
			else
				{
					if (log) logInfo('rules', logPrefix + 'Task finished. The rule will not be executes again today.')
				}
		}
	else 
		{
			if (log) logInfo('rules', logPrefix + 'Finished, automation disabled.')
		}
end

//-----------------------------------------------//

rule "Blinds opening"
when Item Elevation changed
then
    val String logPrefix = 'Autoblinds (Blinds opening) - '
    if (log) logInfo('rules', logPrefix + 'Rule loaded!')

    var String timeLastEnde = 'xxxx-xx-xx'
    if (Rolloautomatik_Last_End.state == NULL) 
		{
			if (log) logInfo('rules', logPrefix + 'First execution on this system.')
		} 
	else 
		{
			timeLastEnde = Rolloautomatik_Last_End.state.toString().substring(0,10)
		}

    var String timeLastStart = 'yyyy-yy-yy'
    if (Rolloautomatik_Last_Start.state == NULL) 
		{
			if (log) logInfo('rules', logPrefix + 'First execution on this system.')
		} 
	else 
		{
			timeLastStart = Rolloautomatik_Last_Start.state.toString().substring(0,10)
		}

	var String timeNow = now.toString().substring(0,10)

    if (Rolloautomatik_Open.state == ON) 
		{
			if (Elevation.state <= Integer::parseInt(Rolloautomatik_Elevation.state.toString())) 
				{
					if (timeLastStart == timeNow) 
						{
							if (timeLastEnde != timeNow) 
								{
									if (log) logInfo('rules', logPrefix + 'Blinds opened') gRolloautomatik.members.forEach
									[i| if((Rolloautomatik_Closure.state as Number).intValue <= (i.state as Number).intValue +5 && (Rolloautomatik_Closure.state as Number).intValue >= (i.state as Number).intValue- 5) {if (log) logInfo('rules', logPrefix + 'Blind set to 0%: ' + i.name) i.sendCommand(0)} else {if (log) logInfo('rules', logPrefix + 'Blind not completely opened by script to 0%, because manually managed: ' + i.name)}]
									sendBroadcastNotification("Auto open finished")
									Rolloautomatik_Last_End.postUpdate(now.toString())
								} 
							else 
								{
									if (log) logInfo('rules', logPrefix + 'Task finished. The rule will not be executes again today.')
								}
						} 
					else 
						{
							if (log) logInfo('rules', logPrefix + 'Finished. No task has been executed today. No auto opening can be executed.')
						}
				} 
			else 
				{
					if (log) logInfo('rules', logPrefix + 'Elevation (' + Elevation.state.toString() + ') has not reached the value set (' + Rolloautomatik_Elevation.state.toString()+ ')')
				}
		} 
	else 
		{
			if (log) logInfo('rules', logPrefix + 'Finished, automation disabled.')
		}
end

shading.sitemap

sitemap shading label="Blinds Auto close"

{
	Frame label="Enable automation" 
		{
			Switch item=Rolloautomatik label="Blinds Auto closure"
			Setpoint item=Rolloautomatik_Closure minValue=0 maxValue=100 step=1 label="Blinds closed at: [%s %%]"
        }

	Frame label="Close if..." 
		{
			Setpoint item=Rolloautomatik_Temperature minValue=0 maxValue=35 step=1 icon="temperature" label="Temperature higher than: [%s °C]"
			Text item=External_Temperature icon="temperature" valuecolor=[>27="red",>20="orange",>10="purple",>5="green",<=5="blue"] label="External temperature: [%.0f °C]"
			Setpoint item=Rolloautomatik_Cloudiness minValue=0 maxValue=100 step=20 icon="sun_clouds" label="Cloudiness higher or equal than to: [%.0f %%]"
			Text item=Local_Cloudiness icon="sun_clouds" label="Cloudiness: [%.0f %%]"
			Setpoint item=Rolloautomatik_Azimuth minValue=0 maxValue=360 step=50 icon="niveau" label="Azimuth higher than: [%d °]"
			Text item=Azimuth icon="niveau" label="Azimuth: [%d °]"
		}

	Frame label="Open if..." 
		{
			Switch item=Rolloautomatik_Open label="Enable auto opening"
			Setpoint item=Rolloautomatik_Elevation minValue=-20 maxValue=60 step=1 icon="sun" label="Elevation less than: [%d °]"
			Text item=Elevation icon="sun" label="Elevation: [%d °]"
		}

	Frame label="Info"
		{
			Text item=Rolloautomatik_Last_Start label="Last run (Close) [%1$tH:%1$tM - %1$td/%1$tm/%1$tY]"
			Text item=Rolloautomatik_Last_End label="Last run (Open) [%1$tH:%1$tM - %1$td/%1$tm/%1$tY]"
		}
}

1 Like