Avoid push notifications when previousState was UNDEF

Hello Community,

I want to stop push notifications when the previousState was UNDEF, how can I achive that?
Maybe, I have a logical problem but my rule is not working and sending notifications also when status was previous UNDEF (for testing I set the status UNDEF of the switch via API) :smiley:

Anybody a suggestion?

rule "Gäste WLAN"
when
    Item FritzBox_GaesteWLAN changed
then
        if (FritzBox_GaesteWLAN.previousState != UNDEF )
        {
            if (FritzBox_GaesteWLAN.state == ON) 
        {
                 //sendLogNotification("Gäste WLAN ist eingeschalten")
                 sendBroadcastNotification("Gäste WLAN ist eingeschalten")
        }
            if (FritzBox_GaesteWLAN.state == OFF) 
        {
                //sendLogNotification("Gäste WLAN ist ausgeschalten")
                sendBroadcastNotification("Gäste WLAN ist ausgeschalten")           
        }
        }
        else
        {
                //Do Nothing
        }

end

Thanks in advance :slight_smile:

Add some logging so that you can follow progress through your rule and see key variables. What is the value of previousState, for example?

This also serves to confirm that you are saving the rule file in the correct place, etc., and that it is not something else performing your unexpected action.

However, you are almost certainly not getting the item.previousState that you think. That works on persistence. What is your default persistence service? How is it configured, persist on every change? What happens if the new state is persisted before the rule retrieves previousState? What happens if the old state is not persisted before the rule runs?

You should probably simplify and use the implicit variable previousState which is similarly named but does not use persistence at all.

i´m not sure but i have two ideas that you might try:
1.:

rule "Gäste WLAN"
when
    Item FritzBox_GaesteWLAN changed
then
    if (FritzBox_GaesteWLAN.previousState(true).state != UNDEF)
    {
        return;
    }
    if (FritzBox_GaesteWLAN.state == ON) 
    {
        //sendLogNotification("Gäste WLAN ist eingeschalten")
        sendBroadcastNotification("Gäste WLAN ist eingeschalten")
    }
    else
    {
        //sendLogNotification("Gäste WLAN ist ausgeschalten")
        sendBroadcastNotification("Gäste WLAN ist ausgeschalten")           
    }
end

or if this does not work
2.:

rule "Gäste WLAN"
when
    Item FritzBox_GaesteWLAN changed from OFF to ON or
    Item FritzBox_GaesteWLAN changed from ON to OFF
then
    if (FritzBox_GaesteWLAN.state == ON) 
    {
        //sendLogNotification("Gäste WLAN ist eingeschalten")
        sendBroadcastNotification("Gäste WLAN ist eingeschalten")
    }
    else
    {
        //sendLogNotification("Gäste WLAN ist ausgeschalten")
        sendBroadcastNotification("Gäste WLAN ist ausgeschalten")           
    }
end
1 Like

2nd is working and sometimes you are losing the easy way when you are scripting :smiley:
@bastler thank you for the hint!

rule "Gäste WLAN"
when
    Item FritzBox_GaesteWLAN changed from OFF to ON or
    Item FritzBox_GaesteWLAN changed from ON to OFF
then
    if (FritzBox_GaesteWLAN.state == ON) 
    {
        //sendLogNotification("Gäste WLAN ist eingeschalten")
        sendBroadcastNotification("Gäste WLAN ist eingeschalten")
    }
    if (FritzBox_GaesteWLAN.state == OFF) 
    {
        //sendLogNotification("Gäste WLAN ist ausgeschalten")
        sendBroadcastNotification("Gäste WLAN ist ausgeschalten")           
    }
end