Page 1 of 1

Python script help for basic maths

PostPosted: Sun Jul 29, 2018 12:02 pm
by petematheson
Hi All,

Not great with Python scripting and need a hand with some basic maths to calculate my E7 electric cost.

I have the following variables:

E7DayRate
E7NightRate
E7Standing
SmappeeTodayCost
SmappeeTodayDaykWh
SmappeeTodayMorningkWh
SmappeeTodayNightkWh

I'm looking for a script that will do the following:

((SmappeeTodayDaykWh - SmappeeTodayNightkWh) * E7DayRate) + ((SmappeeTodayNightkWh - SmappeeTodayMorningkWh) * E7NightRate) + E7Standing = SmappeeTodayCost

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 1:23 pm
by FlyingDiver
Those are Indigo variables?

Code: Select all

E7Standing    = indigo.variables["E7Standing"].value
E7DayRate    = indigo.variables["E7DayRate"].value
E7NightRate    = indigo.variables["E7NightRate"].value

SmappeeTodayDaykWh    = indigo.variables["SmappeeTodayDaykWh"].value
SmappeeTodayNightkWh    = indigo.variables["SmappeeTodayNightkWh"].value
SmappeeTodayNightkWh    = indigo.variables["SmappeeTodayNightkWh"].value
SmappeeTodayMorningkWh    = indigo.variables["SmappeeTodayMorningkWh"].value

SmappeeTodayCost= ((SmappeeTodayDaykWh - SmappeeTodayNightkWh) * E7DayRate) + ((SmappeeTodayNightkWh - SmappeeTodayMorningkWh) * E7NightRate) + E7Standing

indigo.variable.updateValue("SmappeeTodayCost", str(SmappeeTodayCost))



Or something like that.

But, in general, it's better to use the variable ID instead of the name.

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 2:33 pm
by petematheson
Ah thanks for this!

Tried running that and got an error:
Script Error embedded script: unsupported operand type(s) for -: 'unicode' and 'unicode'
Script Error Exception Traceback (most recent call shown last):
embedded script, line 10, at top level
TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 2:44 pm
by FlyingDiver
Oh, right.

Code: Select all

E7Standing    = int(indigo.variables["E7Standing"].value)
E7DayRate    = int(indigo.variables["E7DayRate"].value)
E7NightRate    = int(indigo.variables["E7NightRate"].value(

SmappeeTodayDaykWh  = int(indigo.variables["SmappeeTodayDaykWh"].value)
SmappeeTodayNightkWh    = int(indigo.variables["SmappeeTodayNightkWh"].value)
SmappeeTodayNightkWh    = int(indigo.variables["SmappeeTodayNightkWh"].value)
SmappeeTodayMorningkWh    = int(indigo.variables["SmappeeTodayMorningkWh"].value)

SmappeeTodayCost= ((SmappeeTodayDaykWh - SmappeeTodayNightkWh) * E7DayRate) + ((SmappeeTodayNightkWh - SmappeeTodayMorningkWh) * E7NightRate) + E7Standing

indigo.variable.updateValue("SmappeeTodayCost", str(SmappeeTodayCost))


Or I think maybe you can use .intValue instead of .value and then you don't need the cast. I don't write much code that deals with Indigo Variables.

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 2:50 pm
by petematheson
It seems to be the maths causing the issue.

Night = 4
Today = 10

If I have
SmappeeTodayCost = SmappeeTodayNightkWh+SmappeeTodayDaykWh
Then i get 410 as the value for Cost (but doesn't do addition)

If I have
SmappeeTodayCost= SmappeeTodayDaykWh-SmappeeTodayNightkWh
It throws that error.

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 3:24 pm
by FlyingDiver
I just copied your formula verbatim. I don't know what the variables mean so I couldn't check the math.

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 3:26 pm
by CliveS
That is because 4 and 10 are strings, you need to use int to change them to numbers. And I get confused with Python as well !

Example
>>> x = '100'
>>> y = '-90'
>>> print x + y
100-90
>>> print int(x) + int(y)
10

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 3:35 pm
by petematheson
Definitely on the right track :)

SmappeeTodayCost= int(SmappeeTodayDaykWh) + int(SmappeeTodayNightkWh)

Gives me
Script Error embedded script: invalid literal for int() with base 10: '11.07'
Script Error Exception Traceback (most recent call shown last):

embedded script, line 10, at top level
ValueError: invalid literal for int() with base 10: '11.07'

If I change my SmappeeTodayDaykWh to 2 digitals (11 rather than 11.07) then the calculation works.

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 3:38 pm
by FlyingDiver
change all the those int() casts to float().

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 3:52 pm
by petematheson
ooooh close!

SmappeeTodayCost= float(SmappeeTodayDaykWh)-float(SmappeeTodayNightkWh)
Works

SmappeeTodayCost= (float(SmappeeTodayDaykWh)-float(SmappeeTodayNightkWh))*float(E7DayRate)
Errors with
Embedded script executor host started
Error XML Parse Error: not well-formed (invalid token)
Error On character 219 of line number 1.
Error XML Parse Error: not well-formed (invalid token)
Error On character 325 of line number 1.
Error XML Parse Error: not well-formed (invalid token)
Error On character 350 of line number 1.
Error because embedded scripts execute sequentially they must complete their execution within 10 seconds.
Error modify the script to exit quickly or convert it to an external file (not embedded).
Stopping embedded script executor host (pid 3467)
Stopped "embedded script executor host"

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 4:00 pm
by petematheson
Add my bad, I had the Rate variables as a currency (£0.1378) etc...

Calculations now work but I'm just going to check the maths.

E7Standing = indigo.variables["E7Standing"].value
E7DayRate = indigo.variables["E7DayRate"].value
E7NightRate = indigo.variables["E7NightRate"].value

SmappeeTodayDaykWh = indigo.variables["SmappeeTodayDaykWh"].value
SmappeeTodayNightkWh = indigo.variables["SmappeeTodayNightkWh"].value
SmappeeTodayNightkWh = indigo.variables["SmappeeTodayNightkWh"].value
SmappeeTodayMorningkWh = indigo.variables["SmappeeTodayMorningkWh"].value

SmappeeTodayCost= (((float(SmappeeTodayDaykWh) - float(SmappeeTodayNightkWh)) * float(E7DayRate))) + (((float(SmappeeTodayNightkWh) - float(SmappeeTodayMorningkWh)) * float(E7NightRate)))

indigo.variable.updateValue("SmappeeTodayCost", str(SmappeeTodayCost))

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 4:05 pm
by FlyingDiver
Convert them to floats when you get the values from the indigo variables, like I did in my second example. Makes the calculation much easier to read.

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 4:09 pm
by petematheson
FlyingDiver wrote:
Convert them to floats when you get the values from the indigo variables, like I did in my second example. Makes the calculation much easier to read.


Gotcha:

E7Standing = float(indigo.variables["E7Standing"].value)
E7DayRate = float(indigo.variables["E7DayRate"].value)
E7NightRate = float(indigo.variables["E7NightRate"].value)
SmappeeTodayDaykWh = float(indigo.variables["SmappeeTodayDaykWh"].value)
SmappeeTodayNightkWh = float(indigo.variables["SmappeeTodayNightkWh"].value)
SmappeeTodayMorningkWh = float(indigo.variables["SmappeeTodayMorningkWh"].value)

SmappeeTodayCost= (((SmappeeTodayDaykWh -SmappeeTodayNightkWh) * E7DayRate)) + (((SmappeeTodayNightkWh - SmappeeTodayMorningkWh) * E7NightRate)) + E7Standing

indigo.variable.updateValue("SmappeeTodayCost", str(SmappeeTodayCost))

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 4:22 pm
by petematheson
Math seems to check out!

Thanks all.

Made a minor adjustment to round the final number.
Using this I can now track my daily energy usage and display it on my wall mounted iPad :)

E7Standing = float(indigo.variables["E7Standing"].value)
E7DayRate = float(indigo.variables["E7DayRate"].value)
E7NightRate = float(indigo.variables["E7NightRate"].value)
SmappeeTodayDaykWh = float(indigo.variables["SmappeeTodayDaykWh"].value)
SmappeeTodayNightkWh = float(indigo.variables["SmappeeTodayNightkWh"].value)
SmappeeTodayMorningkWh = float(indigo.variables["SmappeeTodayMorningkWh"].value)

SmappeeTodayCost= ((SmappeeTodayDaykWh -SmappeeTodayNightkWh + SmappeeTodayMorningkWh) * E7DayRate) + ((SmappeeTodayNightkWh - SmappeeTodayMorningkWh) * E7NightRate) + E7Standing
SmappeeTodayCost= '%.2f' % round(SmappeeTodayCost, 2)

indigo.variable.updateValue("SmappeeTodayCost", str(SmappeeTodayCost))

Re: Python script help for basic maths

PostPosted: Sun Jul 29, 2018 11:39 pm
by kw123
Code: Select all
SmappeeTodayCost= '%.2f' % round(SmappeeTodayCost, 2)
indigo.variable.updateValue("SmappeeTodayCost", str(SmappeeTodayCost))


could be shortened:
Code: Select all
SmappeeTodayCostText= '%.2f' % SmappeeTodayCost
indigo.variable.updateValue("SmappeeTodayCost",SmappeeTodayCostText)


the %.2f makes a string with 2 digits after the "."

- the round is not needed
- the str() is not needed

just to help a little on the learning curve..

Karl

although the round() does a real rounding .005 --> 0.01 -- .00499 goes to 0.00
while %.2 just cuts the 3 digit off
if you want to very precise