I am running OH 4.3.5 on a raspberry pi 5.
I use the Astro Binding to get Sunset and Sunrise time.
I would like to calculate a new time when Sunrise time changes.
I found this Topic , but I did not understand how to use JavaScript Scripting.
I would like to subtract 2 hours from the Sunrise time whenever it changes.
How can I do this? Can somebody provide a *.rules file or a *.js file.
If it is a *.rules file, I know how to use it. If it needs a *.js file I would need more explanation on how to use it with a rule.
My Things:
// Typ BindingID:TypeID:ThingID "Label" @ "Location" [parameters]
Thing astro:moon:home "Mond" [ geolocation="47.4060200, 9.6085110,404", interval=300 ]
Thing astro:sun:home "Sonne" [ geolocation="47.4060200, 9.6085110,404", interval=300 ]
My Items:
// Typ Name "Label" <Category> (Gruppe) ["SC"] {channel}
DateTime rise_end "Sonnenaufgang ende[%1$tH:%1$tM]" <sunrise> (gAstro) {channel="astro:sun:home:rise#end"}
DateTime set_end "Sonnenuntergang ende[%1$tH:%1$tM]" <sunset> (gAstro) {channel="astro:sun:home:set#end"}
DateTime set_start "Sonnenuntergang start[%1$tH:%1$tM]" <sunset> (gAstro)
Whenever Item set_end changes I want to subtract 2h from this time and update Item set_start with this calculated value.
sihui
(SiHui)
July 2, 2025, 7:22pm
2
You can do this without any rules via offsets on the range channel (if you need a channel trigger, or also on other channels for displaying purposes):
If you need both times, create two separate Astro Things.
Thank you for your help. I did now add one thing:
// Typ BindingID:TypeID:ThingID "Label" @ "Location" [parameters]
Thing astro:moon:home "Mond" [ geolocation="47.4060200, 9.6085110,404", interval=300 ]
Thing astro:sun:home "Sonne" [ geolocation="47.4060200, 9.6085110,404", interval=300 ]
Thing astro:sun:offset "Sonne Offset" [ geolocation="47.4060200, 9.6085110,404", interval=300 ]
{
Channels:
Type rangeEvent : set#event
[
offset=-60
]
}
And changed my Item:
// Typ Name "Label" <Category> (Gruppe) ["SC"] {channel}
DateTime rise_end "Sonnenaufgang ende[%1$tH:%1$tM]" <sunrise> (gAstro) {channel="astro:sun:home:rise#end"}
DateTime set_end "Sonnenuntergang ende[%1$tH:%1$tM]" <sunset> (gAstro) {channel="astro:sun:home:set#end"}
DateTime set_start "Sonnenuntergang start[%1$tH:%1$tM]" <sunset> (gAstro) {channel="astro:sun:offset:set#end"}
Unfortunately I do get the same time without offset for both Items:
2025-07-02 00:00:30.967 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'set_end' changed from 2025-07-01T21:23:00.000+0200 to 2025-07-02T21:23:00.000+0200
2025-07-02 21:47:22.726 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'set_start' changed from NULL to 2025-07-02T21:23:00.000+0200
What am I doing wrong?
sihui
(SiHui)
July 2, 2025, 8:01pm
4
PyroEngineering:
What am I doing wrong?
Sorry, I don’t know anything about Things in files.
hmerk
(Hans-Jörg Merk)
July 2, 2025, 8:19pm
5
PyroEngineering:
What am I doing wrong?
If I am not mistaken, you did not define the event the offset should be added to.
It should be set#end, not set#event in your .things file.
rlkoshak
(Rich Koshak)
July 2, 2025, 10:05pm
6
The offsets described by others here on this thread are probably the correct solution you want. But to answer your specific question, the code for the action will be this one liner:
items.set_start.postUpdate(time.toZDT(items.set_end).minusHours(2));
Depending on how you create the rule will change how you define the rule trigger, but the rule trigger should trigger on changes to set_end.
Using JS rule builder it would look something like this:
rules.when().item('set_end').changed()
.then( event => {
items.set_start.postUpdate(time,toZDT(items.set_end).minusHours(2));
})
.build('Update set_start');