Cumulative variables

Posted on
Wed Apr 21, 2021 12:07 pm
raoul offline
Posts: 41
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: 6742
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: 41
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: 6742
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: 41
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: 6742
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: 41
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: 6742
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]

Posted on
Thu Jun 17, 2021 8:12 am
raoul offline
Posts: 41
Joined: Jun 11, 2019

Re: Cumulative variables

I am revisiting this script and making a few changes. Specifically I'm going from 'pop' (probability of precipitation) to 'rain_1h' (rainfall forecast in that hour).

The script works fine when there is an entry, i.e. there is a rainfall forecast in those hours, however when there is no rainfall forecast the api rather than have a zero value, doesn't list this parameter. As such my script in it's current form gives an error for any hour range that has no rainfall forecast.

Is there a way to modify this script so that even if there is no entry (for 'hourly_{0}_rain_1hr') then the script just moves onto the next hour within the predefined range?

Thanks very much in advance.

Posted on
Thu Jun 17, 2021 9:04 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Cumulative variables

Change this line:

Code: Select all
      pop += float(dev.states['list_{0}_pop'.format(n)])


To this:

Code: Select all
      pop += float(dev.states.get('list_{0}_pop'.format(n), 0) )


That changes the dict lookup in the states to use the .get() method instead of the direct key. Using .get() allows for a default value to be returned (in this case, it's a zero) when the key doesn't exist in the dict.

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

Posted on
Fri Jun 18, 2021 3:15 am
raoul offline
Posts: 41
Joined: Jun 11, 2019

Re: Cumulative variables

Thank you, that works really well.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest