Garage Door Switch sync with status

I’m using a zwave based garage door controller. It provides the value of 0 for closed and 255 for open. This requires a number item to map to, and I’d like to create a switch to use that maps to it. This switch will be mapped to HomeKit and Hue emulation so that I can speak the commands to make it so. I also want to use the switch in some automation rules.

The problem I’ve run into, is that I can’t seem to figure a solid way to “sync” the status and the switch state. If I use the switch in OH, I can react in sending the right # to the zwave device. But I also want to be sure that if the garage door is manually operated (more often than not), that the status change of that # (0/255) flips the virtual switch item state to the correct ON/OFF state.

I’ve attempted a few options, and end up either with an infinite loop running, or end up having a switch that doesn’t actually operate correctly and resets to the state before being pressed (aka it’s off, I press it to turn on, it does for a moment, then returns to off state - and the zwave device is reported as still being in the off state).

Current rule: (ON= CLOSED, OFF = OPEN / 0=CLOSED, 255 = OPEN)

rule "Garage Switch Monitor"
when
  Item garage_door_status changed or
  Item garage_door_switch changed
then
  if(garage_door_status.state == 0 && garage_door_switch.state != ON){
    postUpdate(garage_door_switch, ON)
      logInfo("rules.automation", "Garage Switch sync - Switch Update CLOSE")
  }
  else if(garage_door_status.state == 255 && garage_door_switch.state != OFF){
    postUpdate(garage_door_switch, OFF)
      logInfo("rules.automation", "Garage Switch sync - Switch Update OPEN")
  }
  else if(garage_door_switch.state == ON && garage_door_status.state != 0){
    sendCommand(garage_door_status, 0)
      logInfo("rules.automation", "Garage Switch sync - Status Update CLOSE")
  }
  else if(garage_door_switch.state == OFF && garage_door_status.state != 255){
    sendCommand(garage_door_status, 255)
      logInfo("rules.automation", "Garage Switch sync - Status Update OPEN")
  }
  else {
      logInfo("rules.automation", "Garage switch is already in sync")
  }
end

I can’t seem to get around this loop. And my brain is failing to think through it effectively to figure out how to sync these without causing the chain reaction each time.

Can you show the Items? I had a similar problem and had to change some settings in the items.

Michael

Have you seen this thread, there are similarities. Note in particular the separation of cause and effect possible with received command and received update rule triggers.

This is what I’ve settled on for the time being, which is broken out into 2 separate rules, and it does leverage the postUpdate function for syncing the Virtual switches when the physical switches are pressed. Unfortunately I can’t show the items really, as they were created inside the UI. Well one was manual, but it is just a Switch item with a name, it doesn’t have any actual information as it’s just a “virtual” switch being used to control from OH the open/close actions.

rule "Garage Switch Sync"
when
  Item garage_door_status changed
then
  if(garage_door_status.state == 0 && garage_door_switch.state == OFF){
    postUpdate(garage_door_switch, ON)
      logInfo("rules.automation", "Garage Switch sync - Switch Update CLOSE")
  }
  else if(garage_door_status.state == 255 && garage_door_switch.state == ON){
    postUpdate(garage_door_switch, OFF)
      logInfo("rules.automation", "Garage Switch sync - Switch Update OPEN")
  }
  else if(garage_door_status.state != 255 && garage_door_status.state !=0){
    logInfo("rules.automation", "Garage is in motion")
  }
  else {
    logInfo("rules.automation", "Garage switch is already in sync")
  }
end

rule "Garage Switch Activation"
when
  Item garage_door_switch changed
then
  if(garage_door_switch.state == ON && garage_door_status.state == 255){
    sendCommand(garage_door_status, 0)
      logInfo("rules.automation", "Garage Switch sync - Status Update CLOSE")
  }
  else if(garage_door_switch.state == OFF && garage_door_status.state == 0){
    sendCommand(garage_door_status, 255)
      logInfo("rules.automation", "Garage Switch sync - Status Update OPEN")
  }
  else {
      logInfo("rules.automation", "Garage switch is already in sync")
  }
end