Files
HA-NerdFlows-Functions/laundry/timer-change.js
2023-09-28 12:28:27 -04:00

40 lines
1001 B
JavaScript

const states = global.get('homeassistant.homeAssistant.states')
const oldState = msg.data.old_state.state
const newState = msg.data.new_state.state
const entity_id = msg.data.entity_id
const washerCycle = flow.get("washerCycle", "diskCon")
const dryerCycle = flow.get("dryerCycle", "diskCon")
const subtract = newState - oldState
const multiply = subtract * 60
const diff = parseInt(multiply)
let machine = {}
let timer = {}
if (entity_id === washerCycle) {
machine = 'washer'
} else if (entity_id === dryerCycle) {
machine = 'dryer'
}
if (machine === 'washer') {
timer = flow.get("washerTimer", "diskCon")
} else if (machine === 'dryer') {
timer = flow.get("dryerTimer", "diskCon")
}
// @ts-ignore
let running = states[timer].state
let changeMsg = {
"timer": timer,
"change": diff
}
if (running === 'active') {
node.send(changeMsg)
node.status({fill:"green",shape:"dot",text:"Timer Changed"})
} else {
node.status({fill:"red",shape:"ring",text:"Blocked"})
}