Rule to issue commands based on upper and lower temps

Hi everyone,
I have an Ecobee3 thermostat with a temperature/occupancy sensor in my kids’ room. In the same room is an AC unit that I can turn on or off with a 433 mhz outlet. Using the lessons here, I have successfully built a switch, sitemap, and rule that allows me to toggle the AC on and off manually. It looks like this:

rule KidsAC
when
	Item KidsAC received command
then
	if (receivedCommand == ON) {
		executeCommandLine("/var/www/rfoutlet/codesend 1234567") //turn on
		Thread::sleep(500)

	} else if (receivedCommand == OFF) {
		executeCommandLine("/var/www/rfoutlet/codesend 7654321") // turn off
		Thread::sleep(500)
	}
end

The broadcasting unit isn’t 100% reliable, so the real code actually has the transmissions run 6 times, .5 seconds apart. Also, I’ve obfuscated the real transmission codes so someone can’t haxx0rz my outlets.

I have connected to the Ecobee3 using the existing binding. The items for the remote sensor occupancy check and temperature are kidsroomOccupancy and kidsroomTemperature, respectively.

I would like to create a rule that checks the Ecobee3 sensor for temperature and occupancy, turns on the AC when it is above 77 degrees F and the room is occupied, and turns it off when it hits 74 degrees F or the room is unoccupied.

This is what I have so far:

rule LowerKidsRoomTemp
when
	Item kidsroomOccupancy = ON
then
	if (kidsroomTemperature > 77) {
		executeCommandLine("/var/www/rfoutlet/codesend 1234567") // turn on
		Thread::sleep(500)
	}
	else if (kidsroomTemperature < 73) {
		executeCommandLine("/var/www/rfoutlet/codesend 7654321") //turn off
		Thread::sleep(500)
	}

However, there are a couple of problems I’m having trouble with.

  1. This doesn’t account for sending the shutoff code if the occupancy changes to OFF and the temperature is still above 73.
  2. At present, this rule is (I think) constantly going to be applied, which means that if the room occupancy is ON, the radio will constantly be in use either sending the turn off or turn on code. I need a way to remember the last state of the AC unit (like a variable to store on or off) and not send the code if the state doesn’t need to be changed. I’m not sure how to store variables or do AND statements as part of an if statement’s conditions.
  3. I also have the switch, item KidsAC, to manually turn on the AC unit. It would be great if when the AC is turned on by this rule, it updates the state of the switch to be on or off. Rather than using the same executeCommandLine bit I have in thee above logic, should I instead be using KidsAC.sendCommand(OFF) or (ON)? I’m not sure how this function works.

I know this is kind of a big post. Any insight is much appreciated!

Take a look at the sample rules, I think you can adapt one of those to your needs.

You cannot trigger like that (take a look at the examples)

Item kidsroomOccupancy changed to ON

You have to call the state of the item:

kidsroomTemperature.state > 77

Use

when
    Item kidsroomOccupancy changed Or
    kidsroomTemperature changed
then

to trigger your rule. That way the rule gets triggered whenever the temperature or the occupancy changes and you can calculate the proper state of the heater.

Incorrect. Rule triggers are event based, not state based. The rule runs when certain events occur, not while Items are in a certain state. The trigger you are attempting to use is invalid

That being said, you can do a simple check like the following:

if(kidsroomTemperature.state > 77 && kidsroomHeater.state != ON) {
    kidsroomHeater.sendCommand(ON)
}
...

Note how I’m using an Item above instead of executeCommandLine. You should have a Switch Item to represent the state of your heater/airconditioner and when this Item is set to ON make the call to executeCommandLine.

See:

Again see the Proxy Design pattern. Essentially you want to represent the state of the air conditioner using a SwitchItem. Then have rules to interact with the other Items and translate commands to the Proxy Item into commands to the device (i.e. your calls to executeCommandLine) and translates changes to KidsAC into updates to the proxy item.

Hi, thanks for the great help!

I have updated my rule to look like this:

rule LowerKidsRoomTemp
when
    Item kidsroomOccupancy changed from OFF to ON Or
    kidsroomTemperature changed
then
	if(kidsroomTemperature.state > 77 && kidsroomOccupancy.state = ON && KidsAC.state = OFF) {
		KidsAC.sendCommand(ON)
	}
	else if(kidsroomTemperature.state < 73 && KidsAC.state != OFF) {
		KidsAC.sendCommand(OFF)
	}
	else if (kidsroomOccupancy.state = OFF && KidsAC.state != OFF) {
		KidsAC.sendCommand(OFF)
	}
end

This should work, right?

My only concern is that there are some cases where none of the else if’s fit correctly, such as when it is 75 degrees, kidsroomOccupancy is ON, and KidsAC is ON. Is this going to crash, or is it just going to do nothing and return?

Thanks again for all of your help!

It will just do nothing and return.

I highly recommend adding the rules into Designer for a syntax check. Everything looks ok but I’m on my phone.