mhbosch
(Michael Bosch)
1
Hello,
i need a Snippet to calculate a duration in Astro Binding (possible millisecond).
There a two items from Astro Bindung.
The Twillight and Night:
DateTime Night_Time “Nacht [%1$tH:%1$tM]” (gAstro) {channel=“astro:sun:home:night#start”}
DateTime Twilight_Time “Abenddämmerung [%1$tH:%1$tM]” (gAstro) {channel=“astro:sun:home:civilDusk#start”}
Now i need the Duration between this times.
In OH2 i have use:
Zeit= DateTime(str(ir.getItem(“Night_Time”).state)).getMillis() - DateTime(str(ir.getItem(“Twilight_Time”).state)).getMillis()
But with the new OH3
from java.time import ZonedDateTime as DateTime
i cant calculate them.
Have anyone a quick snippet for me?
Be healthy,
mhbosch
rlkoshak
(Rich Koshak)
2
Even if you don’t use them, the Helper Libraries are always a great place to look for stuff like this:
openhab-helper-libraries/date.py at ivans-updates · CrazyIvan359/openhab-helper-libraries · GitHub lines 132-237.
Milliseconds will be challenging because ZonedDateTime doesn’t support milliseconds directly and instead provides nanoseconds.
mhbosch
(Michael Bosch)
3
Thanks!!!
I use the helper library, but i dont look inside. My mistake.
The solution:
from core.date import seconds_between
and
@rule("Daemmerung beginnt", description="Daemmerung beginnt", tags=["Example", "Astro"])
@when("Channel astro:sun:home:set#event triggered START")
def astrorule(event):
try:
LogAction.logInfo("Astro",u"Dämmerung beginnt, helle die Lampe auf")
Zeit = (seconds_between(ir.getItem("Twilight_Time").state, ir.getItem("Night_Time").state)*1000)
LogAction.logInfo("Astro","Zeit = " + str(Zeit))
actions.get("telegram", "telegram:telegramBot:Telegram_Bot").sendTelegram(u"Dämmerung beginnt, helle die Lampe auf")
events.sendCommand("EG_Frontdoor_lamp_Dimmer","0")
events.sendCommand("EG_Kitchen_lamp_Fensterlampe_Dimmer","0")
events.sendCommand("universaldimmer","Dimmer,60,"+str(Zeit)+",EG_Frontdoor_lamp_Dimmer,10000")
events.sendCommand("universaldimmer","Dimmer,30,"+str(Zeit)+",EG_Kitchen_lamp_Fensterlampe_Dimmer,10000")
except Exception as e:
LogAction.logInfo("Astro","{}".format(e))
return None
Universaldimmer is a Function in jython to fade ligths.