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:

Hi, is that possible I can do between sunset to a specific time for example 11:00PM? Thank you

In Rules, times are a combination of a reference and an offset. So if you change the reference to Noon you can define any time of day with a plus or minus offset. Here is 5:30am.

"reference": "Noon",
"offset": {
    "value": {
        "integer": -390
     },
     "unit": "Minute"
}

So for 11:00pm the offset is 660. You can use Midnight as the reference but I always feel that has an inherent ambiguity as to whether it is the beginning or the end of the day. It may not be ambiguous in the context of a Rule but I prefer to avoid the issue.

1 Like

Hi thank you and that’s what I did. I was just interested to see if it’s possible to do that way I mentioned. Anyway, thanks for the reply.

I think I left ‘yes’ out of my reply.

1 Like