Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- entity_to_check = "binary_sensor.bathroom_door"
- //calculates time in ms since midnight
- var now = new Date(),
- then = new Date(
- now.getFullYear(),
- now.getMonth(),
- now.getDate(),
- 0,0,0),
- time_since_midnight = now.getTime() - then.getTime();
- //gets state for entity from Home Assistant
- const d = global.get('homeassistant').homeAssistant
- var changed = d.states[entity_to_check].last_changed
- //calculates time in ms since midnight from time entity last changed
- time_since_changed = now.getTime() - new Date(changed).getTime();
- //if the value of midnight is greater than the value of time since
- //changed, //then the door has been opened and the lock needs to
- //be checked
- if ((time_since_midnight - time_since_changed) > 1) {
- node.status({fill:"red",shape:"dot",text:"Check lock!"});
- msg.payload = true
- } else {
- node.status({fill:"green",shape:"ring",text:"Door okay"});
- msg.payload = false
- }
- return msg;
Advertisement
Add Comment
Please, Sign In to add comment