# First Automation

For your first automation, we'll start simple and set up an automation to turn on and off a light when the sun comes up and goes down.

This example will use the state, below_horizon/above_horizon, of the sun.sun entity from Home Assistant to trigger the light.

# Events: state

Place an Events: state node onto the workspace and double click on it to edit.

screenshot

# Entity ID

We'll add sun.sun to the Entity ID field and leave the entity id filter type to Exact as we only want to match one entity.

# If State

If State is a conditional that will be checked against the state of the entity when it triggers the node. If the condition is found to be true the message will be sent out the top output and if found to be false it will be sent out the bottom output. If no conditional value is set there will only be one output and all messages will flow through it.

For this example we will set the If State to above_horizon.

screenshot

# Call Service

Now that the trigger is set up it's time to set up the action. Most communication done with Home Assistant will come in the forms of services calls using the Call Service Node.

Drag two Call Service nodes onto the workspace and connect them to each of the outputs from the Events: state node.

screenshot

Since we made the conditional in the Events: state node if state is equal to above_horizon the top output will be the action to turn off the light and the bottom output will be the action to turn on the light.

The three fields that are relevant for this example are: Domain, Service, and Entity Id. Domain is going to be light, Service is going to be turn_off, and Entity Id is going to be light.front_porch. The Second Call Service node will be the same except the Service is going to be turn_on.

screenshot

# Complete Automation

At this point, you can deploy and should have your first working automation in Node-RED.

screenshot

[{"id":"b74ada49.d7e408","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sun.sun","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"above_horizon","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":244,"y":784,"wires":[["1f467cbb.0c3983"],["da5ff3e0.cbb2a"]]},{"id":"1f467cbb.0c3983","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.front_porch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":474,"y":784,"wires":[[]]},{"id":"da5ff3e0.cbb2a","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.front_porch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":474,"y":832,"wires":[[]]}]

Also see: