Error creating rule to fire at specific time with additional conditions

I am trying to create a Rule that will fire at 10:00pm and only if a door is locked and closed.

i wrote the following but got an error when trying to perform the rules:create function in the CLI

i first wrote this as an IF\AND with all three conditions in the add cluster (datetime, reference, offset, etc) but this didn’t trigger at the specified time, so i rewrote it with the every\Specific and put the test for the closed and lock as in IF inside the actions, which is throwing an error

1st approach

{
	"if": {
		"and": [
			{
				"equals": {
					"left": {
						"device": {
							"devices": [
								"616651dc-a473-4129-b051-661fcfe2ed5f"
							],
							"component": "main",
							"capability": "lock",
							"attribute": "lock"
						}
					},
					"right": {
						"string": "locked"
					}
				}
			},
			{
				"equals": {
					"left": {
						"device": {
							"devices": [
								"a1df7c9b-2fcd-49e8-aa69-c9745c451e4c"
							],
							"component": "main",
							"capability": "contactSensor",
							"attribute": "contact"
						}
					},
					"right": {
						"string": "closed"
					}
				}
			},
			{
				"equals": {
					"left": {
						"datetime": {
							"reference": "Midnight",
							"offset": {
								"value": {
									"integer": -120
								},
								"unit": "Minute"
							}
						}
					},
					"right": {
						"datetime": {
							"reference": "Now"
						}
					}
				}
			}
		],
		"then": [
			{
				"command": {
					"devices": [
						"49cdd33c-aa3d-4610-8c6e-a9ba701aabc0"
					],
					"commands": [
						{
							"component": "main",
							"capability": "switch",
							"command": "off",
							"arguments": []
						}
					]
				}
			}
		]
	}
}

2nd Approach

{
	"name": "ShutOffAtNight test",
	"actions": [
		{
			"every": {
				"specific": {
					"reference": "Midnight",
					"offset": {
						"value": {
							"integer": -120
						},
						"unit": "Minute"
					}
				},
				"actions": [
					{
						"if": {
							"and": [
								{
									"equals": {
										"left": {
											"device": {
												"devices": [
													"616651dc-a473-4129-b051-661fcfe2ed5f"
												],
												"component": "main",
												"capability": "lock",
												"attribute": "lock"
											}
										},
										"right": {
											"string": "locked"
										}
									}
								},
								{
									"equals": {
										"left": {
											"device": {
												"devices": [
													"a1df7c9b-2fcd-49e8-aa69-c9745c451e4c"
												],
												"component": "main",
												"capability": "contactSensor",
												"attribute": "contact"
											}
										},
										"right": {
											"string": "closed"
										}
									}
								}
							],
							"then": [
								{
									"command": {
										"devices": [
											"49cdd33c-aa3d-4610-8c6e-a9ba701aabc0"
										],
										"commands": [
											{
												"component": "main",
												"capability": "switch",
												"command": "off",
												"arguments": []
											}
										]
									}
								}
							]
						}
					}
				]
			}
		}
	]
}

and the error from the 2nd appoach,

? Select a location. 1
    Error: Request failed with status code 500: {"requestId":"8868242A-FC72-46BF-89C0-3FB5627C7957","error":{"code":"UnexpectedError","message":"A non-recoverable error condition occurred.","details":[]}}

C:\Program Files (x86)\SmartThings>

what i am i doing wrong?

Thanks @jkp

Nothing that I can see. The error is being caused by the -120 offset. A positive offset seems to work. I think that has cropped up before. I don’t use Midnight myself as I don’t want to think about whether Midnight was twenty hours ago or in four hours time and what the implications might be. I always use Noon.

3 Likes

i will give the Noon entry a shot. How were you able to determine that the error was the -120? just curious to see how to better troubleshoot my future errors :stuck_out_tongue:

Thanks @orangebucket, i just tested with Noon with and offset of 95 to trigger at 1:35 and that worked… thank you

follow-up question:

how do i set a range from 10pm to 30 minutes before sunrise

i had this, but i am beginning to question as it has the Midnight and -120 offset…

{
	"if": {
		"between": {
			"value": {
				"time": {
					"reference": "Now"
				}
			},
			"start": {
				"time": {
					"reference": "Midnight",
					"offset": {
						"value": {
							"integer": -120
						},
						"unit": "Minute"
					}
				}
			},
			"end": {
				"time": {
					"reference": "Sunrise",
					"offset": {
						"value": {
							"integer": -30
						},
						"unit": "Minute"
					}
				}
			}
		}
	}
}

I thought it looked right and when I tried it I got graceful errors because it still had your device IDs in it. Only when I fixed those did I get the error 500. So I was pretty confident in the structure. The -120 was the first thing I changed as if anything was going to break it that seemed the most likely.

That’s why I use Noon and +600 offset. Midnight takes too much thinking about.

so, would this work then?

{
	"if": {
		"between": {
			"value": {
				"time": {
					"reference": "Now"
				}
			},
			"start": {
				"time": {
					"reference": "Noon",
					"offset": {
						"value": {
							"integer": 600
						},
						"unit": "Minute"
					}
				}
			},
			"end": {
				"time": {
					"reference": "Sunrise",
					"offset": {
						"value": {
							"integer": -30
						},
						"unit": "Minute"
					}
				}
			}
		}
	}
}

It looks OK to me.

What you may need to keep an eye on is whether the end time updates daily. I have an ‘every’ based Rule that is supposed to run at Sunrise+offset and the Sunrise stopped updating daily around about September … 2021. I’m still waiting for the fix.

Is there a log for Rules? See if they triggered, errors, etc. My rule, using the above Every\Specific did not run at 10pm…or did it run and not do what I expected?

In my rule, I have 3 actions. The 1st triggers when a door lock or contact sensor changes (lock/unlock, open/closed). It has if, else if to go through the various combinations and take different actions. This part works as expected. The 2nd and 3rd action are the time based ones, if noon + 10 hours (10pm) then turn off an indicator if all is good ( dark in bedroom at night) and the next one is turn indicator back on at 7am

I was reading on the ‘trigger’ attribute. Is it possible that my two ‘every/specific‘ actions are not executing because the automation server is not seeing the 2nd two actions as triggers?

How does one see the Rule results or if it was even triggered?

Hi @AZSteve

I’m Alejandro from Developer Support team, please, Can you share with us the rule that you are using? to understand more the problem.

Unfortunate, there’s no options for you to see the rule’s logs, if there’s an issue with the execution, the enginnering team can check mode details about it, also the endpoint to execute a Rule can help you check how are the conditions evaluated (true/false).