Page 1 of 1

Temperature Conversion

PostPosted: Sat Apr 20, 2024 1:26 am
by CraigM
I would appreciate some help with getting this Python code correct. The following code seems to work, except I’m not getting consistent results with decimal places.
Code: Select all
# Conversion ºC to ºF
celsiusVar = indigo.variables[123456789]
tempVar = celsiusVar.getValue(float) 
indigo.variable.updateValue(987654321,str(tempVar * 9/5 + 32))

Sometimes the result might be xx.xx (2 decimal places), and sometimes it might be something like this (xx.xxxxxxxxx) both from the same code.

I would like to see examples of how this code can be written to control decimals as follows:

No decimal (xx)
1 decimal (xx.x)
2 decimals (xx.xx)

Also, do I need a “default=xx” after float? .getValue(float, default=xx)

Re: Temperature Conversion

PostPosted: Sat Apr 20, 2024 2:30 am
by CliveS
I copied your question verbatim into my chatGPT and got the following reply.

To control the number of decimal places in your temperature conversion output, you can format the result using Python's string formatting capabilities. Here's how you can modify your code to achieve the desired number of decimal places:

Code: Select all
# Conversion ºC to ºF
celsiusVar = indigo.variables[123456789]
tempVar = celsiusVar.getValue(float)

# Format the temperature with different decimal places
temp_no_decimals = f"{tempVar * 9/5 + 32:.0f}"  # No decimal places
temp_one_decimal = f"{tempVar * 9/5 + 32:.1f}"  # One decimal place
temp_two_decimals = f"{tempVar * 9/5 + 32:.2f}"  # Two decimal places

# Update variables with formatted temperature values
indigo.variable.updateValue(987654321, temp_no_decimals)
indigo.variable.updateValue(987654322, temp_one_decimal)
indigo.variable.updateValue(987654323, temp_two_decimals)


In this code:
- `:.0f`, `:.1f`, and `:.2f` control the number of decimal places in the formatted string. The `f` indicates that the value should be formatted as a float.
- `temp_no_decimals`, `temp_one_decimal`, and `temp_two_decimals` store the formatted temperature values with different decimal places.
- `indigo.variable.updateValue()` is called for each formatted temperature value to update the respective variables in Indigo.

Regarding your question about `getValue(float, default=xx)`, if you're not sure whether the value returned by `getValue()` will always be a float and you want to provide a default value in case it's not, you can indeed use the `default` parameter. However, it's optional, and if you don't provide it, the default value will be `None`. So, it depends on whether you expect `getValue()` to return non-float values and how you want to handle those cases. If you're confident that it will always return a float, you can omit the `default` parameter.


ChatGPT really is helpful for Python novices, but you need to check the output and if it does give code that errors then just paste the Indigo log error lines and it will reformat.

Re: Temperature Conversion

PostPosted: Sat Apr 20, 2024 3:48 pm
by CraigM
CliveS wrote:
I copied your question verbatim into my chatGPT and got the following reply.

ChatGPT really is helpful for Python novices, but you need to check the output and if it does give code that errors then just paste the Indigo log error lines and it will reformat.


PERFECT! Exactly what I was looking for and lots of choices to modify. Thanks CliveS (Its a little creepy that AI can do that)

Re: Temperature Conversion

PostPosted: Tue Apr 23, 2024 12:29 pm
by DaveL17
I meant to post this earlier and then forgot. The formatting statements above are called "Formatted String Literals" (or F-Strings for short) and they're really powerful. In addition to the math operations shown, you can also do things like logical operations. For example,

Code: Select all
print(f'{x} mph. {"" if x < 100 else "Holy Crap!"}')

x = 99
>>>  99 mph.

x = 100
>>> 100 mph. Holy Crap!
These logical operations are especially useful when you want to call attention to something that's abnormal. There are a lot of good examples in the Python docs.