Rules API Syntax for Using Sunset/Sunrise with a Motion Detector

So, I’ve read all of the documentation I can find, but find it less than informative when setting up some simple (what I consider) Rules API scripts (or whatever you call them).

I would like an If action which triggers a command action when the time is between Sunset and Sunrise and a motion detector has motion active. I have simple If’s for turning on/off lights with motion, but nothing with a slightly more complex If with a range of time.

Additionally, what will the Rules Engine do, if it finds a syntax error or has something completely wrong? How does it notify you of errors or problems?

Thanks

@erickv, I guess that is a question for you or to @ady624. Are the Sunset/Sunrise features implemented already?

2 Likes

Hi @cabrioartie Welcome to the Community! Looking at what you want to do, If you go through the API documentation you can check the rules creation part description, you can create a between clause using the time data type, which accepts the following values:

“Now”, “Midnight”, “Sunrise”, “Noon”, “Sunset”

Something like the following might be useful for you, of course if you want more than one condition, then you’ll need the and clause.

{
"name": "If the time is between sunset and sunrise",
"actions": [
    {
        "if": {
            "between": {
                "value": {
                	"time": {
                		"timeZoneId": "America/Argentina/Buenos_Aires",
                		"reference": "Now"
                	}
                },
                "start": {
                    "time": {
                    	"timeZoneId": "America/Argentina/Buenos_Aires",
                		"reference": "Sunset"
                    }
                },
                "end": {
                	"time": {
                    	"timeZoneId": "America/Argentina/Buenos_Aires",
                		"reference": "Sunrise"
                    }
                }
            },
            "then": [
                {
                    "command": {
                        "devices": [
                            "2316d9b8-8bc3-4ecd-af1d-94096fe9eaa9"
                        ],
                        "commands": [
                            {
                                "component": "main",
                                "capability": "switch",
                                "command": "on",
                                "arguments": []
                            }
                        ]
                    }
                }
            ]
        }
    }
]

}

Please let me know if this is useful for your use case.
Regarding the errors, they are also described in the documentation, but as a personal experience I recommend you to use a JSON parser to check the format, which is where the most common errors are.
And you’ll get a message similar to this one:

{
"requestId": "606784EB-A493-4794-A8BF-46FA1E66ABBD",
"error": {
    "code": "ConstraintViolationError",
    "target": null,
    "message": "The request is malformed.",
    "details": [
        {
            "code": "BodyMalformedError",
            "target": "",
            "message": "Malformed body on line 4",
            "details": []
        }
    ]
}
4 Likes

Thank you, this is exactly what I needed.

But, this level of detail isn’t available in the documentation, for example, where do I find that Sunrise/Sunset is under Time or that “timeZoneId” is required or what the options are for “timeZoneId”.

Even looking for using an “and” in an “if” I can’t find. Syntax and order of operations just doesn’t seem to be there.

Maybe it’s there and I’m not seeing it, but I don’t see it or a way to find it.

Thank you again.

@cabrioartie glad to help!
In this documentation check that in the Request Body you can open each section that has more descriptions inside.


If you see all the bold ones can be expanded. And now that you mention that, the timeZoneId is not a required field :slight_smile:
Besides that if you check the right part of the page you can see the request samples, which states each combination that can be made:
Screen Shot 2020-01-28 at 17.13.11
And when you hover that part you get the Expand all to check at the whole description

2 Likes

Thank you again, now it makes sense.

I’m glad my comments help @cabrioartie! If you can, share the final solution with everyone when you finish your rule! :smiley: