Space Engineers

Space Engineers

Simple Battery Management
44 Comments
stiggan  [author] 24 Sep, 2020 @ 12:12pm 
I haven't played the game for a long time. In 2016 it worked with the game. What error do you receive?
Merlin 7 Sep, 2020 @ 6:50pm 
Is there a place where the versions of the game this works with is written?
stiggan  [author] 18 Feb, 2020 @ 1:59pm 
I apparently do.
Merlin 2 Jan, 2020 @ 12:49pm 
Who's listening to this in 2020?
Grifka 28 Jan, 2016 @ 5:16pm 
@stiggan, many thanks will be looking forward to the update :)
stiggan  [author] 28 Jan, 2016 @ 4:19pm 
@Grifka Per previous request I'm already looking in to having a filter option to choose which batteries the script will operate on. I've however had a lot to do lately so it's delayed, but there is a planned update on the script to add this.
Grifka 28 Jan, 2016 @ 9:23am 
@stiggan thanks for this script it works really well and delivers exactly what i need for my base.

I do however have a suggestion/request.

Currently my base has Solar Panels and batteries, the idea is the solar panels are the primary source of power during the day and the batteries are the source for the night cycles. My small ships (e.g miner, exploration, scout) are all battery powered and currently when my ships are parked (via connector) i want them to recharge their batteries when not being used. However this scipt doesn't see the two as seperate entities and so, i find that the batteries (on ships) are being depleted. And therefore are out of power when i need them.

Is there a way the script could be updated to exclude batteries that are on different ships? maybe by giving them a tag in the name which could identify which batteries are supposed to be controlled by the script? Or maybe add a tag search for batteries that i want the script to control?
Keyboard Wario 16 Jan, 2016 @ 2:43pm 
@expressocoffee you tell the timer to start after 3 seconds, and put timer into the setup actions of the timer at the end, to start it again.
lyzer 7 Jan, 2016 @ 4:27am 
The problem with the game is if you have batteries and solar panels it takes the electricity to run your base from your batteries instead of taking it from the solars (they have 0 output) so your script works perfectly for me. Since i have the windmill mod which are counted as reactors your script is even more fitting for me so it takes the solar, reactor (windwill) power first and then loads batteries or just uses them as backup. The problem is when i connect ships for example it manges their batteries to.
stiggan  [author] 6 Jan, 2016 @ 5:04pm 
This shouldn't be too hard to make, so I guess I could take a look at it. I haven't played the game for some months now, but from what I read from update logs this script seem obsolete these days? But sure, if you really want to I could take a look at it.
lyzer 6 Jan, 2016 @ 1:10pm 
I love your script but can you maybe let there be exceptions that you can set manually special betteries you want to set manually for example?
stiggan  [author] 25 Jul, 2015 @ 12:38am 
@Delta99
Any larger refresh on timer will work just fine, reason you can't keep a too short value is so changes has a chance to propagate properly and values changing before reading them again. Longer values are fine, it will just make the script take longer to react to changes in power load. I recomend as short value as possible.

I agree margin isn't a good name for this variable, it should be multiplier or similar. Margin just just multiply the total max power. You might want to lower it to say 0.95f, then it's same as making it 5% smaller than it actually is.

Offset is if you instead want to change it by a certain amount instead of a percentage. Offset value is added to the max power value.

Look at line 130 and line 131 and it should be quite clear. When doing a [variable] += [value]; it will add the value to the value in the variable.

In order for batteries to kick in earlier Offset should be a negative value, or margin should be a value smaller than 1.
Delta99 24 Jul, 2015 @ 10:17pm 
Couple of questions. What happens if you set the timer block on a larger refresh like 5 or 10 seconds or even longer? Will that make things not work well?

