LOGIN  |  REGISTER
Smart Living Made Brilliant!
CASTLEOS FORUM

HomeScripting

A forum for information about scripting with CastleOS. Get samples, suggestions, and other help here

Scene - Run a Script On vs. Off Messages in this topic - RSS

Tommy Long
Tommy Long
Posts: 209


3/5/2017
Tommy Long
Tommy Long
Posts: 209
In the Scene editor you can add devices of course as well as add a script to run.
When the scene is activated ON, the devices turn on, and when the scene is deactivated OFF the devices turn off.
There is no "run script X when activated/ON" nor "run script Y when deactivated/OFF", just simply "run this script".
Since the same script is called either way, is there a way for the script to know whether the scene is being activated/ON or deactivated/OFF?
0 link
Chris Cicchitelli
Chris Cicchitelli
Administrator
Posts: 3305


3/5/2017
Chris Cicchitelli
Chris Cicchitelli
Administrator
Posts: 3305
Within the script you can check the current status of the devices in the scene, by calling the GetDevice method. For instance, if your script turns a device on, then when the script is called it checks if the device is on, and then runs the "off" mode, and vice versa if it's off.
0 link
Tommy Long
Tommy Long
Posts: 209


5/4/2017
Tommy Long
Tommy Long
Posts: 209
Hi Chris,
I'm starting to play with the scripting APIs some.
Have a C# question (I suppose). I wanted to try to do something simple like turn on my kitchen lights.
However the following generates an error: object reference not set to an instance of an object.
Any suggestions?

--------------------
using System;
using System.Collections.Generic;
using CastleOSCoreService;


public class MyScript
{
public void Main(string[] args)
{
AutomationDevice MyDevice;

ScriptingAPI.LogEventToDatabase("This is a test database entry.");
ScriptingAPI.Speak("This is a demo announcement to all speakers.", "all");


MyDevice = ScriptingAPI.GetDevice("Kitchen Lights");
MyDevice.ToggleDevicePower(true);

}
}
0 link
Chris Cicchitelli
Chris Cicchitelli
Administrator
Posts: 3305


5/4/2017
Chris Cicchitelli
Chris Cicchitelli
Administrator
Posts: 3305
MyDevice is null because it's not finding a device. First, you'll want to add a null check:

if (myDevice != null) { }

Second, you need to search by ID, not name, to look up a device.
0 link
Tommy Long
Tommy Long
Posts: 209


5/8/2017
Tommy Long
Tommy Long
Posts: 209
Hi Chris,
I got the following to work, thanks

--------------------------------------
using System;
using System.Collections.Generic;
using CastleOSCoreService; // NOTE: this must be included, but you can add other using statements below



public class MyScript
{

public void Main(string[] args)
{
AutomationDevice MyDevice;
MyDevice = null;

// This method call will create a database entry in the main log. You can write anything you'd
// like here and it will add it to the DB queue for logging. Queue operates as first in first out.

ScriptingAPI.LogEventToDatabase("Creating test database entry.");


// This method call will speak the text of your choice to the speaker of your choice.
// You'll notice two strings, the first is the text to be spoken, and the second is the ID
// of the speaker to speak through. By default, enter "all" to speak to any connected or
// detected speaker. But you can also specify the speaker, as we'll show in the next section.

//ScriptingAPI.Speak("This is a demo announcement to all speakers.", "all");

foreach ( var device in ScriptingAPI.GetAllDevices() )
{

if (device.name.ToLower().Contains("kitchen lights"))
{
MyDevice = ScriptingAPI.GetDevice(device.uniqueId);
break;
}
}

if (MyDevice != null)
{
MyDevice.ToggleDevicePower(true);
ScriptingAPI.LogEventToDatabase("Turning on device"+MyDevice.name);
ScriptingAPI.Speak("Turning on the "+MyDevice.name,"all");
}




}
0 link
Chris Cicchitelli
Chris Cicchitelli
Administrator
Posts: 3305


5/8/2017
Chris Cicchitelli
Chris Cicchitelli
Administrator
Posts: 3305
Great! One note, though it works fine as is, when you are calling GetDevice to get the device, you already have it smile You just need to make it accessible outside the foreach loop by creating a variable outside the loop, and then assigning the device from within a loop to that variable. You already have that variable created as MyDevice, so instead of:

MyDevice = ScriptingAPI.GetDevice(device.uniqueId);

use:

MyDevice = device;
0 link