Detecting state change with Node-Red?

How do I detect a state change with Node-Red, not just have an action on receiving a payload.

for example, I have a “Switch node” that when it sees a device payload with Home, it opens the garage.

What I’d like is to only open the garage if the state changes from Away to Home. Not just if it receives another Home notification.

Here’s a better example of what I’m looking for.

I detect a state node with a filter of sun.sun
when it’s below_horizon I turn on the Porch Light
when it’s above_horizon I turn off the Porch Light

With debug turned on, I see it’s getting the below_horizon every minute which sends the command to turn on the light. While this does what I want, it’s unnecessary to turn on the light every minute.


1 Like

Here’s an example. Paste this into a function node, set the number of outputs to 2, customize the states to suit the state node you’re coming from.

var newState =;
var oldState =;
if (oldState == "not_home" && newState == "home") {
    return [ msg, null ];
else if (oldState == "home" && newState == "not_home") {
    return [ null, msg ];
else { return [ null, null ]; 

In this case I was parsing the output from a device tracker state node - I wanted to route the message to the first output if the state was home, but only if the previous state was not_home. And route to the second output if vice-versa. You could change it to whatever old and new states you want, and even add outputs for additional old and new state changes.

It’s basically a targeted rbe node and switch node in one.


worked great.


The new trigger node in 0.30 can do this as well in a single node.

I’m not sure to fully understand what is going on. For example, where does coming from? do you have to declare the variables somewhere?
I’m trying to implement a “from: to” in node-red but i’m not sure where to find the “from” state value. Should I store it in a varaible?

Here is my HA automation:

- id: a0004
  alias: Veilleuse Nuit
  - platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar
    from: '0'
    to: '8'
    condition: and
    - condition: state
      entity_id: input_boolean.pir_status
      state: 'on'
    - condition: time
      after: '23:00:00'
      before: 05:30:00
  - service: script.red_light

Old post, but, could you please explain how to use the trigger node to do the same as above?

Here’s an example, you just use the constraint feature in the trigger node to check that the previous state was not_home.


Thank you

I am very interested in State Triggers.

I hope there is some more documentation or examples in the future.

It seems very powerful.

Just to be clear; it looks like @e_jp and @Hans_van_der_Drift might be missing a step: the Trigger and Switch elements are part of the NodeRed plugin which serves as a friendly automation gui on top of your home assistant sensors and data; but it seems you’re editing the configuration yaml files by hand.

1 Like

I noticed the same thing in that the message was being resent every minute from the sun node, and thought it wasteful to send it across the network every minute as well (via zwave in my case). I simply inserted two Current State nodes between the sun node and the two Turn On/Off nodes, as follows:

On the “Turn On” path, I use the Current State node to check if the light Status was OFF, and then wired the “if state is True” output (top one) to the Turn Light On Call Service node. The bottom output was unused, effectively halting the logic.

Similarly, on the “Turn Off” path, I use the Current State node to check if the light Status was ON, and then wired the “if state is True” output to the Turn Light OFF Call Service node. The bottom output was unused, again, effectively halting the logic.

The calculations were still being done in HA, but nothing was sent across the network via zwave.

Hope that makes sense.

I was searching for momentatry switch and came across this post. I figured I’d post here.

It creates a Switch in Home Assistant


to make it unique,

  1. change the “name” values in the My Switch element
  2. change the Entity ID in the Trigger and Turn Off elements
  3. replace the yellow and pink elements with your desired service/action.
[{"id":"f012c7fb.d76df8","type":"trigger-state","z":"f69946fd.2ac1b8","name":"My Switch Trigger","server":"cd4403b5.968d1","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"switch.my_switch","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"odgjh18ppkk","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"sr5e2tlj04","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":530,"y":720,"wires":[["69ef9e4.27b156","387cc7d7.1c98a8"],[]]},{"id":"69ef9e4.27b156","type":"api-call-service","z":"f69946fd.2ac1b8","name":"Turn Off My Switch","server":"cd4403b5.968d1","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.my_switch","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":790,"y":740,"wires":[[]]},{"id":"41975c3.22290a4","type":"mqtt out","z":"f69946fd.2ac1b8","name":"","topic":"OpenZWave/1/command/setvalue/","qos":"","retain":"","broker":"250eeee2.afada2","x":1080,"y":700,"wires":[]},{"id":"9e5e096b.d2ef28","type":"ha-entity","z":"f69946fd.2ac1b8","name":"My Switch","server":"cd4403b5.968d1","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"My Switch"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:account-arrow-left"},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":310,"y":720,"wires":[[],[]],"outputLabels":["allowed","blocked"],"icon":"node-red/arduino.png"},{"id":"387cc7d7.1c98a8","type":"change","z":"f69946fd.2ac1b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"ValueIDKey\":281476526391348,\"Value\":29}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":700,"wires":[["41975c3.22290a4"]]},{"id":"cd4403b5.968d1","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"250eeee2.afada2","type":"mqtt-broker","z":"","name":"","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

There is a momentary switch component available in HACS. It creates a switch entity that turns itself off after a specific time. You can then use that entity in node red or whatever you want. With this you don’t have to go:
node red > HA > node red > HA > use case. (It’s just a little bit cleaner)

1 Like

Clean is relative. You’re using two separate additional software. The HACS integration will cause unapproved warnings in the logs at startup. This method keeps the entire start-to-finish management within a single place.

You can also install it without HACS and don’t have to use node red…
I just thought it would be cleaner because of the traffic between two containers. You don’t have to create an entity in node red > toggle on the entity via HA > check with node red if the entity has been turned on > turn off the entity with node red again. And I think many HA users also use HACS.

But I think it’s getting a bit off-topic here.

This thread is about detecting state change with Node Red. Sure, this could all be done with other components, but MQTT is a strong use case for Node Red. We could have done this with:

  1. configuration.yaml input boolean
  2. automation.yaml
  • Trigger input boolean
    -from: off
    -to: on
    -for: 00:00:00
  • Action
    -input boolean
  • Action
    -…perform action

…and repeat a bunch of times. But that’s a very unwieldly configuration. In my particular case, I am sending MQTT messages to a Sound Switch, and I have 29 sounds I plan to automate per-switch. This is a heck of a lot easier within Node Red due to copy-paste.

I understand your point and I also don’t like many HA automations.

I just wanted to show you that there are other solutions for momentary switches.

This is very old but wanted to say Thank You - googling this issue for a simple (was previous state of Home == 0) gave me all sorts of complex solutions - Triggers are clearly the way to go!