And I don't understand either of your comments in the Main() part of the code that seem to be adjustable entries for the two variables (offset and margin). Can you explain in more detail?
stiggan  [author] 12 May, 2015 @ 10:14am 
Seemed there was a bug with the new refinery/assembler modules and this script. Turned out to be a false alarm, seems the modules are considered to be batteries, and gets turned on and off, but it doesn't seem to make them stop working. For now the issue seem to be just cosmetical.
texix [FR] 14 Apr, 2015 @ 6:19pm 
be it rename its battery work?
is it possible to have a video tutorial?
stiggan  [author] 14 Apr, 2015 @ 3:46pm 
@texix I've done tests now, and I see no issue with your mods, they work fine together with the script for me. There is one difference from when I wrote the script, which I should perhaps take a look at. Now max value on solar panels work, back when this script was written it would always be zero.

I'm not sure why it's not working for you. For me batteries stayed on charge until the load was too high, then it would switch batteries on to accomodate for the extra load. I used a large thruster to create the load.

This rig had a reactor, but it shouldn't matter.

Did you try to run the script or switch the batteries to charging manually? Perhaps I can take a look at your rig to see if I can find the problem?
stiggan  [author] 14 Apr, 2015 @ 2:47pm 
@texix I don't know, the script is calculating the values by reading the text readout from the individual blocks, it looks for certain keywords in their printout since that's the only way to know if generators are working at maximum capacity or not. Are these keywords or values different or missing in any way, then the script wont calculate load correctly. Well, it's not the only way, but the one I found most reliable.

I'll take a look and see what I can find out.
texix [FR] 14 Apr, 2015 @ 10:14am 
I use two mods broad solar panels + 25% and batteries (Li-ION-NimH-NiZn)
https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=371384476
https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=301049181
so the script is not compatible with the mods?
stiggan  [author] 13 Apr, 2015 @ 7:25am 
@texix Did you try switching batteries to discharge manually? Did they switch back to charging? Are you using standard batteries/solar panels/reactors? As written earlier, symptom on having a too short timer is batteries switching forth and back, batteries not switching at all is something else.
texix [FR] 12 Apr, 2015 @ 5:49pm 
I have an issue with your script, I am 1200% load as solar cells give even more power but the batteries are recharged. Timer is set to 3 seconds, but nothing happens. No, even if I turn off the reactors.
cy-one 8 Apr, 2015 @ 3:03am 
Gna @ combatting above 30% :D
I can see why that would happen... Ah well, I might need to start learning C# then >.< Or whatever is used...

But thanks for the example of your large carrier XD
stiggan  [author] 7 Apr, 2015 @ 3:19pm 
@cy-one I forgot to tell this, when we first made this script we used it on a rather large carrier, instead of reactors we had batteries, like 3 small reactors to power it all when idle and charge the batteries.... it worked awesome! No need to use lots of conveyor designs to move fuel to reactors, there was only the 3 second kick-in before the engines would go full power when moving, but it was kind of charming.

However as I remember the titan engine is quite a drainer, I suspect it to be easier and quite a space saver to use some large reactors instead of lots of batteries. But I might be wrong, I haven't done the calculation. I anyway like the design of using reactors just for continues power and batteries for power burst, hence why the script works like this.
stiggan  [author] 7 Apr, 2015 @ 3:10pm 
@cy-one I haven't tried running his script, mostly cause it doesn't interest me. I would assume however his and this script would start combating if both are run at the same time once charge is above 30%.

@matthias22101987 I hope you can get back with some more information, when we made this script the goal was to make a small script which did one thing, and did it well. If it doesn't work with certain mods, perhaps there is a issue with parsing, and I'd like to solve such issues.
cy-one 7 Apr, 2015 @ 1:00pm 
I love the idea of this script, I hope I will be able to use it to manage my Titan Engines on my industrial mothership. They won't be used often, so having them get their energy from batteries sounds awesome.

Buuuut I kinda love this script https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=372496767&searchtext=script+battery more. Is your script with that one compatible?
stiggan  [author] 7 Apr, 2015 @ 10:25am 
I should add, common symptom on having too short timer delay is batteries switching on and off in a endless loop.... or doing like this for a little while before stabilizing, if this does not describe the issue then timer should be fine in most cases.
stiggan  [author] 7 Apr, 2015 @ 10:21am 
Just got home, and quite a while since I looked at this script, so I'm a bit rusty on exactly how the code works right now. I assume you with open mean they're on charging. Have you tried setting them to discharge by yourself? Does the script switch them back to charging when you do? If yes, then there is something wrong with how the load is calculated for your case, else it's a starvation issue.
Raziel 3 Apr, 2015 @ 11:00pm 
So I'm at 160% load (Naquadah and Arc Fuel empty) only the solar cells and the ShipShield components give even more electricity but the batteries remain fresh and cheerful on "Open". Timer is set to 3 seconds but nothing happens. Not even if I turn off the reactors.
stiggan  [author] 10 Jan, 2015 @ 6:00pm 
@Anthropy
2 seconds is flimsy at best, 3 seconds worked for me so far. Issue is that it takes some time for details to update, and in this case if details update to show grid getting power from 1 battery, but an other battery hasn't updated being on charge yet. If this happends then it will see the drain as a real drain, and turn everything on, just to realise it's a false alarm and turn it off again. This can't really be solved in script but is limited by game mechanics I think.

There always gonna be a delay on the batteries with this script, so if it doesn't work for you, I hope you'll find a better solution. Scripting wont solve it unless we can make event based scripting.

Thanks about code! Myself I think it's far from perfect, but I've tried to write it clean and easy to understand. The script shouldn't be too hard for someone with a little programming skill to modify if for example want batteries and solar to cover reactor usage instead.
a 10 Jan, 2015 @ 4:16pm 
@Stiggan @Malekeh I had it at 2 seconds which may have been too fast, at 5 seconds it seems more stable, although also too slow; if I turn on a refinery or assembler the power is lost for a short period of time, which causes gravity to turn off and all connectors to disengage. Considering how complex it is to get it running with the current mod API at all I do think you guys did a good job at it, the code looks clean even though you have to parse text in order to get the power readings at all.
Malakeh  [author] 10 Jan, 2015 @ 3:38pm 
@Anthropy

Hi and thnx for the feedback, can you tell us a bit more in detail how u setup the script to run. The timer more specifically?
stiggan  [author] 10 Jan, 2015 @ 2:48pm 
@Anthropy
If you look at how battery power is calculated, the script doesn't count power drained from charging batteries, script doesn't care if charging batteries overload the grid. Try it with a longer delay between runs, perhaps 5 seconds, cause what you experienced sounds like the script is run again before all details on the blocks has updated.

You wont loose charge, cause the power used to discharge one battery for a few seconds is used to charge the rest. So it simply redistribute power between batteries, and with 50 batteries it pretty much just gonna even out how much power is stored in each. There is a "toggleRandomBattery(2);" in Main(), I would set it to like 200 instead of 2, perhaps more even.

Generaly, if the script turns on and off and on and off charging on all batteries, when charging should be on, then you most likely have a too short delay between runs. With 250 batteries, and some solar panels, you most likely need a longer delay than 3 seconds.
a 10 Jan, 2015 @ 1:56pm 
There's too much random factors and attempts to work around current limitation for the script to work well.
I installed it on my station with 50 batteries and 200 capacitors and 1.2MW worth solar panels, the result was that it would set all batteries to charge, causing the script to think the power was overloaded and set all the batteries to discharge, and then back on as it realizes there's plenty power to recharge some batteries, etc.
Also if 50 batteries are charging and you randomly turn on one capacitor, it'll drain in a matter of seconds, and if you do that with a lot of capacitors at random they will eventually all be drained.

Tl;dr the result of your script was that all my batteries were eventually empty and I couldn't run my assemblers and refineries anymore for 10 minutes ;) I think it may work for small ship scenarios with more reactors and less batteries, but for solar-powered stations not so much.
Malakeh  [author] 10 Jan, 2015 @ 9:27am 
@Shukari if the overload you are experiencing is due to the batteries being charged and not due to the actual power loads u have in your system, the script will not enable them.

An easy way to see this is by turning off completly the batteries, if the base is not overloading anymore means that the fake oveload you experience is because of the batteries charging.

The overload you exprience during battery charging is not completly true as the batteries will only receive the energy that is left after all your other power loads will be satisfied.

The game instead of calculating the real power that the batteries are receiving, it calculates the maximum amount of power they can receive when they give you the percentage on your HUD, so pls ignore that message when the batteries are charging.

When a REAL oveload will happen then the batteries will kick in as they should!
stiggan  [author] 9 Jan, 2015 @ 9:32am 
This previous mentioned issue will also make it so if the power spike from turning the gravity gens on is larger than what 1 battery provides, then it might not work as well. The solution then would be to add one line of code in Main(). Under the line "toggleRandomBattery(2);" add an other just like it.

Perhaps change the 2 to 4 the lessen the chance of having 2 batteries turned on at the same time. 2 means the chance is 4/6 to turn on a batteryeach turn, while 4 would mean 4/8 (50%).

Please get back to me about your findings, if this or the timer solves it for you, or if it doesn't.
stiggan  [author] 9 Jan, 2015 @ 9:21am 
@shukari
Issue is the difference on the actual power grid and what game tells you. Game tells you how much overloaded you are in order to run everything. This is not what it looks like to the script.

Gravity gen starts, but there is not enough power to turn it on, so it stay off. This means there is no power drain from the gravity gen, since it stay off. This issue I've had lots of trouble solving, and the current solution isn't very pretty (or good), but it should work even tho it might take some time to kick in.

Try setting timer to longer. If this doesn't work, I can try to take a look at your base to see if I can find the error.
shukari 9 Jan, 2015 @ 6:45am 
I have a station with one reactor, 4 batteries, many gravity generators, timer block and a programm block. Now i turn the gravity generators an on my station use 140% power but the batteries dont change from loading to power.....
Malakeh  [author] 5 Jan, 2015 @ 6:10pm 
@scarvig
It works with multiple batteries.
It does not take into account the whether the batteries are full or not.

It enables the batteries to discharge only when you have a peak demand of power. Meaning the batteries act as a backup to cover any extra power needs you may have that cannot be fullfilled by either solar panels or reactors.

In the same manner the batteries will only be charged when the reactor and solar panels are not at full capacity and they have excess energy to spare.

It works also if u have no reactors.
scarvig 5 Jan, 2015 @ 5:26pm 
does it differentiate between having enougth spare energy to actually load a battery, having no spare and actually needing to use stored energy?
and does it work with multiple batterys?
Dak Smith 5 Jan, 2015 @ 10:02am 
Great work!
Malakeh  [author] 5 Jan, 2015 @ 1:26am 
The Script is supposed to use Batteries as a backup, and enable them if the usage of the ship/station exceeds the power installed in reactor and solar panels. So the batteries will try to cover always the peak power demands of your ship.
May that be because of damage to reactors and solar panels or increased loads you may have such as overload due to multiple thrusters being used in conjuction with assembler and refinery.
stiggan  [author] 4 Jan, 2015 @ 11:54pm 
Crookedstar , yes. Is the reactor being hacked, or damaged enough to not work, then the script will consider it same as not enabled. Future updates of the game might break this tho, even tho it shouldn't. The way I detect reactor being damaged is both official way (which doesn't seem to work for me at the moment) and by looking in the details. If there is no output field in details, then it's considered damaged, same goes for both small and large reactors. I consider this output field thing a bug, so it should get fixed in future versions of the game.
Captain_Crooked 4 Jan, 2015 @ 11:13pm 
So, theoretically, if i generally have more power in my reactors then what I use, but battle damage destroys some of my reactors to the point that I dont have enough anymore. This script seeing my remaining reactors are insuffuciant, it will then activate the batteries to discharge?
stiggan  [author] 3 Jan, 2015 @ 3:19pm 
Thanks, I'm currently working on a better version with more checks. For example it counts unfinished reactors in its current state, something which should be fixed in my working version.
Sigurd Hansen 3 Jan, 2015 @ 3:14pm 
Cool. Actually, battery management was on my todo list for my stop rotor script as I need it for my probe.
Nice clean code.