Noobie needing some help creating automations with Node-red and presence detection

Hello,

I have been following some tutorials on creating automations with Node-red. I have been successful in writing a few but I wanted to expand them to include presence detection if I or my wife was home.

In order for home assistant to detect if we were home, I added the following code into my configuration.yaml file:
device_tracker:

  • platform: ping
    track new device: True
    consider home: 2700
    hosts:
    hostone: 192.168.1.170
    hosttwo: 192.168.1.107

image

This setup adds two cards (icons) at the top of the screen that shows Home or Away which is what I want. The only issue is I am trying to use this status to write an automation in Node-red and I can’t find the topic to use so it pulls my home or away status.

Basically, I want to write an automation that will turn the outside lights on when we are both away and it is night time. I wrote a automation with the BIGtimer that utilizes the execute at dusk (which I like) but I only want it coming on if we are not home.

Hope this makes sense?

@tadrian2 What you want to achieve is pretty straight forward however, I see that you are using ping as your presence detection. The main problem you will run into is that most modern phones will turn off their wi-fi to conserve on power whilst in standby mode. This will cause your device_tracker to show you are away when you may well not be. I would suggest getting your presence detection solid before using it to trigger automations. As this automation is not mission critical like alarm disarm/arm etc you can use it but I feel it will trigger whilst you are home anyway as it will be set for times when your phones are most likely going to be in standby mode. If you would like some further information regarding presence detection with node-red feel free to visit my repo as there are examples of how I have setup mine with multiple trackers. I post my flows up so you can feel free to copy and import them into your instance and just change to suit your needs.

My Repo

Good beginners Guide to Presence detection using Node-RED

As for your flow as I understand it you currently using the big timer which is great all you need to do is add you condition. Firstly I setup a group with the trackers I want to be home or away to set the condition.

group:
  family:
    name: Family
    view: no
    entities:
      - sensor.james
      - sensor.tina

Once you have the group this is what entity you will use to set the condition of your flow.

add a current state node and set with the entity id of your group we just created, then set the condition to halt if you’re home that way it will only fire off when your group is not_home.

Hi James,

This is a good start, I have a question though… I got the group setup, how do I get both users into the group?

Thanks for your help by the way!!

My users are a bayesian sensor feed by multiple device_trackers, thats why mine is a sensor rather then device_tracker.

For your you will need to add like so

group:
  family:
    name: Family
    view: no
    entities:
      - device_tracker.person_1
      - device_tracker.person_2

if you would like to see it in your UI just change view: yes

@jimpower That worked, my two entities are now in the Family group however, my devices aren’t showing up when I am setting up the automation in NODE-Red.

They tend to do that until you add them to node-red. You can just type it manually and it will work.

so confirm your group name and type it in.

Hey James,

I tried typing the group just like your example and the debug comes back with “TypeError: Cannot read property ‘addListener’ of undefined”

Is there a way to make a card for my wife and I (see example below) with the toggle switches instead of the identity at the top of HA?

image

OK so to confirm what your setup is your group should look like this

  family:
    name: Family
    view: no
    entities:
      - device_tracker.huawei_h10
      - device_tracker.galaxy_tina

Then confirm your group by going to the states tab on the lower left

image

in there you should see the group you created (after a reboot)

Hi James,

To confirm:

image

For some reason I am getting two nodes coming off of the Current state after I deploy, is this correct?
and it’s not working still :frowning:

1 Like

ok that looks right, can you share you flow for me

Did you use a halt state?

Yes sir, I did use halt state. Is it Big Timer that is causing the issue?

ok so the big timer when using the top output will only fire off once msg when the time conditions are met, so if you have missed that window the rest of your flow won’t get the msg. I use the second one down which fires off a msg every minute. What this does for me that whilst the big timer conditions are true it will continue to fire off a msg every minute until the conditions are not true. This is handy is a device of entity was unavailable at the time the condition was met when it does become available it will get a msg within a minute and do its thing.

I should also mention that the big timer outputs a 1 when condition is true and a 0 when it is not so you can use a switch node to direct what action you want to take on those conditions for you a 1 would be turn lights on and a 0 would be turn lights off.

example of my big timer

My issue is that it turns on the lights even though both of us are home and I set the halt state condition to home.

your entity for the current state node should be group.family

image

That was it. Thanks for being so patient with me and for being so helpful. You definitely know your stuff.

1 Like

I started out just like you :wink: but thats all part of the fun and this community is what makes the journey possible.

If you ever want to debug your flows you can use the inject node to simulate anything you want and use debug nodes at parts of the flow to see what is not working.

image

1 Like

Hi James,

How can I setup the flow so that if one of us aren’t home the lights will come on? Should I just set a flow for each entity or is their a way to set the family group to state away if one of us aren’t home?

Ok I’m going to share with you how I achieve this as its easier to share then explain, I like pictures as you can probably tell.

here is the flow just copy and use the import from clipboard option

[{"id":"95170df2.dc89a","type":"trigger-state","z":"6dcf8230.01070c","name":"Device Tracker 1","server":"4d97f48a.5f4fac","entityid":"","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":330,"y":660,"wires":[["60018fab.875c6"],[]]},{"id":"cd77da06.ff9c68","type":"trigger-state","z":"6dcf8230.01070c","name":"Device Tracker 2","server":"4d97f48a.5f4fac","entityid":"","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":330,"y":860,"wires":[["35866a9a.989f26"],[]]},{"id":"60018fab.875c6","type":"switch","z":"6dcf8230.01070c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"home","vt":"str"},{"t":"eq","v":"not_home","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":620,"y":660,"wires":[["646a67ee.9610b8"],["c4aef439.01c768"]]},{"id":"35866a9a.989f26","type":"switch","z":"6dcf8230.01070c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"not_home","vt":"str"},{"t":"eq","v":"home","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":860,"wires":[["c4aef439.01c768"],["5b86fb29.55f734"]]},{"id":"646a67ee.9610b8","type":"api-current-state","z":"6dcf8230.01070c","name":"Everyone Home?","server":"4d97f48a.5f4fac","halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"device_tracker.2","state_type":"str","outputs":2,"x":890,"y":600,"wires":[["b069aedd.afc52"],[]]},{"id":"b069aedd.afc52","type":"api-call-service","z":"6dcf8230.01070c","name":"Set Family Home","server":"4d97f48a.5f4fac","service_domain":"device_tracker","service":"see","data":"{\"dev_id\":\"family\",\"location_name\":\"home\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":1270,"y":740,"wires":[[]]},{"id":"5b86fb29.55f734","type":"api-current-state","z":"6dcf8230.01070c","name":"Everyone Home?","server":"4d97f48a.5f4fac","halt_if":"not_home","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"device_tracker.1","state_type":"str","outputs":2,"x":930,"y":920,"wires":[["b069aedd.afc52"],[]]},{"id":"c4aef439.01c768","type":"api-call-service","z":"6dcf8230.01070c","name":"Set Family Not Home","server":"4d97f48a.5f4fac","service_domain":"device_tracker","service":"see","data":"{\"dev_id\":\"family\",\"location_name\":\"not_home\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":920,"y":760,"wires":[[]]},{"id":"4d97f48a.5f4fac","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]

1 Like

The last nodes of the flow will actually create a device_tracker.family entity in your home assistant. You can then use this to get the state of to determine to turn on lights or not.

1 Like