Need help with Setting & Calculating Variables

Posted on
Tue Jan 01, 2019 7:18 am
sumocomputers offline
Posts: 267
Joined: Jun 23, 2008

Need help with Setting & Calculating Variables

My goal is pretty simple: I want to take my current electricity usage in kWh and multiply it by my current Time-of-Use rate from the utility, and display the current cost on a custom control page.

I am using the TED Home plugin to get my current usage in kWh, and have a bunch of Indigo schedules that set a variable called currentElectricityRate. This is because my utility went to a complicated TOU schedule that TED back-end cannot support correctly.

My thought was if I could have 2 variables (kWh usage from TED and TOU rate set from my Indigo schedules), I could do a simple multiplication, and the 3rd variable would be my current cost, which I could display in a custom control page. So something like this:

currentPowerNowTotal * currentElectricityRate = currentCost

I am having 2 challenges:

1. Getting the kWh usage (function is called PowerNow_Total in TED Plugin) into a variable that I can work with
2. Setting up an action (python script?) that multiplies the 2 variables to get my 3rd variable (currentCost)

Posted on
Tue Jan 01, 2019 8:23 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help with Setting & Calculating Variables

If I'm understanding, this should be pretty close:

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

kwh = float(indigo.devices[ID GOES HERE].states['PowerNow_Total'])
rate = float(indigo.variables[ID GOES HERE].value)

cost = kwh * rate

indigo.variable.updateValue(ID GOES HERE, str(cost))  # variables must be stored as strings

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 01, 2019 10:33 am
sumocomputers offline
Posts: 267
Joined: Jun 23, 2008

Re: Need help with Setting & Calculating Variables

This is awesome, it works great. One little thing: the variable value has a lot of extra decimal places.

Is there a way to just round up or truncate like these examples:
0.2007 becomes 0.20
0.2051 becomes 0.21

Posted on
Tue Jan 01, 2019 10:57 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help with Setting & Calculating Variables

Of course!

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

kwh = float(indigo.devices[ID GOES HERE].states['PowerNow_Total'])
rate = float(indigo.variables[ID GOES HERE].value)

cost = kwh * rate

# indigo.variable.updateValue(ID GOES HERE, str(cost))  # variables must be stored as strings
# indigo.variable.updateValue(ID GOES HERE, u"{0:.0f}".format(cost))  # variables must be stored as strings / 0 decimals
# indigo.variable.updateValue(ID GOES HERE, u"{0:.10f}".format(cost))  # variables must be stored as strings / 10 decimals

indigo.variable.updateValue(ID GOES HERE, u"{0:.1f}".format(cost))  # variables must be stored as strings / 1 decimal

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

[My Plugins] - [My Forums]

Posted on
Tue Jan 01, 2019 3:19 pm
sumocomputers offline
Posts: 267
Joined: Jun 23, 2008

Re: Need help with Setting & Calculating Variables

I will give this a go when I am back in front of a computer.

Thank you so much for the help!

Posted on
Wed Jan 02, 2019 7:30 pm
sumocomputers offline
Posts: 267
Joined: Jun 23, 2008

Re: Need help with Setting & Calculating Variables

Just wanted to say that your code worked great.

Thank You.

Posted on
Wed Jan 02, 2019 7:45 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help with Setting & Calculating Variables

Glad to hear that it worked for you.

Python's "format specifiers' are really useful. It's a wonder that Matt doesn't use them more often. :D

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

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests