Event Trigger State - From --> To...Help Needed

Hello

New to node Red so haven’t yet worked out how to do this…

I have an automation I setup which tracks where my iphone is using owntracks. When I leave the house it will turn off the light in the hallway if it is switched on. When I come home and if its after the sun has gone down the light will turn on.

Problem I am facing is that I am using an events: state node which is watching my iphone for state changes. The light is turning on and off as it should be. I also have a push notification setup which tells me that the event has triggered.

Every 5 minutes or so or when owntracks pushes a new location to HA, Node Red picks up on it and i’m getting a “Left Home” notification each time. So it seems that even though i’m already away from home, when a new location update is pushed to HA the “AWAY” status is updated as well to “AWAY” again, even if its already set to away. Node red seems to pick up on this change and keeps triggering the automation.

Is there a way to setup an event trigger that is set to trigger if my iphone state goes from “home” to “away” then that will stop it from constantly firing? That’s how I used to have it setup with HA automation’s, but moving to node red I haven’t worked out how to do this?

Can anyone help?

yes there is a HA trigger node in which you can set the “previous” and “current” state … it will only trigger when it changed according your setting. I can share a mini flow when I’m back home … but its very easy to find :wink:

Alternatively you can use a RBE node after location event. This will only let pass when your stat has changed!

I would like to see the trigger state node as an example.

It seems very power but I just don’t truly appreciate it yet.

In the info panel it has an example of how to test the trigger with creating an input. I would like to see an example of that if anyone has done it.

It depends, what exactly are you trying to acomplish?

I got to this node by reading about functions and switches to send only changes states though a node.

I have been playing with it, but am tryting to lean by seeing how others do use it.

More specificly I am trying to see how to do this (from the info panel)

NOTE: To test automation without having to manually change state in home assistant send an input payload as an object which contains entity_id , new_state , and old_state properties. This will trigger the node as if the event came from home assistant.

This is mine for presence detection.

The State node will only allow it to continue onto the next step if the payload goes from “home” to “not_home” or “not_home” to “home”

9 Likes

Thanks for your example, I needed this!

1 Like

I realize that this thread is three months old, but what if you have zones created and trigger based on the change from work to not_home? I have several zones, work, bart, that I would like to trigger activities at home.
In NodeRed I can see it change to and from home, not_home, work and bart. But the trigger:state isn’t allowing me to use the work and bart constraints as the previous state. I have tried with the device_tracker and the person.xxxx entity. thoughts?

@MechEng70 have you found anything on this?

I have figured it out with some help from others… I have a function (Locations) that has the following in it:

var newState = msg.data.new_state.state;
var oldState = msg.data.old_state.state;
if (oldState == "not_home" && newState == "home") {
    return [ msg, null, null, null ];
}
else if (oldState == "home" && newState == "not_home") {
    return [ null, msg, null, null ];
}
else if (oldState == "work" && newState == "not_home") {
    return [ null, null, msg, null ];
}
else if (oldState == "BartStation" && newState == "not_home") {
    return [ null, null, null, msg ];
}
else { return [ null, null, null, null ]; 
}

Here is the screen shot of the node-red.

1 Like

@MechEng70 what payload are you passing from “Jason Home” and how are you defining msg.data.new_state.state and msg.data.old_state.state. Thanks

Jason - Home is using person.jason that uses my phone to track my position. you could use the device_tracker.yourphone

@MechEng70 are there templates or something else I need to set-up. I receive the following error:

TypeError: Cannot read property ‘new_state’ of undefined

Thanks
Trevor

Sorry, never received a notification that you replied…
Did you assign a device to a person? System - > Persons (Add persons)
I have only one device under each person to track.

You can use this in the developer tools -> template to see if it works:

You can also use person.yournamehere and see what it provides.

{% if is_state("device_tracker.paulus", "home") and
      is_state("device_tracker.anne_therese", "home") -%}
  You are both home, you silly
{%- else -%}
  Anne Therese is at {{ states("device_tracker.anne_therese") }}
  Paulus is at {{ states("device_tracker.paulus") }}
{%- endif %}

Hi guys,

I would like to compare the current state to the old state of one entity using the node red trigger node.

I want to check if the current state > old state.

How can I do that?

What should I put here?
image

image

1 Like

Perfect. Thanks!

I would like to do something similar except I’m comparing to a different entity.

Current state of current entity > current state of another entity

How would that be entered?

https://www.reddit.com/r/homeassistant/comments/kcyp58/use_a_value_template_as_a_condition_in_node_red/gfu7tfi?utm_source=share&utm_medium=web2x&context=3