How to condition based on month in a rule?

I’m trying to create a rule that triggers within a specific time range (using between), but only for a specific range of months. I can’t find examples of that, and the API rules documentation is well, silent of any syntax for that. Is this possible, and if so can anyone point me to examples or correct syntax?

Here is day example (also between)

I think that it’s possible to use month instead of day.

Are you looking for something like this?

I ran across that in my earlier search, but did not see the month check I was looking for.

What I want is a rule that turns on a fan when the temp exceeds a particular temperature, but only during a daily time window AND only from April to September. The last bit is what I was struggling to figure out.

So you’re saying something like this (with desired month numbers and change to between) should work:

          {
            "equals": {
              "left": {
                "date": {
                  "reference": "Today"
                }
              },
              "right": {
                "date": {
                  "month": 1
                }
              }
            }
          },

A couple options:
The april to sept you could do with Mariano’s calendar driver. It would act like a virtual that was only ON during your April to September months. Then you’d just need a simple routine for the rest.

If outside temp > 70 AND virtual ON AND TIME between X and Y 
THEN
turn on fan

Or you could do it in rules api with a single rule:

{
  "name": "Seasonal temperature-controlled fan",
  "timeZoneId": "America/New_York",
  "actions": [
    {
      "if": {
        "and": [
          {
            "changes": {
              "id": "temperature-crossed-threshold",
              "greaterThan": {
                "left": {
                  "device": {
                    "devices": ["TEMPERATURE-SENSOR-DEVICE-ID"],
                    "component": "main",
                    "capability": "temperatureMeasurement",
                    "attribute": "temperature",
                    "trigger": "Always"
                  }
                },
                "right": {
                  "decimal": 24.0
                }
              }
            }
          },
          {
            "between": {
              "value": {
                "date": {
                  "reference": "Today"
                }
              },
              "start": {
                "date": {
                  "month": 4,
                  "day": 1
                }
              },
              "end": {
                "date": {
                  "month": 9,
                  "day": 30
                }
              }
            }
          },
          {
            "between": {
              "value": {
                "time": {
                  "reference": "Now"
                }
              },
              "start": {
                "time": {
                  "reference": "Midnight",
                  "offset": {
                    "value": {
                      "integer": 8
                    },
                    "unit": "Hour"
                  }
                }
              },
              "end": {
                "time": {
                  "reference": "Midnight",
                  "offset": {
                    "value": {
                      "integer": 22
                    },
                    "unit": "Hour"
                  }
                }
              }
            }
          }
        ],
        "then": [
          {
            "command": {
              "devices": ["FAN-DEVICE-ID"],
              "commands": [
                {
                  "component": "main",
                  "capability": "switch",
                  "command": "on",
                  "arguments": []
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

Replace:

  • TEMPERATURE-SENSOR-DEVICE-ID
  • FAN-DEVICE-ID
  • 24.0 with your temperature threshold
  • 8 and 22 with your desired start and end hours

Note that both of these options only respond when the temperature crosses the threshold. If it was already above before 8:00 AM, the fan will not necessarily turn on exactly at 8:00 AM. Likewise, this rule alone does not turn the fan off when the temperature falls or the window ends.

For dependable operation, I recommend adding two more actions/rules:

  1. At the window’s start, check the season and current temperature, then turn the fan on if appropriate.
  2. Turn the fan off when the temperature drops below a lower threshold or the daily window ends.

Using a lower shutoff threshold—for example, on above 24°C and off below 23°C—also prevents rapid on/off cycling near the threshold.

Personally I would do it as much as i can in routines to make things easier. You could just do the calendar part acting on a virtual switch with rules api and the rest as option 1 above.

{
  "name": "April through September virtual switch",
  "timeZoneId": "America/New_York",
  "actions": [
    {
      "every": {
        "specific": {
          "month": 4,
          "day": 1,
          "reference": "Midnight"
        },
        "actions": [
          {
            "command": {
              "devices": ["VIRTUAL-SWITCH-DEVICE-ID"],
              "commands": [
                {
                  "component": "main",
                  "capability": "switch",
                  "command": "on",
                  "arguments": []
                }
              ]
            }
          }
        ]
      }
    },
    {
      "every": {
        "specific": {
          "month": 10,
          "day": 1,
          "reference": "Midnight"
        },
        "actions": [
          {
            "command": {
              "devices": ["VIRTUAL-SWITCH-DEVICE-ID"],
              "commands": [
                {
                  "component": "main",
                  "capability": "switch",
                  "command": "off",
                  "arguments": []
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

Good suggestion. I just recently started moving some existing stuff to rules, and felt like learning some of the techniques would be beneficial, but soon found the number of workarounds needed for moderately simple stuff to be annoying.

I successfully use the calendar driver in combination with a virtual switch to indicate BST for routines. lt is reliable and fairly simple to set up.

On (BST :sun:):

and Off (GMT :cloud:):

The calendar driver worked fine for my routine. I had forgotten about it, and in fact had used in in a different routine long ago - so it was installed and ready for this additional task. Thanks everyone!