- Platform information:
- Hardware: Raspberry Pi 3 B+, 120gb mSATA SSD in a USB enclosure
- OS: Raspbian Stretch
- Java Runtime Environment: 1.8.0_212
- openHAB version: 2.4.0 release build. Jython 2.7.0
This morning my night lights didn’t turn off.
Here’s how I have them set up.
Two Switch items triggered by astro channel events, and other rules triggered based on the switches.
Items:
Switch DarkOutside "Dark" <moon> (Bench)
Switch SleepMode "Sleep" <bedroom_blue> (Bench)
night_lights.py:
from org.slf4j import LoggerFactory
from core.rules import rule
from core.triggers import when
def logprint(text):
pass
LoggerFactory.getLogger("org.eclipse.smarthome.automation.examples").info(text)
@rule("Sunset Rule", description="", tags=["schedule"])
@when("Channel \"astro:sun:local:civilDusk#event\" triggered START")
def Sunset_Decorators(event):
item=ir.getItem("DarkOutside")
item.send(ON)
@rule("Sunrise Rule", description="", tags=["schedule"])
@when("Channel \"astro:sun:local:civilDawn#event\" triggered START")
def Sunrise_Decorators(event):
item=ir.getItem("DarkOutside")
item.send(OFF)
@rule("Night Lights", description="", tags=["schedule"])
@when("Item DarkOutside received update")
@when("Item SleepMode received update")
def NightLights_Decorators(event):
itemDarkOutside=ir.getItem("DarkOutside")
itemSleepMode=ir.getItem("SleepMode")
isDarkOutside=1 if itemDarkOutside.getState() == ON else 0
isSleepMode=1 if itemSleepMode.getState() == ON else 0
itemNightLights=ir.getItem("NightLights")
logprint("dark: " + str(isDarkOutside) + " sleep: " + str(isSleepMode))
itemNightLights.send(ON if isDarkOutside!=0 else OFF)
itemMainGateLights=ir.getItem("MainGateLights_On")
itemMainGateLights.send(ON if isDarkOutside!=0 and isSleepMode==0 else OFF)
I looked at my logs and saw the following:
2019-06-14 00:07:11.985 - bedroom wallmote: 1.0
# that was me turning on my bedside light
2019-06-14 00:07:12.996 - bedroom wallmote: 2.0
# that was me turning off my living room ceiling lights
2019-06-14 05:17:26.087 - wallmote: 1.0
# that was me trying to turn on my workstation monitors
2019-06-14 05:17:35.193 - wallmote: 1.0
# that was me trying again to turn on the workstation monitors since the z-wave switch didn’t react the first time…
2019-06-14 05:32:00.041 - dark: 1 sleep: 0
# this was civilDawn today, when the night lights should have turned off.
2019-06-14 05:46:58.872 - WashingMachineFrontLoaderWatts entering washing cycle
# someone is doing laundry
It looks to me like the “Night Lights” rule was correctly triggered by Item DarkOutside received update, but when we got the state of itemDarkOutside, it still reported ON.
What is the correct way of getting the state of an item that has triggered a rule?
Also, even if the getting the item state the way I did it is wrong, it would probably make things easier for everyone who scripts openHAB in the future if the underlying implementation could be modified (some may say bug fixed) to make the trigger consistently happen after the item states have been updated. As it is now, it behaves as if there’s a race condition in the code, and even if it can be worked around by more scripting, it might be better if it just didn’t happen at all.
Let me also please emphasize that I didspend a good hour googling and searching the forum trying to find a better looking way to do it, before implementing in this fashion to begin with. Frantic searching gets more difficult over time though as more and more related links end up showing as already visited and thus it becomes difficult to know whether I visited a page with regards to the current problem at hand or a previous problem. I was not able to find a better answer, but that doesn’t mean there isn’t one, of course.
“Implicit Variables” seemed to offer a clue, but I never found good documentation about those… until now, as I’m writing this post, of course. This post seems to be it: forum post
The “But how do I…?” page has many good tidbits of information, like event.itemState but it doesn’t say what methods are available on the received object! Different items (numbers / switches / strings) have very different accessor functions, and it would be great to have examples for how to both update all of those item types as well as how to write “received update” rules… I feel this is a highly fundamental part of OpenHAB and could probably use more documentation. I do understand that this is an open source project and as such written by volunteers, and documentation is not the fun part of development, to be sure…
This really feels like a trap, though. I just found another way to do it:
@rule("MyButtonRule", description="", tags=["utility"])
@when("Item MyButton received update")
def MyButtonRule(event):
itemstate = items["MyButton"]
logprint("itemstate is " + str(itemstate))
…and this way ALSO doesn’t work! Here’s what the log says when I toggle the MyButton switch item on and off.
2019-06-14 09:39:41.971 [INFO ] itemstate is ON
2019-06-14 09:39:42.255 [INFO ] itemstate is OFF
2019-06-14 09:39:42.618 [INFO ] itemstate is ON
2019-06-14 09:39:42.957 [INFO ] itemstate is OFF
2019-06-14 09:39:43.804 [INFO ] itemstate is ON
2019-06-14 09:39:44.244 [INFO ] itemstate is OFF
2019-06-14 09:39:44.764 [INFO ] itemstate is ON
2019-06-14 09:39:45.138 [INFO ] itemstate is OFF
2019-06-14 09:39:45.712 [INFO ] itemstate is ON
2019-06-14 09:39:46.244 [INFO ] itemstate is OFF
2019-06-14 09:39:47.022 [INFO ] itemstate is ON
2019-06-14 09:39:47.693 [INFO ] itemstate is OFF
2019-06-14 09:39:48.168 [INFO ] itemstate is ON
2019-06-14 09:39:48.717 [INFO ] itemstate is OFF
2019-06-14 09:39:51.887 [INFO ] itemstate is ON
2019-06-14 09:39:53.163 [INFO ] itemstate is OFF
2019-06-14 09:39:54.608 [INFO ] itemstate is OFF
2019-06-14 09:39:55.159 [INFO ] itemstate is OFF
2019-06-14 09:39:55.996 [INFO ] itemstate is ON
2019-06-14 09:39:57.596 [INFO ] itemstate is ON
2019-06-14 09:39:59.669 [INFO ] itemstate is ON
2019-06-14 09:40:00.372 [INFO ] itemstate is OFF
Seriously. This CAN’T just be me, can it? Is this a really obvious bug? What am I missing?
I’m about to put a freaking sleep statement in at the top of all my events just so I can get something done.
Thank you for reading! I’m very impressed by this forum so far, I’ve gotten excellent answers every time, from one person in particular but nonetheless!
///Leif