Page 1 of 1

Cumulative variables

PostPosted: Wed Apr 21, 2021 12:07 pm
by raoul
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.

Re: Cumulative variables

PostPosted: Wed Apr 21, 2021 3:11 pm
by DaveL17
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.

Re: Cumulative variables

PostPosted: Thu Apr 22, 2021 4:17 am
by raoul
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.

Re: Cumulative variables

PostPosted: Thu Apr 22, 2021 4:59 am
by DaveL17
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)

Re: Cumulative variables

PostPosted: Thu Apr 22, 2021 11:48 am
by raoul
Thanks man, looks pretty straight forward, I'll see how I get on.

Thanks again, take care.

Re: Cumulative variables

PostPosted: Thu Apr 22, 2021 1:29 pm
by DaveL17
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.

Re: Cumulative variables

PostPosted: Fri Apr 23, 2021 8:49 am
by raoul
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

Re: Cumulative variables

PostPosted: Fri Apr 23, 2021 10:32 am
by DaveL17
Excellent. If you find you need to tweak it somehow and need some assistance, don't hesitate to ask.

Cheers.

Re: Cumulative variables

PostPosted: Thu Jun 17, 2021 8:12 am
by raoul
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.

Re: Cumulative variables

PostPosted: Thu Jun 17, 2021 9:04 am
by FlyingDiver
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.

Re: Cumulative variables

PostPosted: Fri Jun 18, 2021 3:15 am
by raoul
Thank you, that works really well.