Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

There are several different ways to add objects to loops in OpenStudio. I typically use the .addToNode method that all HVAC Components (Pumps, Chillers, Fans, etc) have. You don't see this method in the documentation for the PumpVariableSpeed class because this method is inherited from its base class, HVAC Component.

This code should work to disconnect the existing pump and add the new one to the same place on the loop:

  # Get the nodes before and after the existing pump on the loop     
  prev_node = selected_pump.inletModelObject.get.to_Node.get
  next_node = selected_pump.outletModelObject.get.to_Node.get

  # Remove the existing pump.  When this happens, either the pump's
  # inlet or outlet node will be deleted and the other will remain
  selected_pump.remove

  # Get the node that remains after deleting the existing pump
  remaining_node = nil
  if prev_node.outletModelObject.is_initialized
    remaining_node = prev_node
  elsif next_node.inletModelObject.is_initialized
    remaining_node = next_node
  end

  # Add the new pump to the node where the old pump was
  if remaining_node.nil?
    runner.registerError("Couldn't add the new pump to the loop after removing existing pump.")
    return false
  else
    new_pump.addToNode(remaining_node)
  end