Trigger state node property constraint && Last_changed vs Last_updated

Recently discovered the nodered HA populates global context variables which means I can use more programming logic in function nodes than ever before so I can make automations in my form of thinking and smaller as I can program deeper in the function node. Which is awesome! I also discovered that I can also save more nodes by using the constraints in the tigger state node.

Here are my two questions that are related:

  1. How do I use the new_state.last_changed property such that I can make a constraint that is true when the timestamp is less than 5min ago using the property constraint in the trigger state node. (I know I can use the function node with javascript use the new Date() to convert it into ms and then check if the number is less than 5min or 300000 ms however, I want to do this inside the trigger state node
  2. What is the difference between last_changed and last_updated?

I realize this is an old topic but whenever I’ve fumbled around for the answer to this exact question enough times that I want to put an answer on it. Especially since if you search the form for ‘last_changed vs. last_updated’ its the first result and I feel like this every time I see it:

The difference between last_changed vs. last_updated is that last_updated is set any time any entities state or any of its attributes are changed whereas last_changed is only set when the state of an entity changes. These fields are documented here

Also for the Node RED portion of the question, if you want to add a constraint of “only evaluate to true if some other entity has been updated/changed more then x minutes ago” you can do it with something like this:
Screen Shot 2020-05-14 at 3.34.33 PM

Essentially add a constraint and pick the entity ID you want then select either last_changed or last_updated as the property (depending on whether you care about any change or only a state change). Then you can do a mathematical comparison with the jsonata expression of $fromMillis($millis() - x) where x is the time in milliseconds. My screenshot is a constraint I use to ensure that an event only fires if the upstairs lights were last changed more then 5 minutes ago.

Would like to note that this would only help you if you wanted to do constraints of the following form:

  • Comparing the last updated/changed timestamp of another entity relative to now
  • Comparing the last updated/changed timestamp of one entity relative to another one
  • Comparing the last time the current entity was updated/changed relative to now (or another entity)

What it can’t do is add a constraint of “has this entity been in this state for x minutes”. Because the trigger state node is only evaluated on a state change event and that only occurs when state actually changes (not x minutes after it changes). If you try and compare the current entity’s new_state.last_changed/updated to something its probably not going to work.

If your goal is to only execute a Node RED flow when an entity has been in some state for x minutes, the easiest way I’ve found is to follow up a events: state or trigger: state node with a wait until node that watches your entity for further state changes and proceeds with the flow if it times out. If your node is not doing an exact match on entity name and state you can use the input options to provide the entity ID and state to watch for in the payload using a change node first.

Anyway that is all, hope it helps someone.


This was a great answer, really solved my need exactly to the point. Thank you!!!

Thanks! I share that memes point many a time too! Lol.

Never thanked you for the response so thank you! I have since changed many my automations and now just use YAML or Appdaemon and stopped using Nodered mostly because those other two frameworks work a bit better for my thinking. Still use NodeRed for 1 main thing tho. They each serve a purpose. :slight_smile: