Node Red and Home Assistant

I have used Home Assistant some while. It is very powerful and can handle most things. However it takes some effort and time to get it the way you want. You will have to learn how to handle yaml files. This is fine and is not really a problem until you start building your automations. This can be frustrating and you will also have to reload or restart HA in order to test them.

This in an example from HA for turning of lights when the sun is set. One space to much and you will have a config error.

# Example configuration.yaml entry
group:
  living_room:
    - light.table_lamp
    - light.ceiling
    - switch.christmas_lights

automation:
  alias: Turn on the lights when the sun sets
  hide_entity: True
  trigger:
    platform: sun
    event: sunset
    offset: "-01:00:00"
  condition:
    condition: state
    entity_id: group.all_devices
    state: 'home'
  action:
    service: homeassistant.turn_on
    entity_id: group.living_room

I just discovered how easy this is by using Node Red. Things that took me hours before can now be done in minutes. I have just started to learn but noticed that there were no good resources of getting started with the basics so hopefully this can help someone. After doing the steps below you will be able to use Node Red for all automations, connect it with HA, do some simple flows and send notifications. In this case I have used pushbullet.

I have the following devices connected in the flows.

  • Yale Door connected with Verisure
  • Owntracks installed on cell phones
  • Unifi tracker
  • Aeotec nanodimmers
  • Aeotec multisensors
  • Some cameras connected to a Synology nas and surveillance station

Installing, setting up and creating a test flow

Start by installing Node Red. I am using Hass.io so just go to hass.io and add on store. You might have to include the community Hass.io Add-ons as this is not shown by default.

You will see some configurations and make sure you set some passwords.

{
  "log_level": "info",
  "credential_secret": "anysecretyouwant",
  "users": [
    {
      "username": "admin",
      "password": "mypassword",
      "permissions": "*"
    }
  ],
  "http_node": {
    "username": "admin",
    "password": "mypassword"
  },
  "http_static": {
    "username": "admin",
    "password": "mypassword"
  },
  "port": theportyouwanttouse,
  "ssl": true,
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem",
  "require_ssl": true,
  "system_packages": [],
  "npm_packages": [],
  "init_commands": []
}

If you want to you may add some lines to configuration.yaml in order to get node red in the sidebar.

panel_iframe:
 nodered:
   title: 'Node-Red'
   icon: 'mdi:shuffle-variant'
   url: 'https://myurl.com:888888/'

As you already have this file open add something like this.

input_boolean:
  alarm:
    name: alarm
    initial: off
    icon: mdi:home

The input bolean does not really do anything. It is just a dummy switch. However this is useful for setting different states. In the example further down we set the state alarm to on or off. It could be called homemode or something similar maybe.

Time to get started with node red and a very simple example. Go to https://yourdomain.com:1880 if you use the default port. First we should make sure that HA is connected. Drag call service (the blue in the middle below) to the right like in the picture below. Doubleclick it. Make sure that Home Assistant is shown under server. Click the pen in order to change settings.

Create a simple flow like above. Choos any switch you have.

You may have to check the entity in HA. Paste this in the data field.

 

If you want to turn on a lamp or maybe dim the light you can use light turn with the json {“entity_id”:”light.dimmer_livingroom_switch”, “brightness”: 20 }

You can drag a debug node anytime and place this anywhere to see the input. If you want to trigger something manually drag the inject node. Press deploy and press the inject button. Hopefully the switch is turned on.

Creating a flow for presence

Now we should create another flow to manage the home/not home bolean.

The flow in the image below is the flow that sets the alarm mode on or off. Off when no one is home and on if not. I will try to explain each step.

The first “personer och enheter” is events state node. This checks the group in HA called persons. I have added devices in this group. Set the entity ID to group.persons. The output from this group is home or not_home. So if no one is home it will send not_home and if a device is home it will send home. I will get back to this later how to use device tracking. I check both with unifi and owntracks.

The second node is a switch. It outputs to the first if if not_home and to the second if home.

 

A timer waits for 5 minutes

Is door locked?

Another switch sending 1 if locked

Turn input bolean alarm on.

So far so good. Now do the same but check if the door is unlocked.

If everything works now the bolean switch will turn on and off depending if there is anyone home. You could add more things like the Aeotec multisensor. However motion does not mean we are home. It could be a thief.

 

Now we can use the bolean value to create flows and set the house in a different state. I have used the states on or off. The switch only have one output to 1.

For example I turn off the livingroom lights. You will have to copy the JSON data from home assistant and paste this in Node Red.

Sending notifications to Pushbullet

You can use any notification you have in HA in node red. Just add a node and choose notify and pushbullet. You will also have to add the line below in configuration.yaml. And register a free account for pushbullet.

  - name: pushbullet
    platform: pushbullet
    api_key: yourapikey

The settting for pushbullet is like below. Enter your entity in data. In my case it is: { “message”: “Alarm mode on”, “target”: [ “device/thecellphonename” ] }

 

This is it. You can use inject to send a testmessage or toggle the bolean switch in HA. I aws about to show how I have used owntracks as well but ran into some problems. I will get back to this later on.