So i've been wondering how to take a string (from Weather underground - Detailed forecast) and split it onto 2 lines for use in a control page.
i've had a friend work on something this morning that works really well, so thought i'd share it with you all.
I've created 2 variables that the control page reads the data from.
And also set up a trigger that performs this Python script whenever change is detected on the detailed forecast state.
The devices ID and Variable IDs will need to be changed to your specific IDs.
Also the character limit would also need to change depending on text size and label size.
Hope it's of some use to you guys!
Andrew
- Code: Select all
weatherstring = indigo.devices[266180261].states["foreText1"]
CharacterLimit = 73
def splitlines(weatherstring):
lines= []
remainingtext = str(weatherstring)
def sliceofffirstline(remainingtext):
chunk = str(remainingtext[:CharacterLimit])
lastspaceindex = chunk.rfind(' ')
line = str(remainingtext[:lastspaceindex])
remainingtext = remainingtext[lastspaceindex:]
return remainingtext, line
while True :
if len(remainingtext) > CharacterLimit:
remainingtext, slice = sliceofffirstline(remainingtext)
lines.append(str(slice).strip())
if len(remainingtext) <= CharacterLimit:
remainingtext.strip()
lines.append(str(remainingtext).
strip())
return lines
break
lines = splitlines(weatherstring)
if len(lines)==1:
indigo.variable.updateValue(1812552637,lines[0])
indigo.variable.updateValue(753872597,"")
else:
indigo.variable.updateValue(1812552637,lines[0])
indigo.variable.updateValue(753872597,lines[1])