d_sellers1

Time Changed Since Midnight Checker

Jun 10th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. entity_to_check = "binary_sensor.bathroom_door"
  2.  
  3. //calculates time in ms since midnight
  4. var now = new Date(),
  5.     then = new Date(
  6.         now.getFullYear(),
  7.         now.getMonth(),
  8.         now.getDate(),
  9.         0,0,0),
  10.     time_since_midnight = now.getTime() - then.getTime();
  11.  
  12. //gets state for entity from Home Assistant
  13. const d = global.get('homeassistant').homeAssistant
  14. var changed = d.states[entity_to_check].last_changed
  15.  
  16. //calculates time in ms since midnight from time entity last changed
  17. time_since_changed = now.getTime() - new Date(changed).getTime();
  18.  
  19. //if the value of midnight is greater than the value of time since
  20. //changed, //then the door has been opened and the lock needs to
  21. //be checked
  22. if ((time_since_midnight - time_since_changed) > 1) {
  23.     node.status({fill:"red",shape:"dot",text:"Check lock!"});
  24.     msg.payload = true
  25. } else {
  26.     node.status({fill:"green",shape:"ring",text:"Door okay"});
  27.     msg.payload = false
  28. }
  29. return msg;
Advertisement
Add Comment
Please, Sign In to add comment