Energy consumption simulation

Haii guys,

I am trying to obtain the energy consumption data of a machine in statistics (graphical representation). In the component modeling of a machine, I have defined Idle, Working & Off power with some values in kW. And I also have a Python script to calculate the total energy consumption during simulation.
(Screenshots attached)
But when I insert graphs from statistics, it is constant and does not show Idle, Working & Off power that are dynamically varying in the graph during simulation. Also it does not calculate Total energy consumption value.

I dont know where am I going wrong. Kindly share your thoughts to figure this out.
Thanks :slight_smile:

The OnRun doesn’t contain a loop, so it just calculates and sets the property values once in beginning of the simulation.

Typically OnRun is used as follows, if the goal is to run a cycle with constant interval:

def OnRun():
  dt = 1.0 # 1 second
  while True:
    # your logic here
    delay(dt)

Now that your script is reading current values of properties and there’s already the variable lastTime to calculate the time difference from latest call, you could use OnSimulationUpdate for the logic:

def OnSimulationUpdate(simtime):
  global lastTime
  dt = simtime - lastTime
  lastTime = simtime
  # your logic here

This method is called every time the scene is updated. With 1x simulation speed and empty layout it’s called every 40 milliseconds. Some components can call getSimulation().update(), and then the method would be called more frequently with uneven intervals.
image

Ps. I’ll move this topic to Python Programming.

Thank you so much for your reply.
But still, I am not able to plot the graph that changes dynamically during simulation for the “Total Energy Consumption.”

Does the EnergyConsumed property change value, but the value is not reflected in statistics dashboard? Check the statistics interval setting from Home tab. Is it a very large number, e.g. 3600 seconds? Data points are plotted following that interval, not any more often.

What does the script look like after implementing my tips?