Page 1 of 1

Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 10:12 am
by ckeyes888
I'm attempting to switch from Fantastic Weather to the NOAA plugin but am having issues with all the possible weather conditions
and corresponding icons I use.

Hoping I can revise my script to find certain words in a variable and set my icon variable accordingly.
e,g, precip, mist to simply mist.

Here's the script I've been trying.

Code: Select all
theText = indigo.variables[175134881].value
if "Clear" in theText:
    indigo.variable.updateValue(175134881, value="Sunny")
elif "Cloudy" in theText and "Mostly" in theText:
    indigo.variable.updateValue(175134881, value="Cloudy_Mostly")


Thanks,

Carl

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 12:13 pm
by FlyingDiver
And what does or doesn't work when you use that script?

One thing I would do is change the first line to:

Code: Select all
theText = indigo.variables[175134881].value.lower()


Then you can test with lower case strings and it'll work no matter that case they are in the variable.

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 12:45 pm
by ckeyes888
Thanks for the tip on upper and lower case.

The problem is NOAA comes up with dozens of descriptions for conditions.
For example I’d just like the script to convert something like ”partially clear” to just “clear.”.
Or, “likely light snow”, to “snow”.

Thanks,

Carl

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 1:12 pm
by FlyingDiver
That script should work then, if you're just looking for specific keywords.

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 1:17 pm
by ckeyes888
I assume it’s not working because of the case issue which I’ll fix later and see if that’s it.

Thanks,

Carl

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 2:39 pm
by ckeyes888
Still no go. If it returns "light rain" it doesn't change to "Rain".

Code: Select all
elif "Rain" in theText and "Light" in theText:
    indigo.variable.updateValue(175134881, value="Rain")


Thanks,

Carl

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 2:42 pm
by FlyingDiver
You really need to show your current code and the input text if you expect help.

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 4:02 pm
by ckeyes888
Sorry if it was confusing.
For now just trying to get this to work:
Code: Select all
theText = indigo.variables[175134881].value.lower()
if "Overcast" in theText:
    indigo.variable.updateValue(175134881, value="Overcast")
elif "Rain" in theText:
    indigo.variable.updateValue(175134881, value="Rain")


So if the variable became "mostly overcast" it would set it to "overcast".

thanks,

Carl

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 4:34 pm
by FlyingDiver
Your input text is now all lower case, so you need to change the if statements to match.

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 4:40 pm
by ckeyes888
Is there a way to make it ignore upper/lowercase?

The states are all over the place. Just got one “Unknown, misty, rain”.

Thanks,

Carl

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 4:49 pm
by FlyingDiver
What I said to do does that. Just use "rain" not "Rain" in your script.

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 10:17 pm
by ckeyes888
Tried a number of variations, upper and lower case, but none work.

Code: Select all
theText = indigo.variables[175134881].value.lower()
if "fog" in theText:
    indigo.variable.updateValue(175134881, value="Fog")


If the variable contains, "patchy fog" it doesn't convert it to "Fog".

Thanks,

Carl

Re: Python to Find/Replace Text

PostPosted: Sun Jan 15, 2023 10:32 pm
by FlyingDiver
You're doing something wrong. That script worked exactly right for me. The only thing I changed was the variable ID.

Re: Python to Find/Replace Text

PostPosted: Mon Jan 16, 2023 8:50 am
by ckeyes888
Doh, was using a condition that no longer worked.

Works great, thanks for the help.

Carl