Help with a rule that triggers based on month

heres my rule. (The code boxes are missing from the comment window so sorry for the formatting) it triggers and then nothing based upon the if statements based upon month.

"rule “Auto Solar Power Usage”

when
Item Inverter_Generation_Combined changed or
Item Inverter_1_Grid changed
then
createTimer(now.plusSeconds(15), [ |
if (Inverter_1_Grid.state < -3000 && Hot_water_Element_2.state !== OFF)

			{
				Hot_water_Element_2.sendCommand(OFF)
				Hot_water_Element_1.sendCommand(OFF)
			}
	if		(Inverter_1_Grid.state < -5000 && Force_Spa_Heat.state !== ON)
			{
				if(now.getMonth = 1) {
					Spa_Target_Temp.sendCommand(38)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 38")
				}
				if(now.getMonth = 2) {
					Spa_Target_Temp.sendCommand(38)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 38")
				}
				if(now.getMonth = 3) {
					Spa_Target_Temp.sendCommand(39)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 39")
					sendNotification("rick@winterandsonroofing.co.nz", "Spa Set To 39.")
				}
				if(now.getMonth = 4) {
					Spa_Target_Temp.sendCommand(39)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 39")
				}
				if(now.getMonth = 5) {
					Spa_Target_Temp.sendCommand(39)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 39")
				}
				if(now.getMonth = 6) {
					Spa_Target_Temp.sendCommand(40)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 40")
				}
				if(now.getMonth = 7) {
					Spa_Target_Temp.sendCommand(40)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 40")
				}
				if(now.getMonth = 8) {
					Spa_Target_Temp.sendCommand(40)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 40")
				}
				if(now.getMonth = 9) {
					Spa_Target_Temp.sendCommand(39)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 39")
				}
				if(now.getMonth = 10) {
					Spa_Target_Temp.sendCommand(39)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 39")
				}
				if(now.getMonth = 11) {
					Spa_Target_Temp.sendCommand(39)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 39")
				}
				if(now.getMonth = 12) {
					Spa_Target_Temp.sendCommand(38)
					logInfo("Auto Solar Power Usage", "Set Spa Temp To 38")
				}
			}
	if		(Inverter_1_Grid.state > 1000 && Force_Hot_Water_HEAT.state !== ON)
			{
			Hot_water_Element_2.sendCommand(ON)
			Hot_water_Element_1.sendCommand(ON)
			}
	if		(Inverter_1_Grid.state > 1000 && Force_Spa_Heat.state !== ON && Spa_Target_Temp.state !== "33")
			{
			Spa_Target_Temp.sendCommand(30.0)
			}
])

now.getMonth returns the name of the month. If you were to run

logInfo("GetMonth Test",now.getMonth.toString)

the result would be ‘March’

In your case you want now.getMonthValue which returns integers from 1 to 12.

You will also need to use == instead of just = in your comparisons.

You may also be interested in replacing your series of if statements with slightly more streamlined code. Search for switch - case examples here in the forums.

1 Like

Alternatively, match the string value. Whatever the case, I agree with @JustinG on the case statement. You only have three actual outcomes.

Out of curiousity, why are you differentiating by one degree based on month? That seems somewhat arbitrary. Have you considered using an ambient temperature sensor or openweathermap, so that you can adjust the spa temp based on current environmental conditions?

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.