Scripting Multisensor

Posted on
Wed Jul 05, 2017 9:08 am
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Scripting Multisensor

I am attempting to use a short script to round the temperature value coming from an MS6. Below is what I started with. The error is KeyError: 'key MultiSensor6Temperature not found in dict'. I can't find the state information for the MS6. Otherwise, should this script work?

import re # provides regular expressions

dev = indigo.devices[160965732]
string = dev.states["MultiSensor6Temperature"]
indigo.server.log(160965732) # original value
def fixTemp(match):
temp = float(temp) # round the temperature
temp = round(temp, 0)
temp = int(temp)
return str(temp)
indigo.server.log(160965732) # rounded value

John R Patrick
Author of
Home Attitude

Posted on
Wed Jul 05, 2017 10:39 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Aeon Labs MultiSensor 6 ZW100 (Indigo7)

[MODERATOR NOTE] moved to the Python Scripting forum.

Several points:

  • Wrap code snippets in the Code tag so that it'll preserve indentation (makes reading code much easier).
  • You're not using the regular expression module so you don't need to import it.
  • Built-in sensor devices have a property, sensorValue, which is the cleanest way to access it's value. So, in your script, dev.sensorValue.
  • Your log statement won't log what you think - rather, it'll just fail because 160965732 is a number. What you want to log in the first statement is str(dev.sensorValue) (anything passed to the log method must be a string).
  • You defined fixTemp as a function but you never actually call it, so nothing happens. In this case, where you're just doing the conversion once, you don't need to define it as a function.
  • The dev.sensorValue you're looking at is already a float so you don't need to convert it to a float. You can also combine the round and int conversion into one statement.
  • It's unclear from your script what exactly it's going to do with the rounded value so you'll need to fill in that blank.

So, your updated script would be something like this:

Code: Select all
dev = indigo.devices[160965732]
indigo.server.log(str(dev.sensorValue))  # original value
temp = int(round(dev.sensorValue, 0))
indigo.server.log(str(temp))  # rounded value
# the temp variable now contains the rounded temperature, not sure what
# you were planning on doing with it next

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Jul 05, 2017 12:33 pm
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Scripting Multisensor

Thanks very much for the help. I use the variables in an AWS Polly Good Morning Sonos announcement. In addition to the MS6 values, it reports the date, day of week, time, and local forecast from DaveL17's WU plugin. The Good Morning action group has 17 things in it: unlocking doors, tstat settings, select random music from list, start music, etc. I plan to build a Python script to include all this. I am new to Python. Back in grad school in late 1960s, I was pretty good with Fortran, but that is not very useful for most today. Python is totally different, but I like that I can document things with #s. In the action group window some things are clear, some are not. With a script, I will see things clearly. I will save it as a file and execute that way once it works. Slow learning, but I am making progress. Couple of questions:

1. Do you see Python as a long term survivor?
2. I presume it is better than Applescript? Is there a reliable way to convert an Applescript to a Python script?
3. I got PyCharm professional, but found it daunting, to put it mildly. Overkill for me, although the breadth and depth of it is quite impressive.
4. I don't get what you mean by, "Wrap code snippets in the Code tag so that it'll preserve indentation (makes reading code much easier)."

John R Patrick
Author of
Home Attitude

Posted on
Wed Jul 05, 2017 2:55 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Scripting Multisensor

wideglidejrp wrote:
1. Do you see Python as a long term survivor?


Survivor of what exactly? If you mean Python as a viable language, it's number 4 on the Tiobe Index of language popularity. It's going to be around for a very long time. And it is the future of Indigo scripting/development.

wideglidejrp wrote:
2. I presume it is better than Applescript? Is there a reliable way to convert an Applescript to a Python script?


"better", when referring to a programming language is quite subjective. However, we think it's better in almost every respect. It's widely used (as pointed out above) so there are literally tons of libraries, tutorials, how-to sites, open-source projects using it. Converting from language to language can (arguably) never really be automated well. Converting to anything from AppleScript is practically impossible in an automated way given the loosy-goosy nature of AppleScript. Fortunately, once you've learned Python and Indigo's Object Model, manually converting scripts is usually quite fast. Lots of people around here can help.

wideglidejrp wrote:
3. I got PyCharm professional, but found it daunting, to put it mildly. Overkill for me, although the breadth and depth of it is quite impressive.


It's the best Python IDE, bar none (and one of the best IDE's I've ever used on any platform for any language). However, for just writing Indigo scripts, it's way overkill and in some respects it'll even get in the way. I only recommend PyCharm for plugin developers.

wideglidejrp wrote:
4. I don't get what you mean by, "Wrap code snippets in the Code tag so that it'll preserve indentation (makes reading code much easier)."


When editing a message on the forums, look at the buttons above the text area under "Selection style". You'll see a button called Code. Click that button and it'll insert the appropriate code tags in your message. Anything between those tags will be rendered in a monospace font so that you'll see the appropriate formatting. Python uses indentation to signify blocks of code (vs the typical end block tags like }) so the format of the code is important.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Jul 05, 2017 3:30 pm
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Scripting Multisensor

Code button. Gotcha. Survivor may have been a strong word. Seems Apple, Google, edu, etc. keep coming up with new languages. Was not aware of Tiobe list. Good to see Python there and gaining on the top three. None of the four I learned at IBM are there: Cobol, Fortran, APL, and RPG. Compuware claims there are still billions of lines of Cobol out there in big enterprises. One last question: is it possible to export an action group as Python script?

John R Patrick
Author of
Home Attitude

Posted on
Wed Jul 05, 2017 3:46 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Scripting Multisensor

Nope.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest