Node-RED Sunset/Sunrise Light Automations

OK, here an example of how a an existing series of automations can be easier to understand at a glance (and I think the complex automations can be easier to maintain) in Node-RED.

One of my more advanced automations has logic like this:

  • Trigger: 1 hour after Dusk
  • Condition: Only execute if this feature enabled?
  • Action:
    • Get the list of currently turned on lights (omitting certain ones).
    • Dim those lights to 80%.

Which seems simple when it is pseudo-coded out like that. But how long does it take me find the code responsible for this automation? Then once I find it it takes a few minutes to review the long complex code to remember how its working.

I am not ready to stop using Home Assistant’s YAML files to create automations… but I am going to give this path a try for a while. Either way I’m having fun learning something new! :slight_smile:

Node-RED

Home Assistant YAML

6 Likes

Hi @BrianHanifin
Thanks for sharing
This is what I was looking for to move this automation from yaml to NodeRed.
What node types are the first ones?

Thanks

Its called Big Timer, but I now discovered I should have started my “sequence” from the bottom output square instead of the top one. This one is great because in addition to time you can set a schedule based on time relative to sunrise or sunset (or a list of other parameters).

BTW, before I started I downloaded all of the “pallets” from this guy’s list. He recommends one named “Light Scheduler”. Which I haven’t played with much yet.

Oh, and if you are using Docker, make sure you have it configured to your time zone, or your lights will go off at the wrong time and you will be very confused for days. :wink:

1 Like

I hate to say this… but I have been very disappointed with the reliability of Node-Red for running automations. I suspect if I stuck with it long enough I could figure it out, but we have been going on a week with random lights turning on or off, or even worse the bedroom lights refused to stay turned off last night when my wife was trying to go to bed. I am going to have to switch back to using YAML for my automations for the time being.

Maybe I was too ambitious to start? I tried doing things that seemed like they should be reliable, but maybe they aren’t. For example I setup this Home Assistant “Template Node” to attempt to see

  1. Is the automation?
  2. Which light should be turned on?
  3. Increment the brightness by about 1%.

In tests it worked great, but in practice it never runs. But I’m wondering if a complex node like this is just too much for run reliably?

{%- set wakeup_enabled = states("input_boolean.wakeup_nerene") -%}
{%- set wakeup_light = "light.bedroom_light" -%}
{%- set wakeup_light_state = states(wakeup_light) -%}
{%- set stop_now = "no" -%}
{%- if wakeup_light_state == "on" -%}
  {%- set old_brightness = state_attr(states(wakeup_light_state), 'brightness') -%}
  {%- set new_brightness = old_brightness + 3 -%}
{%- else -%}
  {% set new_brightness = 3 -%}
{%- endif -%}

{%- if new_brightness >= 256 -%}
  {%- set new_brightness = 256 -%}
  {%- set STOP = "yes" -%}
{%- endif -%}

{# Only output the payload if it is enabled. #}
{%- if wakeup_enabled == "on" or stop_now == "yes" -%}
  {{ '{ "data": {' }}
  {{ '  "entity_id": "' }}{{ wakeup_light }}{{ '",' }}
  {{ '  "brightness": "' }}{{ new_brightness|int }}{{ '"' }}
  {{ '} }' }}
{%- else -%}
null
{%- endif -%}

I have the same idea, using automations directly on the Node Network.
I’m testing some automations and I’ve tested two for now:

  1. There was a problem with the lights being turned on during the day unnecessarily. Then I made an automation that after turning on the lighting will be on for 3 or 4 minutes, defined randomly by the node Delay. So far working perfectly
  2. Room temperature control. Temperature above a value binds and below another value turns off, these values directly defined in the frontend. Working perfectly, too.

Node Red, as I understand it now, has some peculiarities but gradually I complete the test.

What node is the Is Enabled? node? I don’t see that icon in my list.

Looks like the Current State node (that’s been renamed) and it’s checking the condition of an Input Boolean for that flow. If the Input Boolean is set to on then the flow proceeds.

Possibly the icons have changed since the original post.