Trigger a rule when a variable updates?

Is it possible to trigger a rule when a variable is updated? Can someone tell me how?

I have a rule that updates a variable based on time of day. I have a 2nd rule that i’d like to trigger only after the variable is updated. Is it possible? I can’t get it working and I figured i’d ask before changing directions with this.

Sample of the rules:
Setting the variable.

var Number FamilyRoomLevel=0  
  rule "FamilyRoom Ceiling Light Level"
    when
    	System started or
        Time cron "0 1 */1 * * ?"
    then
    	if (LightingAutomation == 1){
        		if (now.getHourOfDay() >= 00 && now.getHourOfDay() < 05) {
         		FamilyRoomLevel=1
         		logInfo("Lighting Level", "FamilyRoom Updated 01")
         		}	
    		else
         		if (now.getHourOfDay() >= 05 && now.getHourOfDay() < 08) {
         		FamilyRoomLevel=45
         		logInfo("Lighting Level", "FamilyRoom Updated 45")}		
    Etc, Etc...

Trying to trigger the rule below after the rule above.

rule "Lighting adjuster"
when
	Item FamilyRoomLevel received update or
	Item Light_FF_Family_Ceiling_Control changed from OFF to ON
	//cron "* */10 * * * ?"
then
	if(FamilyRoomPresenceSwitch.state == ON){
		if(LightingAutomation!=8 || LightingAutomation!=9){
			if(Light_FF_Family_Ceiling.state != FamilyRoomLevel){
				sendCommand(Light_FF_Family_Ceiling_Control, "ON")
				sendCommand(Light_FF_Family_Ceiling, FamilyRoomLevel)
				logInfo("Lighting Level", "FamilyRoom Lighting Auto Update")
	}}}
end

Use an item for thw variable and sendCommand to set your item

HA! That stinks, that’s the direction I was about to go in but didn’t want to. I only posted a small piece of the rule file. That variable is used like 50 times and with the syntax for variable and item being so different a find and replace is pretty much out of the question. I have 3 other rule files that are structured the same.

O well, live and learn. Guess I don’t have to many choices.

Thanks for the confirmation!!!

Indeed. It is almost always better to store state in Items rather than vars for this very reason. You also get the benefit that your values will get saved and restored if you set up persistence.

I think i’m missing something. So I can set the number item using sendcommand but I can’t use the value of the item to update other items.

This is what I did.
I created a NUmber item named: Light_FF_Family_Ceiling_level

Updated the rules as followed:

Rule 1:
rule "FamilyRoom Ceiling Light Level"
when
	System started or
    Time cron "0 1 */1 * * ?"
then
	if (LightingAutomation == 1){
    		if (now.getHourOfDay() >= 00 && now.getHourOfDay() < 05) {
     		sendCommand(Light_FF_Family_Ceiling_level, "1")
     		logInfo("Lighting Level", "FamilyRoom Updated 01")
     		}	

Rule 2:
rule "Lighting adjuster"
when
	Item Light_FF_Family_Ceiling_level received update or
	Item Light_FF_Family_Ceiling_Control changed from OFF to ON
	//cron "* */10 * * * ?"
then
	if(FamilyRoomPresenceSwitch.state == ON){
		if(LightingAutomation!=8 || LightingAutomation!=9){
			if(Light_FF_Family_Ceiling.state != Light_FF_Family_Ceiling_level.state){
				sendCommand(Light_FF_Family_Ceiling_Control, "ON")
				sendCommand(Light_FF_Family_Ceiling, "Light_FF_Family_Ceiling_level.state")
				logInfo("Lighting Level", "FamilyRoom Lighting Auto Update")
	}}}
end

I know the rules are both triggering as i’m getting an error message: given command is NULL, couldn’t send command to ‘Light_FF_Family_Ceiling’

How do I use a number item to update a dimmer item?

First, I highly recommend using the sendCommand and postUpdate methods on the Item objects rather than calling the action methods as you are doing here. THe Item methods are much smarter at dealing with multiple data types wheras the actions have to be more generic and therefore don’t do as well. For example:

Light_FF_Family_Ceiling.sendCommand(Light_FF_Family_Ceiling_level.state) 

Second, when using the sendCommand action method I think you realized that you must send the state as a String. But just putting quotes around the Item doesn’t do what you think. What you really want is:

sendCommand(Light_FF_Family_Ceiling, Light_FF_Family_Ceiling_level.state.toString)

When you put the quotes around it, instead of sending a String version of the number it holds you are literally trying to set the dimmer to the String “Light_FF_Family_Ceiling_level.state” which of course cannot be converted into something a Dimmer Item understands.

Thanks Rich! Every time I read one of your responses I feel smarter, like I’ve just learned something amazing. Thank you.

I got it working using:

sendCommand(Light_FF_Family_Ceiling, Light_FF_Family_Ceiling_level.state.toString)

Awesome!! This is what I was trying to do when i wrote the rule.

When using:

Light_FF_Family_Ceiling.sendCommand(Light_FF_Family_Ceiling_level.state)

I get the following error when the rule executes:

NODE 17: No converter found for item = Light_FF_Family_Ceiling, endpoint = 0, ignoring command.

Any idea what this means?

Ist Light_FF_Family_Ceiling a zwave device? The NODE 17 implies that but other bindings use “nodes” too. The error looks like it is coming from the binding which is outside my areas of expertise but the error implies that the binding cannot convert the value stored in Light_FF_Family_Ceiling_level.state to something the device understands, which is weird as it seems to work using toString with the sendCommand action.

Try using toString on the second approach too and see if that makes a difference. Also, it could be a good idea to log out the value to see if there is anything funny.

< Insert Dancing here >… I needed to add the toString to the command. So it’s written as

Light_FF_Family_Ceiling.sendCommand(Light_FF_Family_Ceiling_level.state.toString)

Thank you for the help! Not only is this working now but it’s going to pave the way for future rules!!

This is exactly what I have been looking for nearly 3 weeks now. To link a value of one item and update the same value to another.
Thank you so much. This saves a lot of my time and code, being new to Openhab.