Best way to build a rule based on a distance

Hi all,

I am trying to get OpenHab to track my standing-desk uptime and downtime.

I have a value coming every minute from an adruino via MQTT to base my rule on.

My issue is that:
1 - I need to be within a ~5cm range
2 - Sometimes I get a rogue value outside of an normal up/down value
3 - When I use a rule that triggers based on ‘receives update’ it might run three times based on the one MQTT value update.

What is the best way to update a virtual switch based on the state of an incoming numeric value? It seems like I want to make sure it stays within a range for 2-3 minutes before I change the state and record the time.

I’ve tried a few different scenarios, but none have been reliable. Can someone point me to a good guide?

TIA,

Jason

You may need to add either a filter on the Arduino side to prevent sending the rogue values or a filter on the OH side to ignore the rogue values.

On the OH side you would create a rule which processes the incoming MQTT values. If the value is beyond the possible (e.g. larger or smaller than the last value by a lot) drop it. Otherwise pass it on to a proxy item. In your case I think the proxy item is your switch.

As to your received update, are you triggering this rule based on a Group Item? If not, one message from MQTT should only result in one update to that Item. But because of the way Groups aggregate their state one change to an Item in a Group results in multiple updates to the Group.

If you are not triggering off of a Group Item, then something else weird is going wrong. But you can filter out a lot of the noise by using changed as your trigger instead of received update. That will only trigger the rule if the Number actually changes causing repeated updates from the same state change to be ignored.

If you do not want to update your proxy item until the Number Item has been stable for a period of time you can use a Timer to implement a Latch.

var Timer timer = null

rule "Desk state changed"
when
    Item MyNumberItem received update
then
    if(timer == null){
      timer = createTimer(now.plusMinutes(2), [|
          // calculate whether the switch should be ON or OFF based on the current state of MyNumberItem
        MySwitchItem.sendCommand( new_state )
        timer = null
      ]
    }
end

In the above rule the first time a change occurs a timer is created that goes off in two minutes. All triggers of the rule that occur while the timer is waiting to go off are ignored. When the timer does go off it sets MySwitchItem to the appropriate value based on what MyNumberItem is at that time. The timer gets reset to null and the cycle repeats on the next update.

Personally, I don’t think this is the right way to go. There is something else going on that smells fishy but with the limited information provided it is the best I can do.

Hi Rich,

Thanks for the detailed reply! Unfortunately, the Arduino is an NodeMCU running espeasy and just sends a value once every minute. I have to toss out any rogue values in the rule.

I unfortunately haven’t had time to get back to this and apologize for the delay in acknowledging your response, but I’ll take another stab at it and report back with my code.

Cheers,

Jason