Compare current time to preset value blockly

Hi,
I am having a potentially stupid question regarding blockly scripts.
For my blinds I have to check if the current time is before midday or after. I have installed the time extension.

However I cannot seem to find the correct blocks to get this easy operation done.

It should just be

If now is earlier than 12
if now is later than 12

Is there an easy way to do that?

Thank you very much in advance.

as always, there’s multiple approach to this.

you can either use the current timestamp, extract the hours from it and then compare that.

or you can look around in the tutorial, and in the end you only have to configure Rich’s template from the Marketplace…

Thank you,
I will look into it this afternoon.
Although it seems pretty complicated for the simple check that i require.

Very basic (untested)

1 Like

if it’s only one check it sure is a bit of a overhead, but I’m pretty sure, that “time of day” will regularly com up as your openHAB grows.

or just use hafniumzinc’s solution - I don’t know blockly, but apparently he does! :wink:

thank you again,

I already have a time item called midday which is derived from the astro binding but includes a date and not only a time. Can I just use this item or do I have to define a new item which includes just a time?

Try it and see what happens! now has both date and time too.

I tried this

But it does not work.

You’ll have to provide more information than this.

Put some logging blocks in to see what state your various Items and variables are, and logging blocks inside the If results so you can see where the code has gone. Then monitor the logs as you run the rule.

EDIT: I’ve now tested my blockly example from my first post: seems to work well.

EDIT: Looking at what you’ve got: you’re giving the comparison block the Item name, not the state of the Item. You will need to try with the Item state, and you may have to assign that state to a variable first.

Oh yeah, that makes a lot of sense.

now I seem to have a different problem. I am raeding through other posts as well and see the set varable to block a lot (the first one you used in your example) I however cannot find this block in my blockly. Does it come from a different library?

Your help is very appreciated. I am moving my whole smarthome from domoticz to openhab and while openhab has a lot going for it. The coding is quite different and the learning curve is rather steep especially knowing how i solved the problem before and not being able to replicate it.

Another small question. When I construct this script under the script section I cannot call for it from a rule. I always have to write a new script in every rule i create. is that correct? seems a bit strange.

No, it’s there by default. But you have to create the variable first.

No, you can call a rule from a rule. A Script as shown in MainUI is just a rule without any triggers or conditions and a single Script Action. It would probably be helpful for you to go through the Rules section of the Getting Started Tutorial (Rules - Introduction | v3 Documentation Preview) which is awaiting merge into the official docs.

But, usually the best approach in OH when you have a lot of rules that run the same code just on different Item events is to have just the one rule to handle them all instead of duplicating the same rule for each Item, even if those just call the same Script.

I would think that not needing to write any code at all would be simpler than needing to write code to handle this. That tutorial is thorough but it’s pretty simple.

  1. Install the Alarm Clock rule template
  2. Create DateTime Item(s) an populate them with the desired time from Astro, Android app’s next alarm, or whereever
  3. Create a Script or Rule that will run at the time held by the Item(s) from 2 to do what ever it is you want, that tutorial changes the state of a TimeOfDay Item to drive a state machine. Other rules do tests like if (TimeOfDate.state == "DAY") instead of needing to break DateTimes apart and testing the hours, minutes, seconds individually.
  4. Create a new rule and configure it using the Alarm Clock template

You never have to mess with code at all beyond coding what you want to have happen at that time.

1 Like

No, it’s standard. Go into the Variables group, create a new variable, and then the Set block will become available.

Thank you again,

I started to alter my rule starting from your example.

My problem now is that when I set a variable to an item coming from the astro binding the time and date is in a different format than the now coming from the datetime library.

I added two examples. The first one works.

The second one outputs an error which i guess is due to the different format.

Is there a possibility to solve this?



i figured it out.
This one works

And here is the final rule controlling my 12 blinds.
The values can be a bit fine tuned but the concept works.

The actual commands have to be set to where the log outputs are right now.

Thank you again for your help!