Skip to content

Commit

Permalink
add support to send messages on enable/disable
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzix committed May 12, 2022
1 parent 5b061c1 commit 86c3036
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "thingzi-logic-timers",
"version": "1.2.3",
"version": "1.2.4",
"description": "Easy to use time based nodes with clean design",
"bundleDependencies": false,
"dependencies": {
Expand Down
11 changes: 9 additions & 2 deletions node/schedule.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
onPayload: { value: "ON", validate: RED.validators.typedInput("onPayloadType") },
offPayloadType: { value: "str" },
offPayload: { value: "OFF", validate: RED.validators.typedInput("offPayloadType") },
startupMessage: { value: true }
startupMessage: { value: true },
sendOnEnable: { value: true }
},
inputs: 1,
outputs: 1,
Expand Down Expand Up @@ -228,7 +229,13 @@ <h4>Output</h4>
<div class="form-row">
<label style="margin-left: 100px; width: 70%">
<input type="checkbox" id="node-input-startupMessage" style="width: 20px; margin: 0">
<span id="node-input-startupMessageText">Send previous event on startup</span>
<span>Send previous event on startup</span>
</label>
</div>
<div class="form-row">
<label style="margin-left: 100px; width: 70%">
<input type="checkbox" id="node-input-sendOnEnable" style="width: 20px; margin: 0">
<span>Always send event on enable/disable</span>
</label>
</div>
</script>
Expand Down
10 changes: 8 additions & 2 deletions node/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = function (RED) {
this.scheduleEnabled = true;
this.state = null;
this.ignoreFirstSend = !config.startupMessage;
this.sendOnEnable = config.sendOnEnable;

// Get the current state
this.getState = function () {
Expand Down Expand Up @@ -97,16 +98,21 @@ module.exports = function (RED) {
};

// Check for new state
this.update = function() {
this.update = function(alwaysSend = false) {
var newState = node.scheduleEnabled ? node.getState() : false;
var sendMessage = alwaysSend;

// Has the state changed
if (newState != node.state) {
if (node.state) {
node.log(`Change state from ${node.state} to ${newState}`);
}
node.state = newState;
sendMessage = true;
}

// Message to send?
if (sendMessage) {
// Get the message payload
if (node.state) {
node.sendMessage(node.getValue(node.onPayloadType, node.onPayload));
Expand All @@ -128,7 +134,7 @@ module.exports = function (RED) {
var value = msg.hasOwnProperty('payload') ? msg.payload.toString() : null;
if (value) {
node.scheduleEnabled = value === 'ON';
node.update();
node.update(node.sendOnEnable);
}
done();
});
Expand Down

0 comments on commit 86c3036

Please sign in to comment.