@Kermit - Thank you for the help and information! I have lots to learn and it is of great help to be pointed in the right direction.
My project has evolved a bit and my current difficulties are likely due to me not fully understanding the innerworkings of time / date in JavaScript.
This is the latest version of the configuration “panel” for the kids morning wake up routine. In a different tab I have input.datetime entities holding the alarm time for week days and weekends. The “Kids Wake-Up Time” alarm time must match whether the next day (if we are looking at this before midnight) is a school day or not. I used the workday integration for this and have two sensors setup for today and tomorrow.
The “Next Alarm” filed should reflect when the alarm being set applies. Displaying it is not necessary but I use this logic to determine what the default wake up time is so why not show it too. The logic is that if now() is after the alarm time (default or adjusted depending on toggle) then it should show “Tomorrow” as long as it is before midnight. Between midnight and the alarm time (default or adjusted depending on toggle), then it should show “Today”.
Taken at 10am:
Delay is off, so since the applicable alarm time is in the past, it correctly shows tomorrow along with using tomorrow’s correct wake up time (8.30 temporarily later than normal for testing).
If I turn on the delay, the “Kids Wake-Up Time” updates to the weekend default time (8.45 temporarily later than normal for testing) and since the delay is set to 120 minutes, the adjusted alarm time is 10.45. Since it is now 10am, the Next Alarm is set to Today.
As soon as the alarm goes off, the default alarm time should revert to 8.30, and Next Alarm to Tomorrow (Monday).
For the most part this works, but while trying to figure this out, I’ve seen the Today/Tomorrow not update when desired so there is something wrong with how I look at dates and compare them. I am neck deep in web pages discussing time, and date processing in JavaScript but I can’t seem to find how to just look at time discarding date. I suspect the date changing as time passes is screwing this up (due to bad logic…).
[{"id":"28923cd8f7c8e9cc","type":"api-current-state","z":"44181540ccea04aa","name":"Delay Kids Wake-up","server":"6f1799ba.17e938","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.delay_kids_wake_up","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"delay","propertyType":"msg","value":"","valueType":"entityState"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1300,"y":1160,"wires":[["277c799bcca86f83"]]},{"id":"508d3f260ea7a173","type":"server-state-changed","z":"44181540ccea04aa","name":"Wake Up Delay (min)","server":"6f1799ba.17e938","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_select.wake_up_delay","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":220,"y":1100,"wires":[["bb061668327dd2f1"]]},{"id":"277c799bcca86f83","type":"api-current-state","z":"44181540ccea04aa","name":"Wake Up Delay (min)","server":"6f1799ba.17e938","version":2,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","entity_id":"input_select.wake_up_delay","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1540,"y":1160,"wires":[["40919cf2dffe40cf"]]},{"id":"3b055b430a9bd905","type":"api-call-service","z":"44181540ccea04aa","name":"Adjusted Kids Wake-Up Time","server":"6f1799ba.17e938","version":3,"debugenabled":false,"service_domain":"input_datetime","service":"set_datetime","entityId":"input_datetime.adjusted_kids_wake_up_time","data":"{\"time\": payload}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1560,"y":1080,"wires":[[]]},{"id":"40919cf2dffe40cf","type":"function","z":"44181540ccea04aa","name":"time","func":"const delay = msg.delay == \"on\" ? msg.payload * 60 : 0;\nconst alarm = (msg.alarm + delay) * 1000;\nconst adjusted = new Date(alarm);\n\nmsg.payload = adjusted.toISOString().split('T')[1].split('.')[0];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1730,"y":1160,"wires":[["3b055b430a9bd905"]]},{"id":"d0eb2e034631d560","type":"api-current-state","z":"44181540ccea04aa","name":"Awake Mode Week Start Kids","server":"6f1799ba.17e938","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_datetime.awake_mode_week_start_kids","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"alarm","propertyType":"msg","value":"$entity().attributes.timestamp","valueType":"jsonata"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":970,"y":1160,"wires":[["28923cd8f7c8e9cc","67382ca028721bfc"]]},{"id":"1e985f61d47f2c03","type":"server-state-changed","z":"44181540ccea04aa","name":"Awake Mode - Week - Start (Kids)","server":"6f1799ba.17e938","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_datetime.awake_mode_week_start_kids","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":180,"y":980,"wires":[["bb061668327dd2f1"]]},{"id":"49f885c82e3b9a38","type":"server-state-changed","z":"44181540ccea04aa","name":"Delay Kids Wake-up","server":"6f1799ba.17e938","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.delay_kids_wake_up","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":230,"y":1160,"wires":[["bb061668327dd2f1"]]},{"id":"1181df040a902c4a","type":"api-current-state","z":"44181540ccea04aa","name":"Awake Mode Weekend Start Kids","server":"6f1799ba.17e938","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_datetime.awake_mode_weekend_start_kids","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"alarm","propertyType":"msg","value":"$entity().attributes.timestamp","valueType":"jsonata"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":980,"y":1240,"wires":[["28923cd8f7c8e9cc","67382ca028721bfc"]]},{"id":"ad3bcaffebd2fc38","type":"ha-time","z":"44181540ccea04aa","name":"Adjusted Kids Wake-Up Time","server":"6f1799ba.17e938","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityId":"input_datetime.adjusted_kids_wake_up_time","property":"","offset":0,"offsetType":"num","offsetUnits":"minutes","randomOffset":false,"repeatDaily":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"sunday":true,"monday":true,"tuesday":true,"wednesday":true,"thursday":true,"friday":true,"saturday":true,"debugenabled":false,"x":200,"y":1360,"wires":[["0ef95a7847b288db","eee677ab4511eafa","bb061668327dd2f1"]]},{"id":"30e591830692a445","type":"server-state-changed","z":"44181540ccea04aa","name":"Awake Mode - Weekend - Start (Kids)","server":"6f1799ba.17e938","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_datetime.awake_mode_weekend_start_kids","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":170,"y":920,"wires":[["bb061668327dd2f1"]]},{"id":"6cf7326c0f33bc2d","type":"api-call-service","z":"44181540ccea04aa","name":"Next Wake-Up Alarm (Kids)","server":"6f1799ba.17e938","version":3,"debugenabled":false,"service_domain":"input_datetime","service":"set_datetime","entityId":"input_datetime.next_wake_up_alarm_kids","data":"{\"time\": payload}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1460,"y":1240,"wires":[[]]},{"id":"67382ca028721bfc","type":"function","z":"44181540ccea04aa","name":"time","func":"const alarm = msg.alarm * 1000;\nconst adjusted = new Date(alarm);\n\nmsg.payload = adjusted.toISOString().split('T')[1].split('.')[0];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1250,"y":1240,"wires":[["6cf7326c0f33bc2d"]]},{"id":"0ef95a7847b288db","type":"api-call-service","z":"44181540ccea04aa","name":"Kids Mode - Awake","server":"6f1799ba.17e938","version":3,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.kids_mode","data":"{\"option\":\"Awake\"}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":550,"y":1360,"wires":[[]]},{"id":"68d015c35725b1a5","type":"function","z":"44181540ccea04aa","name":"Compare Times","func":"var adjAlarm = msg.adjAlarm\nvar adjAlarmTS = (new Date(adjAlarm)).getTime();\nvar Alarm = adjAlarm.toLocaleString('en-US', {hour12: false});\n\nvar lastMidnightUTC = new Date();\nlastMidnightUTC.setHours(0,0,0,0);\nvar lastMidnight = lastMidnightUTC.toLocaleString('en-US', {hour12: false});\nvar lastMidnightTS = lastMidnightUTC.getTime();\n\nvar nextMidnightUTC = new Date();\nnextMidnightUTC.setHours(24,0,0,0);\nvar nextMidnight = nextMidnightUTC.toLocaleString('en-US', {hour12: false});\nvar nextMidnightTS = nextMidnightUTC.getTime();\n\n/*\nvar testvar = new Date();\nvar testvar2 = testvar.getTime();\n*/\n\nvar nowUTC = new Date();\nvar now = nowUTC.toLocaleString('en-US', {hour12: false});\nvar nowTS = nowUTC.getTime();\n\n\n/*\nnearest midnight in the past:\nvar d = new Date();\nd.setHours(0,0,0,0); // last midnight\n\nnearest midnight in future:\nvar d = new Date();\nd.setHours(24,0,0,0); // next midnight\n*/\n\n/*\nvar d=new Date();\nvar currentTime2=d.getTime()\n\nd=new Date();\ntime2=d.getTime()\n*/\n\nif(nowTS >= lastMidnightTS && nowTS <= adjAlarmTS) {\n msg.payload = \"Today\";\n} else {\n msg.payload = \"Tomorrow\";\n}\n\nvar debugMSG = {payload: msg.payload, nowTS, now, lastMidnightTS, lastMidnight, nextMidnightTS, nextMidnight, adjAlarmTS, Alarm};\n\nreturn debugMSG;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":1000,"wires":[["dcce2857ebb3edfa","6903a374149c7e92"]]},{"id":"dcce2857ebb3edfa","type":"switch","z":"44181540ccea04aa","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Today","vt":"str"},{"t":"eq","v":"Tomorrow","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":730,"y":1000,"wires":[["c0cf7572ff286da7"],["9cf435f4548a6b1f"]]},{"id":"c0cf7572ff286da7","type":"api-call-service","z":"44181540ccea04aa","name":"Next Wake-Up Alarm Kids - Today","server":"6f1799ba.17e938","version":3,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.next_wake_up_alarm_kids","data":"{\"option\":\"Today\"}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1000,"y":980,"wires":[["c247a3a6747436de"]]},{"id":"9cf435f4548a6b1f","type":"api-call-service","z":"44181540ccea04aa","name":"Next Wake-Up Alarm Kids - Tomorrow","server":"6f1799ba.17e938","version":3,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.next_wake_up_alarm_kids","data":"{\"option\":\"Tomorrow\"}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1010,"y":1040,"wires":[["4a4073c5b8678d13"]]},{"id":"6903a374149c7e92","type":"debug","z":"44181540ccea04aa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":1060,"wires":[]},{"id":"405e4133fe0e5843","type":"server-state-changed","z":"44181540ccea04aa","name":"Adjusted Wake-Up Alarm (Kids)","server":"6f1799ba.17e938","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_datetime.adjusted_kids_wake_up_time","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"true","valueType":"bool"}],"x":190,"y":1040,"wires":[["bb061668327dd2f1"]]},{"id":"7f7284cea7358d6f","type":"inject","z":"44181540ccea04aa","name":"Midnight","props":[{"p":"midnight","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"01 00 * * *","once":false,"onceDelay":0.1,"topic":"","x":260,"y":1220,"wires":[["bb061668327dd2f1"]]},{"id":"bb061668327dd2f1","type":"api-current-state","z":"44181540ccea04aa","name":"Adjusted Wake-Up Alarm (Kids)","server":"6f1799ba.17e938","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_datetime.adjusted_kids_wake_up_time","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"adjAlarm","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"x":570,"y":920,"wires":[["0f1c8112efe97a3c"]]},{"id":"55309cb8134b91ec","type":"api-call-service","z":"44181540ccea04aa","name":"Delay Kids Wake-up Auto Off >> Off","server":"6f1799ba.17e938","version":3,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.delay_kids_wake_up_auto_off","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":940,"y":1500,"wires":[[]]},{"id":"03cbb8ad0dab4933","type":"api-call-service","z":"44181540ccea04aa","name":"Delay Kids Wake-up >> Off","server":"6f1799ba.17e938","version":3,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.delay_kids_wake_up","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":920,"y":1440,"wires":[[]]},{"id":"c247a3a6747436de","type":"api-current-state","z":"44181540ccea04aa","name":"Workday Today?","server":"6f1799ba.17e938","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.workday_today","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":550,"y":1160,"wires":[["d0eb2e034631d560"],["1181df040a902c4a"]]},{"id":"4a4073c5b8678d13","type":"api-current-state","z":"44181540ccea04aa","name":"Workday Tomorrow?","server":"6f1799ba.17e938","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.workday_tomorrow","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":560,"y":1240,"wires":[["d0eb2e034631d560"],["1181df040a902c4a"]]},{"id":"0f1c8112efe97a3c","type":"moment","z":"44181540ccea04aa","name":"format time","topic":"","input":"adjAlarm","inputType":"msg","inTz":"America/Chicago","adjAmount":0,"adjType":"days","adjDir":"add","format":"date","locale":"en-US","output":"adjAlarm","outputType":"msg","outTz":"America/Chicago","x":810,"y":920,"wires":[["68d015c35725b1a5"]]},{"id":"eee677ab4511eafa","type":"api-current-state","z":"44181540ccea04aa","name":"Delay Kids Wake-up Auto Off - On?","server":"6f1799ba.17e938","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.delay_kids_wake_up_auto_off","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":600,"y":1440,"wires":[["03cbb8ad0dab4933","55309cb8134b91ec"],[]]},{"id":"6f1799ba.17e938","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
In configuration.yaml I have this:
- sensor:
- name: "Kids Wake-Up Time"
icon: "hass:clock-outline"
state: "{{ state_attr('input_datetime.next_wake_up_alarm_kids', 'timestamp') | timestamp_custom('%H:%M', False, 0) }}"
- sensor:
- name: "Adjusted Kids Wake-Up Time"
icon: "hass:clock-end"
state: "{{ state_attr('input_datetime.adjusted_kids_wake_up_time', 'timestamp') | timestamp_custom('%H:%M', False, 0) }}"
- sensor:
- name: "Next Alarm"
icon: "hass:alarm"
state: >
{% if is_state("input_select.next_wake_up_alarm_kids", "Today") %}
Today
{%- elif is_state("input_select.next_wake_up_alarm_kids", "Tomorrow") %}
Tomorrow
{% else %}
Undefined
{% endif %}
and
#Workday integration for today and tomorrow
#https://www.home-assistant.io/integrations/workday/
binary_sensor:
- platform: workday
name: workday_today
country: US
workdays: [mon, tue, wed, thu, fri]
excludes: [sat, sun]
add_holidays:
- "2021-11-25"
- "2021-12-24"
- "2021-12-25"
- "2021-12-31"
- "2022-01-01"
- platform: workday
name: workday_tomorrow
days_offset: 1
country: US
workdays: [mon, tue, wed, thu, fri]
excludes: [sat, sun]
add_holidays:
- "2021-11-25"
- "2021-12-24"
- "2021-12-25"
- "2021-12-31"
- "2022-01-01"
This morning I was testing it as last night it was not working. After minor changes it appears to work but I suspect it will break again when dates change. This is the debug output (I have timestamp and human readable just to make it easier to read):
1/14/2021, 9:16:08 AMnode: 6903a374149c7e92
msg : Object
{ payload: "Today", nowTS: 1636902968301, now: "11/14/2021, 09:16:08", lastMidnightTS: 1636869600000, lastMidnight: "11/14/2021, 24:00:00" … }
11/14/2021, 9:16:08 AMnode: 6903a374149c7e92
msg : Object
{ payload: "Tomorrow", nowTS: 1636902968450, now: "11/14/2021, 09:16:08", lastMidnightTS: 1636869600000, lastMidnight: "11/14/2021, 24:00:00" … }
11/14/2021, 9:16:08 AMnode: 6903a374149c7e92
msg : Object
{ payload: "Tomorrow", nowTS: 1636902968605, now: "11/14/2021, 09:16:08", lastMidnightTS: 1636869600000, lastMidnight: "11/14/2021, 24:00:00" … }
11/14/2021, 9:16:09 AMnode: 6903a374149c7e92
msg : Object
{ payload: "Tomorrow", nowTS: 1636902969661, now: "11/14/2021, 09:16:09", lastMidnightTS: 1636869600000, lastMidnight: "11/14/2021, 24:00:00" … }
11/14/2021, 9:16:09 AMnode: 6903a374149c7e92
msg : Object
{ payload: "Today", nowTS: 1636902969794, now: "11/14/2021, 09:16:09", lastMidnightTS: 1636869600000, lastMidnight: "11/14/2021, 24:00:00" … }
11/14/2021, 9:16:10 AMnode: 6903a374149c7e92
msg : Object
{ payload: "Today", nowTS: 1636902969955, now: "11/14/2021, 09:16:09", lastMidnightTS: 1636869600000, lastMidnight: "11/14/2021, 24:00:00" … }
The Node Red function that outputs this, and where I believe I have the issue is:
var adjAlarm = msg.adjAlarm
var adjAlarmTS = (new Date(adjAlarm)).getTime();
var Alarm = adjAlarm.toLocaleString('en-US', {hour12: false});
var lastMidnightUTC = new Date();
lastMidnightUTC.setHours(0,0,0,0);
var lastMidnight = lastMidnightUTC.toLocaleString('en-US', {hour12: false});
var lastMidnightTS = lastMidnightUTC.getTime();
var nextMidnightUTC = new Date();
nextMidnightUTC.setHours(24,0,0,0);
var nextMidnight = nextMidnightUTC.toLocaleString('en-US', {hour12: false});
var nextMidnightTS = nextMidnightUTC.getTime();
/*
var testvar = new Date();
var testvar2 = testvar.getTime();
*/
var nowUTC = new Date();
var now = nowUTC.toLocaleString('en-US', {hour12: false});
var nowTS = nowUTC.getTime();
/*
nearest midnight in the past:
var d = new Date();
d.setHours(0,0,0,0); // last midnight
nearest midnight in future:
var d = new Date();
d.setHours(24,0,0,0); // next midnight
*/
/*
var d=new Date();
var currentTime2=d.getTime()
d=new Date();
time2=d.getTime()
*/
if(nowTS >= lastMidnightTS && nowTS <= adjAlarmTS) {
msg.payload = "Today";
} else {
msg.payload = "Tomorrow";
}
var debugMSG = {payload: msg.payload, nowTS, now, lastMidnightTS, lastMidnight, nextMidnightTS, nextMidnight, adjAlarmTS, Alarm};
return debugMSG;
A lot of the code above is just part of my testing / attempts and not necessary.
I have been trying to get rid of all dates as I believe my issues are due to having to handle them. Unless my logic is incorrect, all I really care for is to know whether now() is between midnight and alarm time which means that the alarm is “Today” and now() is after alarm time and therefore before the next midnight, then it should reflect “Tomorrow”.
If I sound confused it is because I am even though it sounds simple in my head…
BONUS: If I can get rid of the time formatting node that converts the time only input.datetime to a timestamp (or whatever I should be using), I would be happier as that way all the time/date handling is in the same place (function node).
Sorry for the super long post. The goal here is not only to make it easier for those willing to help me, but also to help others looking for information on home to do something similar. Once I get this working, I will share. Most of the posts I read only have small portions so trying to copy what they do it very hard…
EDIT: I keep feeling this whole thing would result in a loop given the number of things that affect eachother. I’ll try to give scenarios to clarify.
Scenarios:
Default week alarm: 08:00
Default Weekend alarm: 09:00
now() is 10:00 Sunday
Delay (120m) is OFF
Next Alarm: Tomorrow (current time is after 08:00)
Default Alarm: 08:00 (tomorrow is Monday…)
Adjusted Alarm: 08:00 (Next Alarm is Tomorrow since it is 10:00)
Delay (120m) is ON
Next Alarm: Today
Default Alarm: 09:00 (today is Sunday…)
Adjusted Alarm: 11:00 (Next Alarm is Today since it is 10:00)