Python script help for basic maths

Posted on
Sun Jul 29, 2018 12:02 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Python script help for basic maths

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

Posted on
Sun Jul 29, 2018 1:23 pm
FlyingDiver online
Posts: 2366
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: Python script help for basic maths

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.

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

Posted on
Sun Jul 29, 2018 2:33 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Re: Python script help for basic maths

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'

Posted on
Sun Jul 29, 2018 2:44 pm
FlyingDiver online
Posts: 2366
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: Python script help for basic maths

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.

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

Posted on
Sun Jul 29, 2018 2:50 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Re: Python script help for basic maths

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.

Posted on
Sun Jul 29, 2018 3:24 pm
FlyingDiver online
Posts: 2366
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: Python script help for basic maths

I just copied your formula verbatim. I don't know what the variables mean so I couldn't check the math.

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

Posted on
Sun Jul 29, 2018 3:26 pm
CliveS offline
Posts: 310
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: Python script help for basic maths

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

CliveS

Indigo 7.1.1 : Mac mini Server (Mid 2011) : MacOS Sierra 10.12.6

Posted on
Sun Jul 29, 2018 3:35 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Re: Python script help for basic maths

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.

Posted on
Sun Jul 29, 2018 3:38 pm
FlyingDiver online
Posts: 2366
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: Python script help for basic maths

change all the those int() casts to float().

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

Posted on
Sun Jul 29, 2018 3:52 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Re: Python script help for basic maths

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"

Posted on
Sun Jul 29, 2018 4:00 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Re: Python script help for basic maths

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

Posted on
Sun Jul 29, 2018 4:05 pm
FlyingDiver online
Posts: 2366
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: Python script help for basic maths

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.

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

Posted on
Sun Jul 29, 2018 4:09 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Re: Python script help for basic maths

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

Posted on
Sun Jul 29, 2018 4:22 pm
petematheson offline
Posts: 680
Joined: Sep 14, 2014
Location: Southampton, UK

Re: Python script help for basic maths

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

Posted on
Sun Jul 29, 2018 11:39 pm
kw123 offline
User avatar
Posts: 5599
Joined: May 12, 2013
Location: Dallas, TX

Re: Python script help for basic maths

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest