Cumulative variables

Posted on
Wed Apr 21, 2021 12:07 pm
raoul offline
Posts: 25
Joined: Jun 11, 2019

Cumulative variables

I am looking to get a cumulative value for proposed rainfall in the coming 8 or 12 hour window. I have api.openweathermap working well with Ghost XML, so for instance I am alerted if the forecast temperature is due to drop below a certain point.
However adding values together has me stumped. I did look at converting values to variable but this didn’t help me. I need to take say 12 value readings and add them altogether to create one new value.

I wonder if anyone could make a suggestion.

Thank you I’m advance.

Posted on
Wed Apr 21, 2021 3:11 pm
DaveL17 offline
User avatar
Posts: 5897
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Cumulative variables

If it were me, I'd write a Python script to add the values together and save the sum to the target variable. The script would be very simple to do and I'd be happy to help you with it.

Fair warning: because the GhostXML plugin accepts whatever the source API provides, there is always a chance that the device state names could change (probably very unlikely that they'd change) so I wouldn't recommend doing this with anything mission critical. To account for such things, we could put a safety valve in the script where any change would cause the sum to be a signal value (like -99). Then, your trigger on the sum could not fire if the value is -99.

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

[My Plugins] - [My Forums]

Posted on
Thu Apr 22, 2021 4:17 am
raoul offline
Posts: 25
Joined: Jun 11, 2019

Re: Cumulative variables

Thanks for the reply and offer. If you could set me on the path of a suitable Python script I'd be grateful.

I hear you re the warning, to be honest this is only for a 'heads up' and with a view to creating a couple of irrigation triggers, which I'll also use a rain gauge to 'keep it honest'.

Thanks.

Posted on
Thu Apr 22, 2021 4:59 am
DaveL17 offline
User avatar
Posts: 5897
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Cumulative variables

Sure thing. This script should work regardless of whether the GhostXML device is a Real Type or String Type. You'll need to change the id numbers of the source device and target variable to match yours. The script will add together the values of the device states from day 0 to day 4 (Python 'range' does not include the last value in the range--or 5 here) which gives you five days' worth (0, 1, 2, 3, 4). You can adjust the number of days included by changing this range.

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

dev = indigo.devices[1970132863]
pop = 0.0

try:
   for n in range(0,5):
      pop += float(dev.states['list_{0}_pop'.format(n)])

   indigo.variable.updateValue(1928699259, str(pop))
except KeyError:
   indigo.variable.updateValue(1928699259, str(-99))


EDIT: forgot the safety value. 8)

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

[My Plugins] - [My Forums]

Posted on
Thu Apr 22, 2021 11:48 am
raoul offline
Posts: 25
Joined: Jun 11, 2019

Re: Cumulative variables

Thanks man, looks pretty straight forward, I'll see how I get on.

Thanks again, take care.

Posted on
Thu Apr 22, 2021 1:29 pm
DaveL17 offline
User avatar
Posts: 5897
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Cumulative variables

raoul wrote:
Thanks man, looks pretty straight forward, I'll see how I get on.

Thanks again, take care.

My pleasure. Let me know if you have any questions.

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

[My Plugins] - [My Forums]

Posted on
Fri Apr 23, 2021 8:49 am
raoul offline
Posts: 25
Joined: Jun 11, 2019

Re: Cumulative variables

All good, no rain currently, but the safety value works well.
I just need to make one amendment (other than the relevant device ID's), in order to use hourly forecast

pop += float(dev.states['hourly_{0}_pop'.format(n)])

Thanks again and best of luck

Posted on
Fri Apr 23, 2021 10:32 am
DaveL17 offline
User avatar
Posts: 5897
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Cumulative variables

Excellent. If you find you need to tweak it somehow and need some assistance, don't hesitate to ask.

Cheers.

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 1 guest