Node Red Equivalent for HA Automation Time Condition

I am building an alarm system using HA. I initially did my automations in HA, but decided to try out Node Red. One of the things I was doing through the HA automations was to ignore any sensor triggers for the first 120s after the alarm was activated. This is to allow time to leave the premises. This was pretty simple in HA with an automation that looks like this:

  • id: ‘1615492356581’
    alias: Siren On
    description: ‘’
    trigger:
  • platform: state
    entity_id: binary_sensor.pir1
    to: ‘on’
    condition:
  • condition: device
    type: is_on
    device_id: aba29906bf1af0c6f840effc60774f59
    entity_id: switch.alarm_switch
    domain: switch
    for:
    hours: 0
    minutes: 0
    seconds: 120
    milliseconds: 0
    action:
  • type: turn_on
    device_id: 773a1d29a20abf6c37f3032d1d9c231f
    entity_id: switch.siren_switch
    domain: switch
    mode: single

I was able to add a condition that a siren switch would only be triggered by a sensor if the alarm had been on for longer than 120s.

I was not able to replicate this behavior in Node Red. After quite a bit of experimentation I managed to come up with something similar. After activating the alarm I start a countdown timer, convert the counter (seconds) to a Cancel message and send this to a trigger node, which then delays and discards any messages it gets from a sensor while the timer is running (ie while it gets the cancel messages). The flow looks like this:

[{"id":"46b6b4de.4905bc","type":"tab","label":"Alarm - Main","disabled":false,"info":""},{"id":"42dd48c0.8f3ed8","type":"group","z":"46b6b4de.4905bc","name":"Alarm On/Off Alerts","style":{"label":true},"nodes":["41cf6c21.12ff64","cce74948.61cf38","9d026ac4.808a58","f5f24c95.e4356"],"x":254,"y":339,"w":572,"h":182},{"id":"b9bcc64f.097028","type":"group","z":"46b6b4de.4905bc","name":"Disable Alarms for 120s","style":{"label":true},"nodes":["7000a5b5.ec934c","df59662d.1a4098"],"x":254,"y":119,"w":332,"h":162},{"id":"c6c84d79.9bbbb","type":"server-state-changed","z":"46b6b4de.4905bc","name":"PIR 1 Triggered","server":"73543804.66ae48","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pir1","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,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":520,"y":40,"wires":[["33108ce7.91e244"],[]]},{"id":"d71580c3.e3584","type":"api-call-service","z":"46b6b4de.4905bc","name":"Turn Siren On","server":"73543804.66ae48","version":1,"debugenabled":true,"service_domain":"switch","service":"turn_on","entityId":"switch.siren_switch","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1020,"y":200,"wires":[[]]},{"id":"df59662d.1a4098","type":"countdown","z":"46b6b4de.4905bc","g":"b9bcc64f.097028","name":"Cancel Alarm Timer (120s)","topic":"","payloadTimerStart":"Disable","payloadTimerStartType":"str","payloadTimerStop":"Enable","payloadTimerStopType":"str","timer":"120","resetWhileRunning":true,"setTimeToNewWhileRunning":false,"startCountdownOnControlMessage":false,"x":400,"y":160,"wires":[[],["7000a5b5.ec934c"]]},{"id":"6d7e5c83.539f74","type":"server-state-changed","z":"46b6b4de.4905bc","name":"Alarm On","server":"73543804.66ae48","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.alarm_switch","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,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":100,"y":300,"wires":[["df59662d.1a4098","f5f24c95.e4356"],["9d026ac4.808a58"]]},{"id":"33108ce7.91e244","type":"trigger","z":"46b6b4de.4905bc","name":"Cancel/Allow Alarms","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"1005","extend":false,"overrideDelay":false,"units":"ms","reset":"Cancel","bytopic":"all","topic":"topic","outputs":1,"x":800,"y":200,"wires":[["d71580c3.e3584"]]},{"id":"7000a5b5.ec934c","type":"change","z":"46b6b4de.4905bc","g":"b9bcc64f.097028","name":"Send Cancel","rules":[{"t":"set","p":"payload","pt":"msg","to":"Cancel","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":240,"wires":[["33108ce7.91e244"]]},{"id":"f5f24c95.e4356","type":"api-call-service","z":"46b6b4de.4905bc","g":"42dd48c0.8f3ed8","name":"Alarm On Alert","server":"73543804.66ae48","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.alert_switch","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":360,"y":380,"wires":[[]]},{"id":"9d026ac4.808a58","type":"api-call-service","z":"46b6b4de.4905bc","g":"42dd48c0.8f3ed8","name":"Alarm Off Alert 1","server":"73543804.66ae48","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.alert_switch","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":360,"y":480,"wires":[["cce74948.61cf38"]]},{"id":"cce74948.61cf38","type":"delay","z":"46b6b4de.4905bc","g":"42dd48c0.8f3ed8","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":540,"y":480,"wires":[["41cf6c21.12ff64"]]},{"id":"41cf6c21.12ff64","type":"api-call-service","z":"46b6b4de.4905bc","g":"42dd48c0.8f3ed8","name":"Alarm Off Alert 2","server":"73543804.66ae48","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.alert_switch","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":720,"y":480,"wires":[[]]},{"id":"73543804.66ae48","type":"server","name":"Home Assistant","addon":true}]

Does anyone have a better/simpler way of replicating the HA condition in Node Red?

Hi there, you can use a template sensor between the PIR and the siren switch like this.
image

The code for the template switch is this

[{"id":"ea5af559.3c3128","type":"api-render-template","z":"51045e07.bbf87","name":"Activated for 120s","server":"d177950.2a5aa68","template":"{{ states('entity_id') == \"home\" and as_timestamp(states.sensor.date_time_iso.state)|round - as_timestamp(states.entity_id.last_changed)|round > 120  }}","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":780,"y":200,"wires":[[]]},{"id":"d177950.2a5aa68","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

You will also have to enable time iso sensor in HA. Please replace the entity id and state in the template to the entity_id and activated state of the alarm.

Brilliant! Thanks for the assistance. I thought there must be a better way …

Why not simply code the delay_time in your alarm control panel? It will do the same if I understand you correctly:

delay_time integer (optional, default: 60)

The time in seconds of the ‘pending’ state before triggering the alarm.
1 Like

Thanks for the pointer. I’ve only just managed to get to this now and it looks like a very useful component. I think this will make the whole setup much easier.