Skip to content

Commit

Permalink
UI Switch - Handle on/off values that are Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
joepavitt committed Jan 5, 2024
1 parent 34faf64 commit d41bf58
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
24 changes: 18 additions & 6 deletions nodes/widgets/ui_switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,25 @@ module.exports = function (RED) {
// retrieve the assigned on/off value
const on = RED.util.evaluateNodeProperty(config.onvalue, config.onvalueType, wNode)
const off = RED.util.evaluateNodeProperty(config.offvalue, config.offvalueType, wNode)
if (msg.payload === true || msg.payload === on) {
msg.payload = on
} else if (msg.payload === false || msg.payload === off) {
msg.payload = off

if (typeof msg.payload === 'object') {
if (JSON.stringify(msg.payload) === JSON.stringify(on)) {
msg.payload = on
} else if (JSON.stringify(msg.payload) === JSON.stringify(off)) {
msg.payload = off
} else {
// throw Node-RED error
error = 'Invalid payload value'
}
} else {
// throw Node-RED error
error = 'Invalid payload value'
if (msg.payload === true || msg.payload === on) {
msg.payload = on
} else if (msg.payload === false || msg.payload === off) {
msg.payload = off
} else {
// throw Node-RED error
error = 'Invalid payload value'
}
}
if (!error) {
// store the latest msg passed to node
Expand Down
7 changes: 7 additions & 0 deletions ui/src/widgets/ui-switch/UISwitch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export default {
const val = this.value
if (typeof (val) === 'boolean') {
return val
} else if (typeof (val) === 'object') {
// don't make a decision either way, unless it matches, exactly, the defined on/off values
if (JSON.stringify(val) === JSON.stringify(this.props.evaluated.on)) {
return true
} else if (JSON.stringify(val) === JSON.stringify(this.props.evaluated.off)) {
return false
}
} else if (this.props.evaluated) {
return val === this.props.evaluated.on
}
Expand Down

0 comments on commit d41bf58

Please sign in to comment.