2 hours - rolling data

I am a new to Node Red and so far I am really enjoying it. Coming from a PLC and electrical background, the ‘ladder logic’ type layout is much easier for me to understand compared to YAML files.
I am currently struggling with one particular element of an automation flow however. I have a set of batteries that came out of equipment for the winter that are on a charger that is connected to a Kasa EP25 outlet with energy monitoring. I would like the automation to work as follows:

  1. Charger turns on and begins charging the batteries (electrical consumption grows to 50+ watts)
  2. Batteries begin to reach full charge (may take hours or days) and the electrical consumption steadily drops
  3. Once the electrical consumption reaches less than 5w and stays under 5 watts for 2 hours, turn off the outlet
  4. Start a timer for 14 days
  5. Restart the process

What I am unable to figure out how to implement is the 2 hours where the electrical consumption stays under 5w. The issue I am running into is when the batteries are nearing 100%, they bounce around from 8-5 watts for a long time, so I cannot just use a timer that triggers at 5w. I’d like a way to watch the electrical consumption for a rolling consecutive 2 hours and if the electrical consumption has been under 5 for the last 2 hours, proceed to the next step. How might I implement that?

A similar use case I have would be humidity in the bathroom. I’d like to turn on the fan at a high level value, and run the fan until it has been below the low value level for a consecutive 15 minutes.

Similar to a hysteresis, but with time rather than an analog value.

Use the events state node

Regarding your 14 day timer, i suggest you set an input_datetime with now+14 days and then use the time node to trigger on this datetime instead of waiting for 14 days

Thank you for the guidance. That event state node is perfect. I didn’t realize it worked with the non binary states.

Now for more dumb questions. I like your idea of using now +14 days, but I am not following on how to implement that. Which node can I use that allows an input for the ‘now’ trigger?

{"datetime": $moment().add('days', 14)}

Thank you for the help. I think I have it working now.

However, the timezones are causing a struggle. I need to use a -6 hr offset (I am CST) in the time node in order to get the correct time trigger. Is there any way to set the correct time zone in the service call node? Seams counter intuitive to set a timestamp, add 15 seconds (for testing, will use 14 days later) to it, then subtract 6 hours in the next step.

[{"id":"8279a1421d0a85fb","type":"api-call-service","z":"48da3aa08f5bc110","name":"","server":"58bff01.29ea91","version":5,"debugenabled":false,"domain":"input_datetime","service":"set_datetime","areaId":[],"deviceId":[],"entityId":["input_datetime.charger"],"data":"{\"datetime\":$moment().add(15, 'seconds')}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":620,"y":400,"wires":[[]]},{"id":"40a8ce34c08c3f97","type":"inject","z":"48da3aa08f5bc110","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":400,"wires":[["8279a1421d0a85fb"]]},{"id":"8c4080587b891731","type":"ha-time","z":"48da3aa08f5bc110","name":"","server":"58bff01.29ea91","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityId":"input_datetime.charger","property":"","offset":"-6","offsetType":"num","offsetUnits":"hours","randomOffset":false,"repeatDaily":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"sunday":true,"monday":true,"tuesday":true,"wednesday":true,"thursday":true,"friday":true,"saturday":true,"debugenabled":false,"x":620,"y":460,"wires":[[]]},{"id":"58bff01.29ea91","type":"server","name":"Home Assistant","addon":true}]

I don’t need to think about timezones.
Are you sure you have set it up correctly?

No, I am not sure I have it set correctly. That is why I included my flow.

I use an inject to trigger the service call for an HA datetime helper. If I look at the logbook in HA right after injecting, it shows a time 6 hours in the future.