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

Correct way to use DataExchange API

asked 2020-11-01 16:59:32 -0600

Red's avatar

updated 2020-11-01 22:02:40 -0600

I am trying to use the DataExchange API to read the hourly "Site Outdoor Air Drybulb Temperature" variable. The code I use is from the official documentation:

one_time = True
api_ready = False
outdoor_dry_sensor = 0
outdoor_temp = []
def time_step_handler(state):
  global api_ready, one_time, outdoor_dry_sensor
  sys.stdout.flush()
  if one_time:
    if api.exchange.api_data_fully_ready(state):
      api_ready = True
      outdoor_dry_sensor = api.exchange.get_variable_handle(
          state, u"Site Outdoor Air Drybulb Temperature", u"ENVIRONMENT"
      )
      if outdoor_dry_sensor == -1:
        sys.exit(1)
      one_time = False
  if api_ready:
    oa_temp = api.exchange.get_variable_value(state, outdoor_dry_sensor)
    outdoor_temp.append(oa_temp)
api = EnergyPlusAPI()
state = api.state_manager.new_state()
api.runtime.callback_end_zone_timestep_after_zone_reporting(state, time_step_handler)
api.runtime.run_energyplus(args)

These are some of my relevant simulation parameters:

Timestep = 1
RunPeriod from 1/1 to 1/2
SizingPeriod:WeatherFileDays from 1/1 to 1/2

so I expected to get an "outdoor_temp" of size 48 that has weather data from day1 and day2. but I get an "outdoor_temp" of size 240 that has weather data of day1 and day2 in this order:

day1 day1 day1 day1 day2 day1 day1 day1 day1 day2

What am I doing wrong?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-12-14 12:53:06 -0600

hermmanhender's avatar

Hi. I was doing some tests with the API and I came across some things like that.

First, you could use the api.exchange.warmup_flag () function to avoid calculations during the warm-up stage of the simulation. It is easy to use a conditional if api.exchange.warmup_flag (state) == 0: which checks for equality to 0 (the function returns 1 if it is warm-up and 0 otherwise).

Second, it sure returns all that amount of information and not the 48 measurements that you would like because you are calculating in each timestep of the simulation. You can use a conditional as follows if api.exchange.zone_time_step_number (state)% n == 0 :, where n would be equal to the timestep that you have configured in your IDF if you want hourly values.

I hope you find it useful.

Greetings.

edit flag offensive delete link more

Comments

Thank you @hermmanhender. Those 3 extra repetitions of day1 of simulation time were indeed due to the warmup stages. I did find a way to avoid that, but your solution is easier. My other problem was setting SimulationControl:Run Simulation for Sizing Periods to Yes, which caused another repetition in my output.

Red's avatar Red  ( 2020-12-16 08:38:33 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

2 followers

Stats

Asked: 2020-11-01 16:59:32 -0600

Seen: 367 times

Last updated: Dec 16 '20