# Python code for replicating energy storage behaviour

Hi to all,

I am new in the forum and Python. I am trying to create basic Python code to replicate a battery storage behavior. My definition have a series of input values:
input 1 (x or x0) = is the first number of the series and represents the capacity of a energy battery (i.e. 50)

input 2 (y) = is an hourly energy consumption list for the whole year

input 3(z) = is an hourly energy production list of the photovoltaic panels for the whole year

and the definition of the battery charge and discharge is something like this:

if(y-z+x(i)>0;0;if(y-z+x(i)<x0;x0;y-z+x(I))

I wonder how can I create a loop in python so that represents how much does the battery charge and discharge.
I have try something like this but there is no results, I am not sure what am I doing wrong.

``````def battery_simulation(consumption, production, initial_capacity):
"""Simulates the state of an energy battery over a year.

Args:
consumption: A list of hourly energy consumption values.
production: A list of hourly energy production values.
initial_capacity: The initial capacity of the battery.

Returns:
A list of hourly battery states.
"""

battery_state = []
capacity = initial_capacity

for i in range(len(consumption)):
net_energy = consumption[i] - production[i] + capacity
if net_energy > 0:
battery_state.append(0)
capacity = 0
elif net_energy < initial_capacity:
battery_state.append(initial_capacity)
capacity = net_energy
else:
battery_state.append(net_energy)
capacity = net_energy
return battery_state
``````

Thanks

What do you mean â€śthere is no resultsâ€ť? You run it and an empty list is returned?

Hi,

being that Python is an indentation centric language, I would recommend increasing your tab indentation to four spaces this way it improves its readability. Right now, there are only two spaces.

On to the code. I think that you have to reassess your code decision making logic. When it first comes into the first conditional statement, it checks if `net_energy` is positive. If it is, it executes the statement and appends a `0` to the `battery_state` list. If it is a negative value, it moves on to the next conditional statement and checks if it is less than `initial_capacity`. Since there is no such thing as a negative `initial capacity` (just like your carâ€™s gas tank canâ€™t have a negative gallonâ€™s value), this all but ensures that the last alternative statement never gets executed - the one shown here:

``````battery_state.append(net_energy)
``````

Review your logic and ensure that it does what you actually want it to do.

1 Like

hi @brass75, yes, that is exactly what I mean.

I donâ€™t know if is a mistake in the python definition nor how to print the results of the loop.

hi @onePythonUser
I made some arrangements but i still donâ€™t obtain any results:

``````def battery_simulation(consumption, production, initial_capacity):
"""Simulates the state of an energy battery over a year.

Args:
consumption: A list of hourly energy consumption values.
production: A list of hourly energy production values.
initial_capacity: The initial capacity of the battery.

Returns:
A list of hourly battery states.
"""

battery_state = []
capacity = initial_capacity

for i in range(len(consumption)):
net_energy = consumption[i] - production[i] + capacity
if net_energy > 0:
battery_state.append(0)
capacity = 0
else:
battery_state.append(initial_capacity)
capacity = net_energy

return battery_state
print(battery_state)
``````

Thanks

I see that you have added a `print` statement under the `return` statement. As written, it will never be executed. Once a `return` statement is encountered, the interpreter will execute it and exit the function immediately. If you want that `print` statement to execute, put it before the `return` statement.

You have removed the last conditional statement therefore removing the last prior version append statement. Is this what you want it to do? To completely disregard this statement? Please review your requirements for this project - carefully.

As @onePythonUser stated, that `print` will never do anything since itâ€™s part of the function but after the `return`.
As to why it returns an empty `list`, the only reason could be that you are calling it with an empty list for `consumption`.
One personal tweak I would make (to make the code more understandable) is changing the variable name `net_energy` to `net_energy_consumed`.

could you please provide me a very simple example of how to do it?
My knowledge in python is practically inexistent, so far this code does not work

``````def battery_simulation(consumption, production, initial_capacity):
"""Simulates the state of an energy battery over a year.

Args:
consumption: A list of hourly energy consumption values.
production: A list of hourly energy production values.
initial_capacity: The initial capacity of the battery.

Returns:
A list of hourly battery states.
"""

battery_state = []
capacity = initial_capacity

for i in range(len(consumption)):
net_energy = consumption[i] - production[i] + capacity
if net_energy > 0:
battery_state.append(0)
capacity = 0
elif net_energy < initial_capacity:
battery_state.append(initial_capacity)
capacity = net_energy
else:
battery_state.append(net_energy)
capacity = net_energy
print battery_state
return battery_state
``````

I just want to replicate the behavior of a battery that has a limited capacity, so it charges and discharges based on electricity production and electricity consumption list for every hour.
The argument works in excel, but I would like to make a code so to integrate it in other programs.

input 1 (x0) = is the first number of the series and represents the capacity of a energy battery (i.e. 50)

input 2 (y) = is an hourly energy consumption list for the whole year

input 3(z) = is an hourly energy production list of the photovoltaic panels for the whole year

x(i) is the resulting battery state after charge or discharge that feeds again to the next hour
and the definition is something like this:

if(y-z+x(i)>0;0;if(y-z+x(i)<x0;x0;y-z+x(i))

As written, this is not a Python issue. This is a logic (how youâ€™re understanding the problem) issue. Python code will only do what you tell it to do. So you first have to understand what the problem requires. Once having done so, then you can begin interpreting your `pseudo code` into functioning `Python` code. By `pseudo code` I mean your understanding of how the problem should be implemented (whether written or not).

Again, look at the logical sequence of the conditional statements. The first one checks if the expression is greater than `0`; i.e., if it is a positive value - ALL positive values. If the first conditional statement is not satisfied, this implies that the result must have a negative value. Since the initial capacity MUST be positive (no such thing as a negative capacity - it is a battery after all), then the second conditional statement will be satisfied â€¦ by default. As written, the third conditional statement will NEVER be encountered.

This is why I recommend reviewing the problem at hand and making sure that you understand it fully before you begin writing Python code. Even if you write Python code with no syntax errors, it will not mean that it will do what it is intended to do.

This is the `pseudo` code that you provided by the way:

``````if(y - z + x(i)) > 0;      # y-z+x(i) is positive
0;   # append '0'

if(y - z + x(i)) < x0;     # y-z+x(i) is negative by default (includes zero value)
x0;  # append 'initial state'

y - z + x(i):
# append net energy
``````

Perhaps you need to revise the `pseudo` code into something more practical. Instead of checking for positive and negative values, you may have to consider ranges of values.

Here is a simple test where we sweep potential values for the `y - z + x(i)` expression. Note that the following values are included:

1. Less than zero (negative)
2. Equal to zero
3. Greater than zero (all positive values)
a. greater than zero and less than `initial_capacity`
b. equal to `initial_capacity`
c. greater than `initial_capacity`

Despite all of these test values, it NEVER satisfies the third and final default conditional statement.

``````initial_capacity = 50

# This list simulates potential values for the `y - z + x(i)` expression
test_values = [-45, -25, -10, 0, 10, 30, 45, 50, 65, 80]

for value in test_values:

if value > 0:
print(f"'y - z + x(i)' = {value:>3} is greater than zero.")

elif value < initial_capacity:
print(f"'y - z + x(i)' = {value:>3} is less than {initial_capacity}.")

else:
print('Entered the last conditional statement.')

``````