Python to Find/Replace Text

Posted on
Sun Jan 15, 2023 10:12 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Python to Find/Replace Text

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

Posted on
Sun Jan 15, 2023 12:13 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python to Find/Replace Text

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.

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

Posted on
Sun Jan 15, 2023 12:45 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python to Find/Replace Text

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

Posted on
Sun Jan 15, 2023 1:12 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python to Find/Replace Text

That script should work then, if you're just looking for specific keywords.

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

Posted on
Sun Jan 15, 2023 1:17 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python to Find/Replace Text

I assume it’s not working because of the case issue which I’ll fix later and see if that’s it.

Thanks,

Carl

Posted on
Sun Jan 15, 2023 2:39 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python to Find/Replace Text

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

Posted on
Sun Jan 15, 2023 2:42 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python to Find/Replace Text

You really need to show your current code and the input text if you expect help.

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

Posted on
Sun Jan 15, 2023 4:02 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python to Find/Replace Text

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

Posted on
Sun Jan 15, 2023 4:34 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python to Find/Replace Text

Your input text is now all lower case, so you need to change the if statements to match.

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

Posted on
Sun Jan 15, 2023 4:40 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python to Find/Replace Text

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

Posted on
Sun Jan 15, 2023 4:49 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python to Find/Replace Text

What I said to do does that. Just use "rain" not "Rain" in your script.

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

Posted on
Sun Jan 15, 2023 10:17 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python to Find/Replace Text

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

Posted on
Sun Jan 15, 2023 10:32 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python to Find/Replace Text

You're doing something wrong. That script worked exactly right for me. The only thing I changed was the variable ID.

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

Posted on
Mon Jan 16, 2023 8:50 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python to Find/Replace Text

Doh, was using a condition that no longer worked.

Works great, thanks for the help.

Carl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 7 guests