Help with very simple script

Posted on
Thu May 25, 2017 1:58 pm
PhilP offline
Posts: 5
Joined: May 25, 2017

Help with very simple script

Hi,

I’m new to the world of scripting and could do with a pointer. Despite searching i’m still unsure of the correct syntax. Not even sure if i’m supposed to be using Python or Applescript?

I know this is incredibly simple script. Basically I want a script to check if a device is On, if so switch it off. I will then repeat this for multiple devices. It’s to be actioned from a trigger so I don’t have to create a separate trigger with condition and action for each device.

Thanks for your help

Posted on
Thu May 25, 2017 4:32 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Help with very simple script

Python is the way forward here.

But - can you explain your scenario?

If you're only testing if a device is on, then turning it off if it is, then don't bother testing it at all; just turn it off anyway - it'll turn off if it's on, or stay off if it isn't.

That can be done without scripting all together - in your trigger just add several turnoff commands via the UI.


Sent from my iPhone using Tapatalk

Posted on
Fri May 26, 2017 2:35 am
PhilP offline
Posts: 5
Joined: May 25, 2017

Re: Help with very simple script

I presumed it would be more efficient to only send commands if necessary, hence checking if the device was on before sending an off.
Building on this I am wanting to reduce the number of triggers by nesting multiple commands in scripts.
I also want to (for some devices) only send and on/off if it falls within a certain time range.

Posted on
Sat May 27, 2017 4:04 am
sgbirch offline
Posts: 99
Joined: Sep 11, 2013

Re: Help with very simple script

I have something similar to run my central heating. My system has to "notice" when a family member turns the heat up in one of the rooms. Usually, they feel cold if the heating hasn't been on for long and the walls are still cooler than the room which causes convection currents. So, if they turn it up the system waits at the higher temperature for an hour and then resets it to 21c.

Since there are about 15 rooms I chose to use a python script so every change to the system doesn't have to be repeated 15 times. Here is how it works:

1. The script is called every 5 seconds.
2. Devices are identified by the script using special names (e.g. roomname_radiator)
3. The script has no state information so variables are used (roomname_triggered)

The script was then put in

Posted on
Sat May 27, 2017 4:05 pm
PhilP offline
Posts: 5
Joined: May 25, 2017

Re: Help with very simple script

Anyone got some example code I can use as a starter?

Posted on
Sat May 27, 2017 7:48 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with very simple script

Here is a very simple script which checks to see if a light is on, and if it is, turn it off.

Code: Select all
dev = indigo.devices[1990777234]
if dev.onState:
   indigo.device.turnOff(1990777234)

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun May 28, 2017 11:43 am
PhilP offline
Posts: 5
Joined: May 25, 2017

Re: Help with very simple script

Thanks for the example. Looks straightforward.
How would I add a condition for a time window?

e.g. Check if device is on AND current time between 18:00 and 21:00 hours then switch device off

Posted on
Sun May 28, 2017 2:22 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with very simple script

PhilP wrote:
Thanks for the example. Looks straightforward.
How would I add a condition for a time window?

e.g. Check if device is on AND current time between 18:00 and 21:00 hours then switch device off

In Python, there's often several different ways to do the same thing--I think this is a nice example that's pretty straightforward.
Code: Select all
import datetime

dev = indigo.devices[1990777234]
now = datetime.datetime.now()
time_1 = now.replace(hour=18, minute=0, second=0, microsecond=0)
time_2 = now.replace(hour=21, minute=0, second=0, microsecond=0)

if dev.onState and (time_1 <= now <= time_2):
   indigo.device.turnOff(1990777234)

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon May 29, 2017 3:34 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Help with very simple script

I like that use of time replacement!

Don't know if it exists in Python but I've always used mktime() in php - I prefer your method though!


Sent from my iPhone using Tapatalk Pro

Posted on
Mon May 29, 2017 12:27 pm
PhilP offline
Posts: 5
Joined: May 25, 2017

Re: Help with very simple script

Thanks. Plenty to get me started.
Is there an online reference for the commands and syntax?

Posted on
Mon May 29, 2017 12:44 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with very simple script

Do a search for 'python datetime'. The official Python docs are a little tough to follow if you're not that familiar with Python, but there are many good examples on other sites. Look especially at http://stackoverflow.com where the common practice is to explain *why* a particular approach is used.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests

cron