On light on, select brightness based on time of day

Hi guys

I just installed node-red and I have exactly one production automation working in it right now. (Well, two, I have a test-light-on whatever-ya-callits groups of bubbles).

I want to essentially do a case statement - on light on event, check the time and set brightness as appropriate (~8pm, 10pm, midnight - 6am).

If anyone has the urge would you mind giving me an idea of how I would do this?
(so much easier to get into this stuff when you have a bit working)

thanks

You can get the hour of the day nodered way by using the moment node if you pass the timestamp as input. but you can also use function node and use use new Date()

so in code woould be

var d = new Date()
hour = d.toLocaleTimeString(undefined, {  hour: 'numeric' })

From there you can work your case statement

1 Like

Instead of using the time of day, you could as well use the sun position :slight_smile:

1 Like

I have zero coding skills which is why I love Node-Red.

There is probably a better way but this is the way I would do it.

1 Like

Yes, with the time_node this is one of the simpliest solutions.

Within the sun_position module you can specify the different light_levels and assign the out-node accordingly

This would make your flow even simpler and would adjust for summer/winter season as it considers the actual sun_position.

1 Like

Why is it that the sun-position node returns an error when it’s in one of the azimuth positions, and doesn’t return an error when it’s not in one of the configured azimuth positions?

It maybe better to ask this on the nodes git page since it is very specific to the nodes behavior. You could use a catch node to catch and log the error in a way that you can trigger from it.

I miss read that, basically it doesn’t work at all? Configured result in error and un configured does nothing?

Edit. I used the example flow from the nodes documentation and I get that error if the azimuth is within a configured range. It appears to be a bug.

It seems this issue has been around for a while. You could always use a change node after to sort the azimuth.

When in the configured azimuth position, the node appears to be adding fields to the input message.

Adding a property (field) to something that is not an object will generate this error - you can’t add {“sunPos”: true} to a number.

You can, however, add properties to an object. Just set msg.payload to an object (even {} works) and then the node functions.

Not a bug, just a ‘feature’.

1 Like

great, this does the trick! this wasn’t clear to me in the documentation :slight_smile: