HELP : How to Mimic HA Zone Triggers in Node-Red

So I was playing around this weekend and finally got to installing Node-Red and I really do like it. It does require you to rethink a bit in how you construct your automations but I think it will get me to a lot less of them to accomplish things I have already built in YAML.

One thing I can’t seem to mimic is Zone Triggers. I have a few in HA (kid gets/leaves school, bus stop, home, etc.) to trigger notifications but I can’t seem to figure out a way to do that (at least not cleanly) in Node-Red. I know I can trigger on a “location change” but can’t seem to figure how to determine if it’s entering or exiting the zone.

Here is an example of the YAML Automation I am trying to replicate. I should add I don’t want to keep the zones in HA anymore, I really want to use Node-Red and the geofence node. The ability to use polygons, circles, and squares vs just circles in HA is very appealing to me.

alias: Enter Zone Notifications
trigger:
  - platform: zone
    entity_id: device_tracker.A, device_tracker.B, device_tracker.C
    zone: zone.home
    event: enter
  - platform: zone
    entity_id: device_tracker.C
    zone: zone.school
    event: enter
  - platform: zone
    entity_id: device_tracker.C
    zone: zone.bus_stop
    event: enter
action:
  - service: notify.iPhones
    data_template:
      title: "Enter Area Alert"
      message: >
        {{ trigger.to_state.attributes.friendly_name }} just arrived {{ trigger.zone.attributes.friendly_name }}.
      data:
        subtitle: "{{ trigger.to_state.attributes.friendly_name }} is on the move."

Tagging @oakbrad (love your blog) and @nordlead2005 as they seem to be very knowledgeable about Node-Red being used with HASS.

I use HA as my state machine and currently let it handle zones because circles work fine for me. Also, note that not everything needs to be in node-RED. Things that are easier in HA I keep in HA.

Using the HA zones, what you have in YAML is really easy to re-create.

Start with an event:state node, and set the entity_id to “device_tracker” and you’ll get all device_tracker entities (the filtering is a simple substring match), or you can just have multiple event nodes so you don’t accidentally pick up device_tracker.D.

Feed everything into a switch node and use the “expression” option for the property and check to see if the state changed. This is because some device trackers will report a new state even though the old state is the same. (my gps logger does)
image

Then feed everything into a switch and switch based on msg.payload. Then that should be fed into either a template for creating the message or into another switch the switch on device_tracker.id.

It might look something like this…


If I was going to use the geofence node, I would probably feed the results of that into a sensor hosted by HA, and then in the flow above instead of monitoring the device trackers, monitor the new sensors.

5 Likes

WOW THANKS SO MUCH!!!

I love this community so much for all the help people are willing to give. But your response goes way beyond any of my expectations for help.

I can’t believe I didn’t realize that the trigger event held the new state and the old state in the JSON. Doh!

Now that I know that, I think along with your tutorial above I will be able to get what I want.

No problem. One thing I find super helpful is to always dump the whole message, not just the payload, when using debug nodes. Yeah, technically the information is in the “info” tab when you select a node, but seeing the message data is really useful (that, and some nodes have poor documentation).

I also just let Home Assistant manage zones and devices.

The geofence node IS pretty nice since you can draw different shapes. I do my presence using a bayesian binary sensor that combines multiple device trackers, so what I did was just use Node-Red to make another device_tracker with a custom geofence. Details: Presence Detection Part 2: Improving Presence with Node-Red - DIY Futurism

Another way to do this is to use the “rbe” node. It will only pass the message if the payload has changed.

Yup, I have been doing that, but of course on everything BUT the event trigger. :blush:

I actually have been reading your blog quite a bit. I had a really hard time with presence when I was still using SmartThings and built a pretty elaborate Core Piston automation to look at Smartthings Presence, IFTTT, etc. They finally integrated Life360 so that at least worked for “Home” and “Away”.

When I moved to HA I did almost use Bayesian it when I was originally setting up HASS. I tried using OwnTracks, but I never could get it working well, at least on all my iOS devices. It seemed it got stuck in the background and didn’t update properly. So then I found this script for L360 that writes to my MQTT broker, which I then currently suck into HASS using the owntracks component. I find this to work really well for my needs.

The only thing I want to fix is the “circle” only geofence shape. I have some zones that overlap if I use circles like our house and the school bus stop so I think using geofence will help with false alarms there.

Thanks again to both you and @nordlead2005 for all the help.

Any chance, you could post the json flow please…

I’m struggling with just the last part of this. I cannot get the contents of msg.payload into notify service node. I’m using Push Bullet, so in the data field of the service node I get an error if I put { “message”: msg.payload }. What am I missing?

Not sure if HA has changed or if there is a problem with my config; however, with HA 81.6 and the iOS app I receive notifications (via the app) that I have entered and exited zones; however, the change does not reflect in a debug message tied to the device location. Any thoughts?

Hi, don´t know if this is solved for you, but I think the solution here is that you dont put anything in the “data” field on the servicenode. It will use the msg.payload and the already set values of “message”, “title” etc.
(From the format-node)

So I don’t use push bullet, but here is how I do my notifications generally.

First I use a template node in the flow to get this formatting, note how I use some of the info from the as a variable.

{"data": { "title":"Door Lock Alert", "message": "The Front Door was opened with {{{data.old_state.attributes.friendly_name}}}", "data":{ "subtitle": "The Front Door Was Opened" } } }

I hope that helps.

strangedesign13

Would it be possible to paste your flow here? I am very close to getting it. I just want to see how the switches work.

Eg Location Entered and Device C.

I am trying to get a notification when a person leaves a Node GEOFENCE

Thanks

I actually never implemented my geofences in this way and have so far left them in HA. I played with it a bit but never really went that far as life kinda got in the way. Sorry.

Any chance you could post your json format for this flow?

1 Like

Hi,
I’m trying to implement a zone based notification in node red too, used the example provided by @nordlead2005 (thanks again for this!) and will build my notification around it.
As an example, here is a simple flow using a state node for your device.tracker (or “person”), a switch for different zones (home and work in this example), a function node to format the notification output (“Mike entered “your-zone””) and my telegram notification - hope this helps.

Node red flow: https://pastebin.com/uivCrNYx