Node-Red entity changed amount of time ago

I am currently transfering some of my automations to Node-Red, one of this automations includes a “For” as in time

for:
  hours: 48
  minutes: 0
  seconds: 0

Since a delay/stoptimer of 48 hours is a little long I was wondering if there is another way of detecting if an entity its state did not change for 48 hours.

I found out I could use templating with the “get template” node, but how will I use this or an node to measure if an entity has not been changed for 48 hours or more.

{{ as_timestamp(states.binary_sensor.deur_douche.last_changed) | timestamp_custom('%d-%m-%Y %H:%M',False)}}

This might work

I found a solution to the problem.

I create a get template card with this template below inside of it if the entity did not change for 24 hours (84600 seconds) it will output True as payload which you can catch with a switch node:
{{(as_timestamp(now())-as_timestamp(states.input_boolean.test.last_updated)) > 84600 }}

Thanks for posting the solution.
Your day is only 23:30 long though :wink:
GV

You could also accomplish this with a poll-state node.

image

[{"id":"bddbd75a.af7608","type":"poll-state","z":"ffbd7f06.4a014","name":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"input_boolean.test","state_type":"str","halt_if":"$entity().timeSinceChangedMs > 86400000","halt_if_type":"jsonata","halt_if_compare":"jsonata","outputs":2,"x":268,"y":688,"wires":[[],[]]}]
7 Likes

@Kermit This is exactly what i was looking for, Thank you very much :smiley:

Does everyone have right value for the timeSinceChangedMs?
For me it is alway low one. E.g. since 18T19:26:03 to 19:54:45.01 the value is only 90 ms.

image

Hmm, with my motion sensors the timeSinceChangedMs increments every time I checked the value and the output was the right measurement (Ms).

I was able to fix my motion sensor lastchanged condition check also with the get entities node. Now I can use it in Node-Red to run only actions if motion was > 20 minutes ago. Thanks @Kermit !

Hopefully this isn’t too far after the fact … but if you still have the nodes, could you share the export?

Hey @AlecD,

This feature has recently become a standard feature of the Node-Red Home-Assistant Pallette (Nodes).

If you can’t see the For section yet, you may to update your pallettes that was enough for me to make it visible.

Thank you. I do have the current version and do have the For box. I was down the rabbit trail of trying to figure out how to accomplish using the time since last change of specific state as a condition.

So, something like IF motion sensor = on THEN IF presence = home within last 5 minutes THEN perform action1 ELSE perform action2.

I know there’s different ways of approaching this, but was kinda hoping for being able to evaluate this condition within the flow.

@AlecD
I only have a template for you with which you can check how long it has been since its last change.
If you put a current state node in front of this template node I think you will kinda achieve what you are looking for.

{{(as_timestamp(now())-as_timestamp(states.person.username.last_updated)) > 172800 }}
[
    {
        "id": "5e11ce76.5b512",
        "type": "api-render-template",
        "z": "c758b7eb.b557c8",
        "name": "Last Changed?",
        "server": "d0bd252f.2cce48",
        "template": "{{(as_timestamp(now())-as_timestamp(states.person.username.last_updated)) > 84600 }}",
        "resultsLocation": "payload",
        "resultsLocationType": "msg",
        "templateLocation": "template",
        "templateLocationType": "msg",
        "x": 700,
        "y": 900,
        "wires": [
            [
                "434fdb7c.9598f4"
            ]
        ]
    },
    {
        "id": "434fdb7c.9598f4",
        "type": "switch",
        "z": "c758b7eb.b557c8",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "True",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "False",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 850,
        "y": 900,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "d0bd252f.2cce48",
        "type": "server",
        "name": "Home-Assistant",
        "legacy": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true
    }
]

Sorry for the late response, but this looks neat! I’m going to incorporate this somewhere and give it a shot! Thanks for the assistance.