Page 1 of 1

Python script to not output below zero

PostPosted: Wed Apr 19, 2023 2:23 am
by mwoodage
Hello,
I'm after some help with the following script, everything works well, but the output will go into a minus figure at some points during the day, but i'd like it to stop when it gets to zero.
Is there something i can add to the end of the script to stop it from going lower than zero?

Code: Select all
Roof_Solar_Battery_Combined = int(float(indigo.variables[7675940965].value))

Immersion_Power = int(float(indigo.variables[2887052].value))

Battery_Power = int(float(indigo.variables[7292602006].value))

Available_Power = Roof_Solar_Battery_Combined - Immersion_Power - Battery_Power
indigo.variable.updateValue(3319680207, value=str (float(Available_Power)))


Thanks in advance
Martin

Re: Python script to not output below zero

PostPosted: Wed Apr 19, 2023 4:56 am
by DaveL17
This is what you asked for:

Code: Select all
Roof_Solar_Battery_Combined = int(float(indigo.variables[7675940965].value))

Immersion_Power = int(float(indigo.variables[2887052].value))

Battery_Power = int(float(indigo.variables[7292602006].value))

Available_Power = Roof_Solar_Battery_Combined - Immersion_Power - Battery_Power

if Available_Power >= 0:
    indigo.variable.updateValue(3319680207, value=str(float(Available_Power)))

But if you want it to log zero when the value goes negative, then you want this:

Code: Select all
Roof_Solar_Battery_Combined = int(float(indigo.variables[7675940965].value))

Immersion_Power = int(float(indigo.variables[2887052].value))

Battery_Power = int(float(indigo.variables[7292602006].value))

Available_Power = Roof_Solar_Battery_Combined - Immersion_Power - Battery_Power

if Available_Power > 0:
    indigo.variable.updateValue(3319680207, value=str(float(Available_Power)))
else:
    indigo.variable.updateValue(3319680207, value="0")

Re: Python script to not output below zero

PostPosted: Wed Apr 19, 2023 5:29 am
by FlyingDiver
Why all the conversions? You're converting your inputs to floats then to integers, then converting the output from an integer to a float.

Re: Python script to not output below zero

PostPosted: Wed Apr 19, 2023 6:22 am
by DaveL17
Joe has a good point. The code can be simplified:

Code: Select all
Roof_Solar_Battery_Combined = float(indigo.variables[7675940965].value)

Immersion_Power = float(indigo.variables[2887052].value)

Battery_Power = float(indigo.variables[7292602006].value)

Available_Power = Roof_Solar_Battery_Combined - Immersion_Power - Battery_Power

if Available_Power >= 0:
    indigo.variable.updateValue(3319680207, value=str(Available_Power))
or

Code: Select all
Roof_Solar_Battery_Combined = float(indigo.variables[7675940965].value)

Immersion_Power = float(indigo.variables[2887052].value)

Battery_Power = float(indigo.variables[7292602006].value)

Available_Power = Roof_Solar_Battery_Combined - Immersion_Power - Battery_Power

if Available_Power > 0:
    indigo.variable.updateValue(3319680207, value=str(Available_Power))
else:
    indigo.variable.updateValue(3319680207, value="0")
Plus, all the conversions can have an effect on the outcome. Consider:

Code: Select all
int(10.4 - 1.4 - 1.4)  # equals 7

int(10.4) - int(1.4) - int(1.4)  # equals 8

Re: Python script to not output below zero

PostPosted: Wed Apr 19, 2023 6:50 am
by mwoodage
Thanks Joe, Dave,
I've very little knowledge of python coding, i adapted something i'd seen / used before and just fiddled around with it until it worked. Really appreciate the simplified code, i've gone for this one and it seams to be working but won't know until the sun goes in tonight and stops producing power!

Code: Select all
Roof_Solar_Battery_Combined = float(indigo.variables[7675940965].value)

Immersion_Power = float(indigo.variables[2887052].value)

Battery_Power = float(indigo.variables[7292602006].value)

Available_Power = Roof_Solar_Battery_Combined - Immersion_Power - Battery_Power

if Available_Power >= 0:
    indigo.variable.updateValue(3319680207, value=str(Available_Power))


Out of interest, how does this example work?

Code: Select all
int(10.4 - 1.4 - 1.4)  # equals 7

int(10.4) - int(1.4) - int(1.4)  # equals 8


Thnaks again guys for the speedy reply :D

Re: Python script to not output below zero

PostPosted: Wed Apr 19, 2023 7:08 am
by DaveL17
In Python, int() doesn't round the value, it truncates it. So int(10.8) yields 10. So:

int(10.4 - 1.4 - 1.4) is 10.4 minus 1.4 (equals 9) minus 1.4 (equals 7.6). int(7.6) equals 7

int(10.4) - int(1.4) - int(1.4) is 10 minus 1 minus 1 equals 8

Re: Python script to not output below zero

PostPosted: Wed Apr 19, 2023 8:00 am
by berkinet
Just as a matter of style and compactness. This
Code: Select all
if Available_Power > 0:
    indigo.variable.updateValue(3319680207, value=str(float(Available_Power)))
else:
    indigo.variable.updateValue(3319680207, value="0")
could be reduced to this
Code: Select all
if Available_Power < 0:
    Available_Power = 0
indigo.variable.updateValue(3319680207, value=str(Available_Power))
Thanks to one of my favorite books: https://amazon.com/Elements-Programming ... 070342075/

Re: Python script to not output below zero

PostPosted: Wed Apr 19, 2023 9:27 am
by mwoodage
Wow, thanks Dave, Berkinet for taking the time to explain, I've still got so much to learn! :D

Not sure i still fully understand but i'll give the reduced scripts a try and see how they work,

Cheers all,
Martin