How to stop a node being triggered on deploy?

Hi,

I have a function with a basic counter that should count each time a door is opened. The door has a binary sensor so the counter only counts to 2 and then resets to 0.

The problem I have is the counter increments by 1 when the flow is deployed, even though the door isn’t opened (or closed).

I have a reset function that triggers when the sun goes down but it doesn’t seem to be changing flow.get("door_state_counter").

The script is in the “On Message” tab, but from the node.warn("Message: " + stringy) line, it dumps a lot of messages on deployment.

node.warn("Door state counter: " + flow.get("door_state_counter"));

var stringy = new String
stringy = JSON.stringify(msg)

node.warn("Message: " + stringy)


var counter = flow.get("door_state_counter") || 0

counter ++

flow.set("door_state_counter", counter)

// convert counter output for 'join' node.
msg.topic = "counter";
msg.payload = counter;

return msg;

Any ideas and thanks in advance.

W.

It is probably something with the state changing from unknown/unavailable to your checked states.
You will probably have to look into it at some time, but a quick fix would be to just add another state, ie instead of 0,1,2 then 0,1,2,3, where 0 is just for start up and where increment over 2 earlier reset to 0, then it just do it to 1 now.