Switch receives update from UNDEF to OFF but rule does not fire [rules]

  • Platform information: Rasperry PI 3
    • Hardware: ARM
    • OS: Raspbian 201804031720
    • Java Runtime Environment: Zulu 11
    • openHAB version: 2.4.0

Hi all!
I need to command some light switch via Alexa, so I installed and configured a Raspberry PI with raspbian and I used the HUE emulator binding. It’ all right and the system does his job, but the lights are used also outside the system, so I need to write a rule that fires the event in any case because I don’t know if the light was switched ON or OFF outside the system.
I wrote a simple rule:

rule “Test_Light”

Item Test_Light changed

This rule writes in the log ALWAYS I send an ON command, also if I sent an ON as a previous command, but It does not log anything when I send the OFF command. It seems that the value OFF is equal to UNDEF or NULL (I tried both).

Also if I use received update instead of changed the rule does not work because it seems that there is not an update from NULL or UNDEF and OFF.

Is there someone that can expain me if I’m missing something?

It’ll be worth looking in your events.log to understand the sequence of, umm, events.

Note that a command to an Item does not always force an Item state to anything in particular. You are relying on something else, usually a binding or autoupdate, to take notice of the command, do something about it, and perhaps make a state update.

Hi Rossko,
thanks for your reply, this is the events log:
2019-08-04 12:08:26.618 [INFO ] [marthome.model.script.Test_Light] - UNDEF
2019-08-04 12:08:42.834 [ome.event.ItemCommandEvent] - Item ‘Test_Light’ received command ON
2019-08-04 12:08:42.844 [vent.ItemStateChangedEvent] - Test_Light changed from UNDEF to ON
==> /var/log/openhab2/openhab.log <==
2019-08-04 12:08:42.863 [INFO ] [marthome.model.script.Test_Light] - ON
==> /var/log/openhab2/events.log <==
2019-08-04 12:08:42.875 [vent.ItemStateChangedEvent] - Test_Light changed from ON to UNDEF
==> /var/log/openhab2/openhab.log <==
2019-08-04 12:08:42.886 [INFO ] [marthome.model.script.Test_Light] - UNDEF
==> /var/log/openhab2/events.log <==
2019-08-04 12:08:50.062 [ome.event.ItemCommandEvent] - Item ‘Test_Light’ received command ON
2019-08-04 12:08:50.071 [vent.ItemStateChangedEvent] - Test_Light changed from UNDEF to ON
==> /var/log/openhab2/openhab.log <==
2019-08-04 12:08:50.078 [INFO ] [marthome.model.script.Test_Light] - ON
==> /var/log/openhab2/events.log <==
2019-08-04 12:08:50.092 [vent.ItemStateChangedEvent] - Test_Light changed from ON to UNDEF
==> /var/log/openhab2/openhab.log <==
2019-08-04 12:08:50.106 [INFO ] [marthome.model.script.Test_Light] - UNDEF

As you can see, the rule works very well when the state changes from ON to UNDEF and UNDEF to ON, but when I send the OFF command via Alexa, the log does not show anything.

In the items file I have only:

Switch Test_Light “Test_Light” [“Switchable”]

that is addressed correctly from Alexa.

If I write a simple rule that switches Test_Light ON or OFF it works perfecty, but if someone switchs the light on or off outside, Openhab does not manage the state, so I need to reset the default value to the switch that, (I think) is UNDEF.

events.log shows things happening as expected. When there’s a change, your rule runs.

UNDEF state means, Undefined, in other words “I have no idea what the state is” and usually a binding might set that because something has gone wrong.

I’m guessing that somehow this Item is linked to real hardware, via Hue binding perhaps? What you need is an update from the device that reflects its actual state. Look to your device and Hue setup.

I’m unclear on this statement. If you have a connected device that can be turned on/off manually, it should report its status to openHAB, So unless I’m missing something, you don’t need this rule.

One potential issue is that your rule is constantly setting Test_Light to UNDEF on any change. So when Alexa changes it to ON, your rule detects a change and sets the OH status to UNDEF. And then the same when Alexa sets it to OFF. So, Test_Light is always UNDEF.

I don’t know why Alexa would send UNDEF commands instead of OFF commands, but if that’s the case, that’s why the rule doesn’t react: Alexa is sending UNDEF to an item that is already UNDEF.

To avoid this, you either need to have a rule that detects when the light goes ON and another when it goes OFF, or a single rule with an if condition that reacts to the new state.

On a side note, code fences make it much easier to review code. You can apply them by selecting a block of text and clicking the </> icon in the editing bar.

Hope this helps!