HA and Node-Red: Time-based Automations

You’ve decided you want an automation to run based on time. Creating a time-based Node-Red flow is extremely simple. In my home I have speakers that I want to turn off at 10pm in case any of them were left on. Any speaker in my house being on will turn on the amplifiers which use energy. I use automation to cut down on my electrical bills. While I’m using it to turn off speakers, the process is the same for turning or off any smart devices.

To get started we will use an inject node and a debug node which are the nodes I recommend to being any flow.

The Inject node (timestamp) and the debug node (debug 1)

Double-click the inject node to open the options. The inject node has options for time of day and how often to repeat. This is where we will set the daily midnight trigger.

Blank inject node

Name your inject node the time of the trigger and how often. It’s not required but it helps keep the flow organized without having to open up the nodes.
If you want this to repeat, select the Repeat dropdown on select all of the times you would like it to repeat. Since I want mine to trigger at 10pm every day, I will select “at a specific time” and then select the days I want it to trigger, and the time. Note that this node uses 24 hour time, so plan accordingly.

Inject node set to fire every night at 10pm

Now we have an inject node that will fire every day at 10pm. By itself that is not going to do anything, so we will wire it up to some service calls.
The “call service” node is what we will want next for this flow. The “call service” node is used to call different actions within Home Assistant.

The “call service” node in the flow

The red triangle above the node is to remind you that this node needs to be setup before it can be used. Double-click on this node to bring up the options menu.

Options menu for the call service node

Name: Again, you will want to name the node to help keep the flow organized.
Server: This is where you select which server to use. There should be no reason to change this.
Domain: This is where you select what type of device you are going to toggle. Since my speakers are technically “media players” then I will select the media_player domain. If you are working with lights or switches then select either “light” or “switch.”
Service: This is where you select what action in the specific domain to use. I will be turning my speakers off, so I will use the “turn_off” service. There is also a service to turn on or toggle so whatever you need, this is where you select that.
Area: The specific area you would like to focus on. I tend to leave this blank and just select the entities.
Device: Which device you would like to focus on. Again, I tend to leave this plank and just select the entities directly.
Entity: This is where you start selecting your entity or entities. This node will send the selected action to each device listed, so make sure to put only the entities you want the action to work on.
Data: If you need to send specific data to the next node in the flow, this is where that goes. This flow does not require any additional data to be sent down the line, so this will be blank for now.

Call service node ready to go


Now the call service node is ready to go. The domain has been selected and the service for that domain is ready to go as well. So this node will turn off the office and rear porch media players. Connecting this to the inject node will cause it to trigger every day at 10pm.

The flow all wired up

This is the flow in all of it’s glory. Simply click and drag to create the wires that connect the nodes. There is a red button in the top right corner of Node-Red that says “Deploy.” This is how you sav your flows and also how you get the system to start using them. You want to press deploy after every change you make. So this flow will trigger every night at 10pm, and it will turn off all of the speakers in my house. The debug node at the end is simply there for troubleshooting. If you every have a flow that does not work correctly, the debug node will tell you what information is being sent and also what errors are occuring.

The gray box to the left of the inject node is there so that you can manually trigger that flow. In the case of my speakers, I want to make sure that the flow will actually turn the speakers off, so I will press that button as a test. And it is all done! A simple time-based flow that turns off my speakers every night at 10pm. I don’t have to worry about forgetting to turn them off, because my house will do it for me!

Leave a Reply

Your email address will not be published. Required fields are marked *