[SOLVED] Rule getting executed Twice

Hello everyone,

I am currently running openhab on Rpi3. I have created items, rule and sitemap file and I tested it before 2-3 months . It was working fine but now its causing me issue.

One of my rules is getting executed multiple times, without any reason .

Below i have mentioned my items, rules and sitemap files,.

sitemap demo label="Main Menu"
{
Frame label="STM32F4 Serial" icon="socket" {
			Text item=STM32F4							
			Switch item=Relay			
		}			
}

And here goes my Rules file:

rule "STM32F4"  
when   
Item STM32F4 received update  
then  
var String EPBUpdate = STM32F4.state.toString.trim  
end 

rule "Relay"
when   
 Item Relay changed  
then  
  var state = Relay.state as OnOffType  
  sendCommand (STM32F4 , "R" + state + "\r")  
end 

Here goes my items file:

String STM32F4 "STM32F4 [%s]" { serial="/dev/ttyUSB0" }                                                    
Switch Relay "Relay (PL3)" <contact>

I am trying to send my data from OH2 to my board serialy and it was working fine few months before.

Here is my event log file :

2018-11-29 17:06:34.762 [ome.event.ItemCommandEvent] - Item ‘Relay’ received command ON
2018-11-29 17:06:34.767 [vent.ItemStateChangedEvent] - Relay changed from OFF to ON
2018-11-29 17:06:34.770 [ome.event.ItemCommandEvent] - Item ‘STM32F4’ received command RON
2018-11-29 17:06:34.778 [vent.ItemStateChangedEvent] - STM32F4 changed from F to RON
2018-11-29 17:06:34.887 [vent.ItemStateChangedEvent] - STM32F4 changed from RON
to N
2018-11-29 17:06:35.741 [ome.event.ItemCommandEvent] - Item ‘Relay’ received command OFF
2018-11-29 17:06:35.745 [vent.ItemStateChangedEvent] - Relay changed from ON to OFF
2018-11-29 17:06:35.747 [ome.event.ItemCommandEvent] - Item ‘STM32F4’ received command ROFF
2018-11-29 17:06:35.751 [vent.ItemStateChangedEvent] - STM32F4 changed from N to ROFF
2018-11-29 17:06:35.867 [vent.ItemStateChangedEvent] - STM32F4 changed from ROFF
to R

Data is recieved correctly on the board serially. and relay is turning ON and OFF serialy. Its just my STM32F4 Item misbehaving , reason unknown ?

Can anyone tell were ios the issue ?

2018-11-29 17:06:34.778 [vent.ItemStateChangedEvent] - STM32F4 changed from F to RON
2018-11-29 17:06:34.887 [vent.ItemStateChangedEvent] - STM32F4 changed from RON
to N

and

2018-11-29 17:06:35.751 [vent.ItemStateChangedEvent] - STM32F4 changed from N to ROFF
2018-11-29 17:06:35.867 [vent.ItemStateChangedEvent] - STM32F4 changed from ROFF
to R

The first line is the change from the command from your rule. That’s working.
The second line is the item getting an update from the serial port. You serial device is answering to the command and your item updates accordingly.
I have a look at the serial binding docs and I didn’t see a way to set the port as an output only.
You need to configure your serial device so that it doesn’t respond when a command is sent.

You should changed your rule to:

rule "Relay"
when   
    Item Relay changed  
then  
    var String relayCommand = "R" + Relay.state.toString + "\r"
    STM32F4.sendCommand(relayCommand)  
end 

Or adapt the trigger (received update RON, changed from X to Y, whatever your intention there is)

BTW your first rule does not make sense, it just defines a local variable but does not use it.

1 Like

oops… Got it. And yes problem was my serial device was also transmitting.

Problem solved :slight_smile:

Yeah my serial device was also transmitting that caused prob. Fixed that issue now. Thank you :slight_smile: