Time Based State Machine [4.0.0.0;5.9.9.9]

If you don’t need different times for different types of days this might be a little overkill for you. You can set the DateTime Items states and then use the Time is <item> trigger on a rule to change the states.

  • Trigger the rule with each of the four time of day Items using the Time is Item trigger to use the time only and ignore the date so you don’t need to update the Item every day
  • The script action in JS for a managed rule would be something like:
var tod = event.itemName.replace("Default_", "").toUpperCase();
items.TimeOfDay.sendCommandIfDifferent(tod);

In Blockly the script action would look something like

What do you mean you can’t find it. It’s not showing up in the Add-on store? It should be listed under Add-on Store → Automation → Rule Templates

Click “Add” and verify that it installs (the “add” will change to “remove”). If if fails you’ll get a toast telling you it failed and you’ll see why in openhab.log.

Then you need it instantiate a rule based on the template.

  1. Navigate to Settings → Rules → +
  2. Fill out the rule metadata (ID, label, description, etc.).
  3. Choose “Time Based State Machine” as the template and fill out the form

Choose your “TimeOfDay” Item from your list of Items for the first one, your “TimesOfDay” Item from the list of Items for the second one and enter “etod” for the Time of Day Namespace.

  1. Click save

Watch the logs. The rule will run automatically when you hit save but there is a 10 second delay from when the rule triggers and it actually runs so the rule doesn’t run a bunch at midnight when Astro et. al. are updating Items with the new times for the current day. If there is an error it should generate a meaningful error message in the logs. Otherwise you will see it update TimeOfDay.

2024-07-29 00:05:10.184 [INFO ] [omation.rules_tools.TimeStateMachine] - All etod Items are configured correctly
2024-07-29 00:05:10.386 [INFO ] [omation.rules_tools.TimeStateMachine] - Today is a default day.
2024-07-29 05:59:00.000 [INFO ] [omation.rules_tools.TimeStateMachine] - Transitioning Time State Machine from BED to DAY

You don’t need to copy/paste/edit or even look at the code. That’s the whole point of rule templates. However, if you did want to look at or modify the code, you can navigate to the Rule you instantiated based on the template and examine the code that was generated. Once instantiated it just a rule like any other.

All of my rule templates require the JS Scripting add-on and openHAB Rules Tools Announcements.

If you are looking for a file based rule, you will not be able to use rule templates. Rule tempaltes are UI/managed rules only.

I have no plans on creating a file based version of any of my templates (I’ve stepped way back from file based config support, it’s a waste of my time fighting syntax errors). You should be able to look at the code generated though and create your own. The link at the bottom of the OP is the raw rule template code and you can use that too to create your own file based version. Just be aware that in my rule templates roughly half of the code is checking the configuration and other tests to make sure the configuration is correct and generating meaningful and actionable error logs when there is a problem. If you are just wanting something for yourself you can cut a lot of that out.