Current System Time in Sitemap

I tried searching lots of different terms, but closest I came was finding a lot of stuff about the NTP binding.

Certainly this has to be available somewhere in the core system, no?

EDIT: I am marking rossko57 answer as “Solution” however I want to point out that there appears to be no direct way that OpenHAB exposes system time, so this is the closest workaround we have. Thanks rossko!

Everything displayed by sitemap comes from an Item.
So, you make a DateTime type Item, and update every so often with “now” by rule, triggered by cron.

1 Like

Of course. I just figured there must be some more elegant way. Something like:

DateTime   currentSystemTime   "Current System Time [%1$tH:%1$tM]"   { channel="core.system.time" }

… or whatever…

But apparently not?

This is exactly what the ntp binding does… Just that it is not part of the core but a binding

If it would be part of the core system the binding wouldn’t exist. It seems you did not read the NTP Binding Documentation.

Maybe you are not familiar with what the N in NTP stands for?

Network Time != System Time

(although they should be very close, even maybe same, depending on refresh interval, etc…)

I read a tremendous amount (as I always do) before even posting the question. There is nothing whatsoever in the NTP documentation explicitly stating that the concept of “System Time” is not exposed in any way and that is why the NTP binding was created. And while perhaps a valid inference on your part, that is what it is, an inference.

At any rate, I suppose I am just a bit surprised that this is not directly exposed in any way. Closest I think I found (and I’m not even sure about that) was back in this old thread which was about then (Oct 2018) future roadmap for OpenHAB.

Other than that, I think the only way to actually get the system time would be to do something like what @rossko57 suggested above. But wow, what a hack! Running a cron every minute (or more! if you want accurate granularity…). What an unnecessary load on the system, IMO…

So, last night, I made choice to try the ntp binding, but I could not get it to work. I just un-installed it and gave up, but maybe instead I should make a separate thread about that and try and figure out what happened.

Why do you need this?

How do you imagine some built-in variable would get updated?

Yeah, I suppose you are right. I dunno, maybe I am thinking in bash terms where each new exec command spawns another shell… I would imagine some built in primitive would be less “expensive” so to speak. Particularly since I would likely be calling it more than once per minute (to be more accurate).

I just installed Astro binding recently. There are a lot of different times in there for sun rise, set, dusk (of numerous types, etc.). So I put them all in a frame in my sitemap, so I could start comparing them to observed light levels, with the idea of eventually using them in rules, adopting Time of Day Design Pattern, etc…

And so I am comparing them with an exterior clock. I thought, well why not just make an Item in the Sitemap to show the current system time? How hard can it be? :laughing:

It isn’t hard. Here’s mine

DateTime dt_Systime "System time [%1$tA %1$td/%1$tm %1$tR]"	<time>
rule "Clock tick"
when
    Time cron "5 0/1 * * * ?"  // each minute, offset by 5secs
then
    dt_Systime.postUpdate(new DateTimeType())
end

sitemap

		Text item=dt_Systime

Making it is a good introduction to parts of openHAB.

Making an auto-updating NTP driven Item is another approach, showing binding interactions.

2 Likes

Thanks for sharing that.

Something I had considered, is when exactly the cron job fires, in relation to the changing minute on the system. From watching logs of other things, I was thinking that it is “close” but maybe not “exactly” on the minute, depending on what else the system is also doing.

I am assuming this is why you fire your cron 5 seconds after the minute change?

Yes, a good exercise and discussion, next person coming along looking for same should have an easier time.

There’s bound to be other jobs, both mine and under the hood, firing at the “top of (some) minutes”. Easy enough to avoid, immaterial to a simple clock display on UI.

Exactly.

And updating the system time variable, 5 seconds after top of every minute, I think strikes the best possible balance between considerations (system load vs accuracy).

Even though as you correctly point out, it’s not even super critical for my particular application (although it could be for others).

Then (in my case at least) there will also be the issue of how often the sitemap itself updates, from the system time variable…

I take that one! :pensive:

1 Like

It is if you use the localhost NTP daemon as the source for the time. Though it’s generally a good idea to dedicate one machine on your network as the “clock” and synchronize all your other machines based on that one machine. I run the NTP service on my pfSense firewall.

You are concerned about this but not about to causing the web page displaying the current time to refresh every second? That’s probably where you’re going to run into real problems.

And even so, worrying about this sort of performance problem on a home automation system is almost never worth the time to think about it. Unless something is wrong, the vast majority of the time your machine will be sitting there idle.

Honestly, I’ve never really understood the use case for the NTP binding in the first place or the benefit of putting the time in an Item. When you are in a Rule, where millisecond accuracy is important, you’d use now instead of an Item anyway. About the only thing I can think of where it’d be useful is display on a sitemap/HABPanel. And given all the latencies that add up from getting the time, updating the Item, generating the event to announce that the time changed, passing that event through the websocket, and finally rendering the new time on the UI, I’d be surprised if what you see on the sitemap is accurate to the second.

OK, given that use case I would either use the suggestion of a Rule that runs once a minute (you don’t need more than minute accuracy really) and updates an Item with now, or set up ntp on your OH machine and using the NTP binding.

Note, if you are running on a Raspberry Pi which doesn’t have a real time clock, if time accuracy is important to you, you will want to run NTP on the RPi anyway and synchronize it with something that has a better clock anyway.

Nothing on a computer, especially something like an RPi, happens at exactly a scheduled time unless it’s running on a real time operating system. If you are worried about that sort of accuracy, than you really need to set up NTP and use an external source for your time. There are lots of free NTP services on the web, most hosted by universities or government agencies. To have accurate time locally you need to buy a time server that gets it’s time from the GPS satellites.

But given your use case, the time could be off by 30 seconds or more and not have a practical impact.

To repeat a saying from my old boss, you are measuring with a micrometer to cut with an axe.

For those who really need accuracy in the sub-second or I’d even say sub-five seconds or so range, you can’t get there from here. openHAB has too much latency and is not a real time system. It cannot guarantee that sort of accuracy.

Good point. Although not generally, which was my point.

I don’t think I ever said that. To be clear, I was only concerned about roughly “to the minute” accuracy, being more or less reasonably accurate. In other words, not move the minute up until 59 seconds into the next minute or something way off like that.

You hit the nail on the head as to where all the real latencies will be, and something I even pondered on a bit myself, in this post.

Yes, I think this is what we concluded. However thanks as always for your detailed input.

I thought you knew well by now my disdain for fruit flavored devices? :smile:

I think it got lost somewhere that this was not the accuracy I was looking for, but I already addressed that in my second reply in this post.

At any rate, this has been a great discussion around topic of “system” time, and will certainly give anyone coming along later plenty to consider. So thanks again Rich for the detailed and thoughtful reply, as always.

Not every response is directed solely at the person I’m responding to. Most of the time, when I write a reply like above, all the future readers of the forum who will come across this thread, most of whom will be using RaspberryPis, are the target audience.

I find that providing a good bit of background and detail on posts similar to this cuts down a little bit on users asking the same question over and over again, only with slightly different twists or details or use cases. If most of the details are in a reply to an easily findable post like this, whether the details were specifically asked for or not, often answers those user’s questions and a new post isn’t required. Everybody wins! :smiley:

1 Like

@rossko57

How do I create this rule in the new rule engine on OH3? I just cant get the “then” statement…

When creating rules in files, you need rule-when-then-end stuff to give it a framework.

When creating rules in GUI, all that part is done by point and click. You only need to type in part between, but not including, ‘then’ and ‘end’ in a DSL script action section.

:+1: