Knowing how long a switch has been on

How can I tell how long a switch has been on in nodered? The situation is that I have a pump that is switched on and off depending on temperature, however I’d like it to stay on for a minimum of a hour.

Should be as simple as using a stop timer for 1 hour.

What is the trigger you use to turn it on and off?

If the pump’s operation is driven by temperature and you want a minimum cycle-time of 1 hour, wouldn’t you need to include that requirement in the logic driving the pump? In other words, it can’t be driven purely by temperature.

For an analogy, think of a gas furnace. Even if it were reach the desired temperature setpoint within seconds of startup, it won’t simply shutdown. It has a minimum cycle-time (to avoid short-cycling) and will continue to run (albeit briefly) then shutdown.

At the moment I use a timestamp with a current state, to get the temperature followed by a switch for on/off, I need add a further check to the off to make sure it has been powered on for at least 1hr

IF temp≥80 THEN pump on
ELSEIF pump has been on > 1hr THEN pump off

I tried to do the same with NodeRed and checking how long i have been at a specific location any given date. I tried to work with the history state node… but it turns out there is a simple HA sensor for this. Its called History Statistics. With this it will show you in a defined time frame how long or how often the state has been at the specific value you want to check. Give it a try :slight_smile:

Reading more details about your use case… i think this does not do exactly do what you want it to.
You could work with a traffic node. If the Pump Turns on, you also turn on a timer with 1hr. At the same time you set the traffic node (Traffic light) to red and stop any incoming messages from the temperature to reach the turn off service call. Once the Timer reaches 0 it can turn the Traffic Light green again and allow your Temperature to turn off the pump

1 Like

Thanks, I’ve had a look at both, and I can see how traffic node would help, but I can’t figure out what to use in the regex??

You can use anything you want. The REGEX for allow will set the TrafficLight to green. The Regex in STOP will put it to red. Just send the message. If you select payload as property simply send: msg.payload = stop or allow to turn it green or red (or any value you set in the regex)

Thanks I get that, but I still need to detect how long the pump has been running.

For that you might simply check the state of the pump and check the msg.lastChanged. It usually shows the time in milliseconds, so you might need to calculate the time to a more useful value.

I couldn’t find any documention at all on msg.lastChanged, and was getting undefined errors, so I used the function node.

var now = new Date();
var changed = new Date(msg.data.last_changed);
msg.data.TimeSinceChange = (now.getTime()-changed.getTime())/1000;
return msg;

Indeed, msg.data.TimeSinceChange is the correct one. For some reason I had msg.lastChanged in my head…

I also had to code a function to calculate it. Unless I missed something.