Using last_tripped_time in switch node

I have a HA Automation that I use to turn on the garage light when I open the garage entry door if there was motion in the mudroom in the last seconds. See yaml code below. I would like to move this automation over to Node Red but I can’t seem to work out condition portion in Node Red. What is the best way to accomplish this?

'- alias: ‘Garage Light’
initial_state: ‘on’
trigger:
- platform: state
entity_id: binary_sensor.garage_entry_door
to: ‘on’
condition: ‘{{ as_timestamp(now()) - as_timestamp(states.binary_sensor.mud_room_motion.attributes.last_tripped_time) | int < 60 }}’
action:
- service: switch.turn_on
entity_id: switch.light_garage

In Example #1 $toMillis() may need to be modified depending on the format of last_tripped_time.

image

[{"id":"58ceb780.97eb38","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"entityidfilter":"binary_sensor.garage_entry_door","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"$entity().state = \"on\" and\t$millis() - $toMillis($entities(\"binary_sensor.mud_room_motion.attributes.last_tripped_time\").attributes.last_tripped_time) < 60000","halt_if_type":"jsonata","halt_if_compare":"jsonata","outputs":2,"output_only_on_state_change":true,"x":260,"y":1008,"wires":[["dbd86abd.877968"],[]]},{"id":"dbd86abd.877968","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.light_garage","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":554,"y":1008,"wires":[[]]}]

[{"id":"c0eef319.fa8bd","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"entityidfilter":"binary_sensor.garage_entry_door","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":260,"y":1088,"wires":[["c5cee556.c86578"],[]]},{"id":"c5cee556.c86578","type":"api-render-template","z":"ffbd7f06.4a014","name":"condition","template":"{{ as_timestamp(now()) - as_timestamp(states.binary_sensor.mud_room_motion.attributes.last_tripped_time) | int < 60 }}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":528,"y":1088,"wires":[["1594836b.371f9d"]]},{"id":"ca5be70a.58f018","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.light_garage","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":826,"y":1088,"wires":[[]]},{"id":"1594836b.371f9d","type":"switch","z":"ffbd7f06.4a014","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":658,"y":1088,"wires":[["ca5be70a.58f018"]]}]

[{"id":"79563e31.39e3e","type":"server-state-changed","z":"ffbd7f06.4a014","name":"","version":1,"entityidfilter":"binary_sensor.garage_entry_door","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":260,"y":1168,"wires":[["9c32c87b.f47fa8"],[]]},{"id":"38e5f7b2.203908","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.light_garage","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":682,"y":1168,"wires":[[]]},{"id":"9c32c87b.f47fa8","type":"function","z":"ffbd7f06.4a014","name":"","func":"const gHomeAssistant = global.get('homeassistant').homeAssistant.states;\nconst lastTripped = new Date(gHomeAssistant['binary_sensor.mud_room_motion'].attributes.last_tripped_time).getTime();\n\nif(Date().now() - lastTripped < 60000) {\n    return msg;\n}","outputs":1,"noerr":0,"x":514,"y":1168,"wires":[["38e5f7b2.203908"]]}]

please format your work bro make it easyer for people to fix

- alias: ‘Garage Light’
initial_state: "on"
trigger:
- platform: state
entity_id: binary_sensor.garage_entry_door
to: "on"
condition: "{{ as_timestamp(now()) - as_timestamp(states.binary_sensor.mud_room_motion.attributes.last_tripped_time) | int < 60 }}"
action:
- service: switch.turn_on
entity_id: switch.light_garage

looking at your code

you are trying to use some jinja
but HA some not know

you need to tell HA

so

condition: template
value_template: >-
  {{ as_timestamp(now()) - as_timestamp(states.binary_sensor.mud_room_motion.attributes.last_tripped_time) | int < 60 }}

if you paste this in the template

 {{ as_timestamp(now()) - as_timestamp(states.binary_sensor.mud_room_motion.attributes.last_tripped_time) | int < 60 }}

do you get the answer you want ??

Thank you. I try this out tonight when I get home.

Example #1 did not work as you suspected. The last_tripped_time attribute is in the following format: 2019-11-04T06:15:00-06:00. See attached picture.

Example#2 worked. It processed the HA Jinja2 template as it did in yaml.

Example #3 produced the following Node-RED debug error: “TypeError: Date(…).now is not a function”

Some typos on my part.

Example #1 should be:

$entity().state = "on" and
$millis() - $toMillis($entities("binary_sensor.mud_room_motion").attributes.last_tripped_time) < 60000

Example #3

const gHomeAssistant = global.get('homeassistant').homeAssistant.states;
const lastTripped = new Date(gHomeAssistant['binary_sensor.mud_room_motion'].attributes.last_tripped_time).getTime();

if(Date.now() - lastTripped < 60000) {
   return msg;
}

Example #1 is good now.

Example #3 is not yet working. I can’t see anything obvious wrong but then again I very green.
I can connect the event straight to the service call and the light comes on so the problem lies with function node.

Below is complete debug message capture triggered by the binary_sensor.mud_room_motion

I could use any of the working examples that you have provided but I would to see the last example working for my own edification and others might benefit from the working example.

Example #3 is working for me in my tests. Are you still getting an error or something else?

The update example #3 posted on 11/4 does work. I just was driving it from the wrong HA event during testing. My bad. Kermit you did a great job. Thank you for the three (HA template, JSONATA, & Java) good examples of bring in the HA attribute of last_tripped_time into Node-Red.