Jython - Trying to use ephem_tod.py

Openhabian on Pi 4 running openHAB 3.2.0 Build #2454

Ok I have to admit defeat. Been reading through the various posts, (DPs thanks @rlkoshak !) and documentation for using the ephem_tod for a few days and I’m not getting it …

I have many other jython scripts working so my setup is largely correct (obviously missing something though)

what would be helpful for me is the simplest example that gets all the components of ephem_tod working… and basically sets the TimeOfDay item accordingly …

So here is what I have so far:

In automation->jsr223->python->community:
300_item_init.py
ephem_tod

In automation->lib->python->community:
rules_utils.py
time_utils.py (and the py.class was created)
timer_mgr.py

What I dont have is the configuration.py populated because I don’t know what to put in there … I see that it wants from configuration import tod_group, tod_item but of course they are not there … and so also of course there isn’t a TimeOfDay item to set!

I see the python example at:

But I dont know where that is supposed to go … I cant add it to a Rules yaml as is implied.

I’m also confused since alot of the docs are assuming you are going from rules to jython from a pre-OH3 world … I have just started with OH3 and never wrote DSL Rules (just doing mostly jython) … perhaps that’s the reason i’m not getting it … also, I see some of the posts are very new, so possibly I am trying to do something that isn’t fully baked yet?

Anyway … has someone else had this similar conundrum trying to get the ephem_tod.py examples to work? … any pointers would be helpful!

#Frustrated but not giving up :slight_smile:

The one thing you’ve probably missed is that the Jython libraries in my repo are not supported on OH 3 yet. Over the last few weeks I’ve been trying to make that clear in the readmes on the repo but that’s not complete.

The challenge is coming up with a new implementation that is backwards compatible which takes more work than just a couple of tweaks.

So on OH your only choice is to either make the changes to the code yourself (it boils down to changing Joda DateTimes to ZonedDateTimes) or use the JavaScript version of the library. The JavaScript version is likely going to be the only one I maintain going forward. Once we have a marketplace where these rules and libraries can be distributed like add-ons, I’ll convert the code to be a Rule Template and because I want to have as few extra dependencies as possible I’ll use JavaScript without the Helper Libraries.

I do plan on upgrading the python libraries where possible to work with OH 3, but the rules like ephem_tod and debounce will likely become deprecated. Why maintain them when users can just install them? It doesn’t matter what language they are written in because no one ever needs look at the code except me.

If using the Helper Library you have to at least have a configuration.py. Just copy the example one to a .py file and you can leave the defaults. I didn’t think the rule depended on configuration.py but it appears to try to import the name of the Group Item and the name of the TOD Item from there. I did away with that in the JavaScript version and just expect a hard coded TimesOfDay Group and TimeOfDay Item.

Anyway, if you want to use ephem_tod in OH 3, you must either fix the Python version or use the JavaScript version. The JavaScript version does not depend on the Helper Libraries. It also doesn’t create the Item for you nor does it use rules_utils (I haven’t written a JavaScript version of that yet and am not sure I will). It also doesn’t use item_init because I’ve not written the JavaScript version of that yet either and with MainUI it’s better to use a widget to initialize the DateTime Items anyway. See OH 3 Alarm Clock and OH 3 Examples: How to boot strap the state of an Item.

All you need to do is create a TimeOfDay String Item and a TimesOfDay Group.

Create your DateTime Items with the ephem_tod metadata and make sure they all have a valid state (not UNDEF, not NULL).

Then create a new rule in MainUI, copy the contents of ephemTimeOfDay.yml into the Code tab of your new rule. Save.

Finally run the rule by clicking the play button in MainUI. Watch the logs for errors. If there are no errors you are good to go. If not, the error should indicate what is wrong.

1 Like

Ok thanks Rich for the quick response … I was suspecting I was trying something that was still work in progress … I think I will abandon the Jython version for now (or forever) and just get the ephem_tod javascript to work.

So I will remove the python code related to ephem_tod and add the yaml javascript code for ephem_tod.

Then I read the and tried to follow " How to boot strap the state of an Item … I created an item named Default_Day, added it to the TimesOfDay group, added the various fields of the “Default Standalone Widget” (fyi type = datetime-local s/b time-local) … and then set the time to 08:00:00 (and hit the check mark to save it) … then I hit “Run” for the Rule …

making process, but now have the error:

2021-08-02 13:29:57.082 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'TOD_Javascript' failed: Default_Day lacks metadata or metadata value.
Invalid metadata for Item! Expected metadata in the form of etod="STATE"[type="daytype", set="dayset", file="uri"] where set is required if type is dayset and file is required if type is custom. in <eval> at line number 256 at column number 6

any ideas?

Don’t forget to get timeUtils.js and timerMgr.js as well. Those are still required dependencies.

Did you add the Item metadata to Default_Day? If not, go back and reread the README for the Time of Day rule on the repo. The Item metadata drives the whole thing. It’s not optional. And it’s a little complicated because it supports a lot of different options.

Yes added timeUtils.js and timerMgr.js … yes added the Item metadata:

I added a second group member Default_Night as well thinking that might cause issues … same result … The item changes to the time that I set … so it is seeing the metadata.

Ok clearly I am missing:

etod="STATE"[type="daytype", set="dayset", file="uri"]

Where is that supposed to be defined?

There is nothing related to the Item metadata in that screen shot.

In a text defined Item it would look something like

DateTime Default_Morning (TimesOfDay) { etod="MORNING"[type="default"] }

The etod=... part is the metadata.

See openhab-rules-tools/ephem_tod at main · rkoshak/openhab-rules-tools · GitHub for a full discussion of each part of the metadata.

On the Default_Day Item in the UI you navigate to the Item in MainUI. Click on “Add Metadata” and then “Enter custom metadata namespace”. The namespace is etod. The metadata breaks out something like

value: BED
config:
  type: default

The value is right after the first = in the text based definition. Config is a series of zero or more name/value pairs, the stuff between [ ] in the text based definition. Most of the time you’ll just have type: <one of the supported types> where the list of supported types is in the README. If using custom as the type you need to also provide the path to the ephemeris config file.

1 Like

Ok, that was it … I added a “etod” custom metadata for DAY and NIGHT (for my examples) and it set the TimeOfDay … I have some jolly errors that I need to resolve …thanks Rich!

The fact that there are many ways to do the same thing is probably messing with my mind as I read through the threads :slight_smile: