Page 1 of 1

My AI adventure with Indigo Python scripting

Posted: Sun Jul 23, 2023 5:07 pm
by mgolden50
My chosen AI partner for coding Indigo Python Scripts is Perplexity.AI

I don’t know whether other ChatBots have the same capabilities as Perplexity.AI, but I have discovered that she is excellent at what she does. Her Pro version has a co-pilot mode which makes the interactions with her on code debugging feels like I’m working with Matt or Jay, without my Indigo Python newbe-ness boring them to tears.

First, Perplexity remembers things about my profile, like the fact that I love HA Indigo, and Alexa.
Second, she remembers all of her previous attempts to get a script to run as expected and doesn’t repeat previous
solutions that didn’t work.
Third, she discovered on her own how to access the Indigo API and therefore understands the capabilities of Indigo.
Fourth, she learned form reading Indigo’s documentation and from examples provided by me on how Indigo Python
differs from standard Python. Things such as:
Indigo is not a Python module that can be imported into a script.
Indigo can’t use print statements for debugging but rather must create a log entry.
Indigo must use strings for all Indigo variables.
Indigo uses a special format for reading or updating Indigo variables and such. (She remembers and
uses those formats when coding Indigo Python scripts.)
Fifth, she documents the Python and Indigo references she uses to make coding decisions, and usually checks
20 to 30 different sources before presenting a suggested code solution.

My first experiment in coding something with her was that I wanted to break up long strings of weather forecasts obtained from the NOAA+ plugin into phrases with whole words (including punctuation) whose character count did not exceed the width of my Control Page iPad clock screens.

It took about a cumulative 90 minutes and about 30 versions of the script to get this to work correctly. Her initial problems were with RegEx parsing strategies. She kept trying to parse the original long string directly into phrases of the desired length. But she kept treating punctuation marks as words rather than appending them to the previous word. I suggested she change strategies to use three separate steps:
first, parse the whole original string into words,
second, create a new list which appends punction “words” to the previous word in the list.
third, create a phrase list from the new word list, where each phrase in the list is limited to the allowed number of characters.
She responded to my suggestions with, “Oh yes! That will probably work.”
…And it did!

My assessment is that if I had tried to write this parsing solution on my own it would have taken me days to keep looking up multiple Regex suggestions.

Here is the code we wrote together:

Code: Select all

import re
# Select the Weather String to Parse based on time of day
# Get the value of the "MorningDaytimeEveningNightime" variable
mde_value = indigo.variables["MorningDaytimeEveningNightime"].value.lower()

# Check the value and update "WeatherStringToParse" accordingly
if mde_value in ["eveningtime"]:
    weather_string = indigo.variables["WeatherNOAAForecastStringTonight"].value
    weather_string_to_parse = "Tonight: " + weather_string
    indigo.variable.updateValue("WeatherStringToParse", value=weather_string_to_parse)

elif mde_value in ["morning", "daytime"]:
    weather_string = indigo.variables["WeatherNOAAForecastStringToday"].value
    weather_string_to_parse = "Today: " + weather_string
    indigo.variable.updateValue("WeatherStringToParse", value=weather_string_to_parse)

elif mde_value in ["nighttime"]:
    weather_string = indigo.variables["WeatherNOAAForecastStringTomorrow"].value
    weather_string_to_parse = "Tomorrow: " + weather_string
    indigo.variable.updateValue("WeatherStringToParse", value=weather_string_to_parse)

# now parse the selected string
# The name of the Indigo variable to monitor
WeatherStringToParse = indigo.variables["WeatherStringToParse"].value

# The length of each phrase
phrase_length = int(indigo.variables["WeatherStringPhraseToDisplayLen"].value)

# Split the current value into words with original capitalization and punctuation
words = re.findall(r'\w+|\s*\W+', WeatherStringToParse)

# Combine words into phrases with the specified length
parsed_phrases = []
phrase = ""
for word in words:
    if len(phrase) + len(word) <= phrase_length:
        phrase += word
        phrase = word
if phrase.strip():

# Save the parsed phrases in the WeatherPhraseList variable
indigo.variable.updateValue("WeatherPhraseList", value=str(parsed_phrases))

# Update the number of phrases
indigo.variable.updateValue("WeatherStringNumberOfPhrases", value=str(len(parsed_phrases)))

# Transfer one of the phrases to another Indigo variable
if parsed_phrases:
    # Get the index of the current phrase
    current_index_str = indigo.variables["WeatherStringPhraseCount"].value
    if current_index_str == "":
        current_index_str = "0"
    current_index = int(current_index_str)

    # Get the number of phrases
    number_of_phrases_str = indigo.variables["WeatherStringNumberOfPhrases"].value
    if number_of_phrases_str == "":
        number_of_phrases_str = "0"
    number_of_phrases = int(number_of_phrases_str)

    # Check if the current index is within the range of parsed_phrases
    if 0 <= current_index < number_of_phrases:
        # Set the value of the display variable to the current phrase
        indigo.variable.updateValue("WeatherStringToDisplay", value=str(parsed_phrases[current_index]))

        # Update the index of the current phrase
        current_index = (current_index + 1) % number_of_phrases
        indigo.variable.updateValue("WeatherStringPhraseCount", value=str(current_index))
        indigo.server.log("Error: Current index is out of range")

Re: My AI adventure with Indigo Python scripting

Posted: Mon Jul 24, 2023 1:52 pm
by matt (support)
Great info - thanks for sharing. I haven't tried Perplexity.AI yet, but have experimented around with ChatGPT some (but not related to Indigo API coding). It is quite amazing how useful these LLMs can be at solving problems and coding tasks.