Python script to not output below zero

Posted on
Wed Apr 19, 2023 2:23 am
mwoodage offline
User avatar
Posts: 174
Joined: Dec 19, 2014
Location: Devon UK

Python script to not output below zero

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

Posted on
Wed Apr 19, 2023 4:56 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script to not output below zero

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")

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Apr 19, 2023 5:29 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python script to not output below zero

Why all the conversions? You're converting your inputs to floats then to integers, then converting the output from an integer to a float.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Apr 19, 2023 6:22 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script to not output below zero

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

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Apr 19, 2023 6:50 am
mwoodage offline
User avatar
Posts: 174
Joined: Dec 19, 2014
Location: Devon UK

Re: Python script to not output below zero

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

Posted on
Wed Apr 19, 2023 7:08 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python script to not output below zero

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

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Apr 19, 2023 8:00 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Python script to not output below zero

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/

Posted on
Wed Apr 19, 2023 9:27 am
mwoodage offline
User avatar
Posts: 174
Joined: Dec 19, 2014
Location: Devon UK

Re: Python script to not output below zero

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest