# Design Pattern: Simple State Machine (e.g. Time Of Day)

**URL:** <https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407>\
**Category:** Tutorials & Examples\
**Tags:** jodatime, designpattern, time\
**Created:** [October 17, 2016, 9:09pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407 "2016-10-17T21:09:34Z")\
**Posts on this page:** 20\
**Page:** 32

<div class="post-metadata">

**Author:** ![ljsquare](https://community.openhab.org/user_avatar/community.openhab.org/ljsquare/32/26819_2.png) [@ljsquare](https://community.openhab.org/u/ljsquare)\
**Post date:** [February 10, 2021, 3:55pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/628 "2021-02-10T15:55:42Z")

</div>

> [@rlkoshak](#):
>
> In my opinion, the JavaScript/Python versions are significantly better and more flexible compared to the Rules DSL version and I’m inclined to deprecate the Rules DSL version entirely.

Thx @rlkoshak for your answer. I understand now the difference and I’m also think that the JavaScript/Python versions are better, despite the “old feature” I miss. I’m going to test it with the Astro binding as you said.  
Keep up the good work 👍🏻

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [February 10, 2021, 4:09pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/629 "2021-02-10T16:09:11Z")

</div>

> [@rlkoshak](#):
>
> Have you filed an issue?

Long ago. No replies there nor a fix.  
Does that happen to you, too, or is it just me?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 10, 2021, 4:21pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/630 "2021-02-10T16:21:53Z")

</div>

> [@Tron](#):
>
> Does that happen to you, too, or is it just me?

It’s all volunteer effort. Filing an issue is no guarantee that the issue will be fixed. Someone must volunteer their time to fix it. And if it’s a problem that is difficult to reproduce, or the issue doesn’t have enough detail for the developer to reproduce, it may never get fixed.

Sometimes you can sweeten the pot by filing a bounty source on the issue. Sometimes a little bit of money will help convince someone to fix an issue or implement a feature request.

But at this point you’ve done your part and that’s about all you can to.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [February 10, 2021, 7:03pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/631 "2021-02-10T19:03:44Z")

</div>

Sorry, that’s not what I meant.  
I just wondered if your Ephemeris settings also get screwed up when you restart openhab.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 10, 2021, 7:15pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/632 "2021-02-10T19:15:35Z")

</div>

> [@Tron](#):
>
> I just wondered if your Ephemeris settings also get screwed up when you restart openhab.

Oh, no. I’ve not had any problems with Ephemeris. Though I’ve only defined weekdays/weekends and my location to get holidays.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [February 10, 2021, 8:51pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/633 "2021-02-10T20:51:24Z")

</div>

Strange. It’s the location that gets deleted every time, and that prevents the script from running.  
Do you know if I can define that in a config file somewhere in OH3?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [February 10, 2021, 9:31pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/634 "2021-02-10T21:31:54Z")

</div>

I’m sure there is but I’ve never looked into that. Probably ephemeris.cfg. You can set it and then look in /var/lib/openhab/config/org/openhab/ for the ephemeris.config file and see what the cfg file should include.

One thing though that is important is always select from the list, don’t type in a region. If there is no drop down (e.g. there is no city options for my region) don’t enter anything.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [February 10, 2021, 10:11pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/635 "2021-02-10T22:11:34Z")

</div>

That’s the thing, after a restart the dropdown is empty.  
I’ll have a look at the config file. Maybe that cures it.

---

<div class="post-metadata">

**Author:** ![curlyel](https://community.openhab.org/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@curlyel](https://community.openhab.org/u/curlyel)\
**Post date:** [March 7, 2021, 6:19am UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/636 "2021-03-07T06:19:28Z")

</div>

> [@Tron](#):
>
> happen to you, too, or is it just me?

No. You’re not alone. I see the same. Every time I restart openHAB I have to re-select the country …

> [@Tron](#):
>
> I’ll have a look at the config file. Maybe that cures it.

Curious to hear the results…

---

<div class="post-metadata">

**Author:** ![curlyel](https://community.openhab.org/letter_avatar_proxy/v4/letter/c/71e660/32.png) [@curlyel](https://community.openhab.org/u/curlyel)\
**Post date:** [March 7, 2021, 6:24am UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/637 "2021-03-07T06:24:52Z")

</div>

> [@Tron](#):
>
> > [@rlkoshak](#):
> >
> > Have you filed an issue?
> 
> Long ago. No replies there nor a fix.

Would you share the link to the issue? Were not able to find it…

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [March 7, 2021, 2:16pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/638 "2021-03-07T14:16:18Z")

</div>

There wasn’t a config file for ephemeris, and creating one didn’t change anything, so that wasn’t the answer. I currently rely on the DSL version of the script, which does not use ephemeris, so that’s currently working for me.

The issue I opened was this: [Ephemeris losing entered data on restart · Issue #2024 · openhab/openhab-core · GitHub](https://github.com/openhab/openhab-core/issues/2024)  
No replies or activity, though.

---

<div class="post-metadata">

**Author:** ![MikeH](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@MikeH](https://community.openhab.org/u/MikeH)\
**Post date:** [March 17, 2021, 2:58pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/639 "2021-03-17T14:58:59Z")

</div>

I need help with my Time Of Day rule. Ever since the day lights savings change on Sunday my times of day that are based on specific time of day (5:00am & 10:00pm) are running 1 hour late. I tried to update them with a rule and also rebooted. The time shown in the Openhab3 log viewer is correct. Not sure how to fix it. Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 17, 2021, 3:20pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/640 "2021-03-17T15:20:31Z")

</div>

Which version of the rule? Show the logs from when the rule runs and when the transition between times of day occur. Put the rule into debug logging, or modify the debug logs to info so we can get more details about what is going on.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [March 17, 2021, 3:26pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/641 "2021-03-17T15:26:44Z")

</div>

If you use the JS rule, maybe the country in ephemeris is not set correctly? Like to a country that’s in the same time zone but has not switched over to DST?

---

<div class="post-metadata">

**Author:** ![MikeH](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@MikeH](https://community.openhab.org/u/MikeH)\
**Post date:** [March 18, 2021, 4:14pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/642 "2021-03-18T16:14:36Z")

</div>

I’m using the rule with the script you created. I have the country in ephemeris set to U.S. . I ended up just changing the times on times of day. Changed 5am to 4am and 10pm to 9pm. The rule now goes off on time, at least until the fall comes.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 18, 2021, 4:35pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/643 "2021-03-18T16:35:17Z")

</div>

> [@MikeH](#):
>
> I’m using the rule with the script you created.

I’ve posted three different versions in the original post. One in Rules DSL, one in Jython, and one in JavaScript. Which one?

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [March 19, 2021, 2:55am UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/644 "2021-03-19T02:55:26Z")

</div>

Rich,  
interesting fact: I run both the DSL and the JS rule, and just noticed that the JS rule now is an hour off, while the DSL rule (the improved one that was posted by someone else a few months ago) works correctly.

So I’d say he’s using the JS rule.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 19, 2021, 1:58pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/645 "2021-03-19T13:58:47Z")

</div>

Can you gather some debug logs for me to look at? Also, check your Items, particularly your “hard coded” Items. Perhaps the logic that moves those forward to the next day didn’t account for the time change. I’ll also look at my own but I’ve not noticed any problems.

---

<div class="post-metadata">

**Author:** ![Tron](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@Tron](https://community.openhab.org/u/Tron)\
**Post date:** [March 19, 2021, 2:35pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/646 "2021-03-19T14:35:25Z")

</div>

Now that you mention it, it looks rather strange.  
All fixed items (I wouldn’t call them hardcoded) have shifted an hour, but show a different time offset now:  
was: 5:00 GMT-6  
now 6:00 GMT-5

As we are in DST right now, GMT-5 is correct.  
They get updated, though, so they show today’s date. I could probably just issue an “openhab:send” to update them to the correct time, but that’s just a workaround.  
The variable items that use astro are correct.  
I’ll try to cook up some debug logs over the weekend.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 19, 2021, 2:42pm UTC](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407/647 "2021-03-19T14:42:30Z")

</div>

My working theory is that the code that moves the date times to the next day is messed up.

```auto
  context.toToday = function(when) {
    var now = ZonedDateTime.now();
    var dt = toDateTime(when);
    return dt.withYear(now.getYear()).withMonth(now.getMonthValue()).withDayOfMonth(now.getDayOfMonth());
  }

```

Date Times are complicated and I suspect that leaving the time part unchanged is skipping the part that will calculate the DST changes.

[Previous page](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407.md?page=31)

[Next page](https://community.openhab.org/t/design-pattern-simple-state-machine-e-g-time-of-day/15407.md?page=33)
