Skip to content

Commit

Permalink
feat(event-state): Added Only output on state change
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Jan 24, 2019
1 parent f1a9ba7 commit 0707d72
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
8 changes: 7 additions & 1 deletion nodes/events-state-changed/events-state-changed.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
haltifstate: { value: "" },
halt_if_type: {},
halt_if_compare: {},
outputs: { value: 1 }
outputs: { value: 1 },
output_only_on_state_change: { value: true }
},
inputs: 0,
outputs: 1,
Expand Down Expand Up @@ -162,6 +163,11 @@
</select>
</div>

<div class="form-row">
<input type="checkbox" id="node-input-output_only_on_state_change" style="display: inline-block; width: auto; vertical-align: top;margin-left: 105px;">
<label for="node-input-output_only_on_state_change" style="width: auto;">Output only on state change</label>
</div>

<div class="form-row">
<input type="checkbox" id="node-input-outputinitially" style="display: inline-block; width: auto; vertical-align: top;margin-left: 105px;">
<label for="node-input-outputinitially" style="width: auto;">Output Initially / On Deploy</label>
Expand Down
111 changes: 57 additions & 54 deletions nodes/events-state-changed/events-state-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = function(RED) {
halt_if_type: {},
halt_if_compare: {},
outputinitially: {},
state_type: {}
state_type: {},
output_only_on_state_change: {}
}
};

Expand Down Expand Up @@ -79,69 +80,71 @@ module.exports = function(RED) {
);
}

const shouldIncludeEvent = this.shouldIncludeEvent(entity_id);
if (!this.shouldIncludeEvent(entity_id)) {
return null;
}

if (shouldIncludeEvent) {
this.nodeConfig.halt_if_compare =
this.nodeConfig.halt_if_compare || 'is';
this.nodeConfig.halt_if_type =
this.nodeConfig.halt_if_type || 'str';
if (
this.nodeConfig.output_only_on_state_change === true &&
event.old_state.state === event.new_state.state
) {
return null;
}

const isHaltValid = await this.getComparatorResult(
this.nodeConfig.halt_if_compare,
this.nodeConfig.haltIfState,
event.new_state.state,
this.nodeConfig.halt_if_type
);
const shouldHaltIfState =
this.nodeConfig.haltIfState && isHaltValid;

const msg = {
topic: entity_id,
payload: event.new_state.state,
data: event
};

if (shouldHaltIfState) {
this.debug(
'flow halted due to "halt if state" setting'
);
this.status({
fill: 'red',
shape: 'ring',
text: `${
event.new_state.state
} at: ${this.getPrettyDate()}`
});

return this.send([null, msg]);
}
this.nodeConfig.halt_if_compare =
this.nodeConfig.halt_if_compare || 'is';
this.nodeConfig.halt_if_type =
this.nodeConfig.halt_if_type || 'str';

const isHaltValid = await this.getComparatorResult(
this.nodeConfig.halt_if_compare,
this.nodeConfig.haltIfState,
event.new_state.state,
this.nodeConfig.halt_if_type
);
const shouldHaltIfState =
this.nodeConfig.haltIfState && isHaltValid;

const msg = {
topic: entity_id,
payload: event.new_state.state,
data: event
};

if (shouldHaltIfState) {
this.debug('flow halted due to "halt if state" setting');
this.status({
fill: 'green',
shape: 'dot',
fill: 'red',
shape: 'ring',
text: `${
event.new_state.state
} at: ${this.getPrettyDate()}`
});

event.old_state
? this.debug(
`Incoming state event: entity_id: ${
event.entity_id
}, new_state: ${
event.new_state.state
}, old_state: ${event.old_state.state}`
)
: this.debug(
`Incoming state event: entity_id: ${
event.entity_id
}, new_state: ${event.new_state.state}`
);

return this.send([msg, null]);
return this.send([null, msg]);
}
return null;

this.status({
fill: 'green',
shape: 'dot',
text: `${event.new_state.state} at: ${this.getPrettyDate()}`
});

event.old_state
? this.debug(
`Incoming state event: entity_id: ${
event.entity_id
}, new_state: ${event.new_state.state}, old_state: ${
event.old_state.state
}`
)
: this.debug(
`Incoming state event: entity_id: ${
event.entity_id
}, new_state: ${event.new_state.state}`
);

return this.send([msg, null]);
} catch (e) {
this.error(e);
}
Expand Down

0 comments on commit 0707d72

Please sign in to comment.