Skip to content

Instantly share code, notes, and snippets.

@hyukishi
Created February 12, 2021 19:46
Show Gist options
  • Select an option

  • Save hyukishi/9deaa1c5bdabdb833fce6e6301c45441 to your computer and use it in GitHub Desktop.

Select an option

Save hyukishi/9deaa1c5bdabdb833fce6e6301c45441 to your computer and use it in GitHub Desktop.
Node-RED Alexa Timers
/*
The custom Node-RED integrations from the HACS store must be installed for this to work.
You must create the timers you want to use in your configuration.yaml file for Home Assistant.
For information on creating timer entities for Home Assistant, visit https://www.home-assistant.io/integrations/timer/
This flow gets the current state of the sensor.<entityID>_next_timer and converts it into a HH:MM:SS format. If there are no hours in the timer, it only displays MM:SS.
I suggest using a filter entities card to display the timer. A regular entity card will only return a status of "active" or "idle". I have 4 timers configured so I used the filtered entities card to only show active timers.
*/
[{"id":"eab341f9.2c961","type":"group","z":"97a7aa7b.d1ab4","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["62203df0.703d4c","c3e52440.095a5","6957b1cf.2f2878","c5d2b9df.75395","5686790.84ada08","4f99b257.8ba294","a613a8d4.fc5a98","6f597882.044ab"],"x":-6,"y":-1,"w":972,"h":222},{"id":"62203df0.703d4c","type":"server-state-changed","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"timer state","server":"2a5e2ac3.7dd716","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.guest_bedroom_echo_dot_next_timer","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":80,"y":120,"wires":[["c5d2b9df.75395"]]},{"id":"c3e52440.095a5","type":"function","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"","func":"msg.timestamp = Date.parse(Date())/1000;\nmsg.timer = Date.parse(msg.data.old_state.state)/1000;\nmsg.totalSeconds = Math.floor(msg.timer - msg.timestamp);\nmsg.seconds = Math.floor(msg.totalSeconds % 60) + \"\";\nmsg.minutes = Math.floor(msg.totalSeconds / 60) % 60 + \"\";\nmsg.hours = Math.floor(msg.totalSeconds / 3600) + \"\";\n\n if (msg.totalSeconds < 60) {\n if (msg.totalSeconds < 10) {\n msg.remaining = \"00:00:0\" + msg.seconds;\n } else {\n msg.remaining = \"00:00:\" + msg.seconds;\n }\n } else if (msg.totalSeconds >= 60) {\n if (msg.minutes < 10) {\n msg.remaining = \"00:0\" + msg.minutes + \":\" + msg.seconds;\n } else {\n msg.remaining = \"00:\" + msg.minutes + \":\" + msg.seconds;\n }\n } else if (msg.totalSeconds >= 3600) {\n msg.remaining = msg.hours + \":\" + msg.minutes + \":\" + msg.seconds;\n } else {\n msg.remaining = \"00:00:00\";\n return [null,msg]\n }\nmsg.payload = msg.remaining;\n\nreturn [msg,null];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":360,"y":120,"wires":[["5686790.84ada08","4f99b257.8ba294"],["a613a8d4.fc5a98"]]},{"id":"6957b1cf.2f2878","type":"debug","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":120,"wires":[]},{"id":"c5d2b9df.75395","type":"ha-wait-until","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"","server":"2a5e2ac3.7dd716","outputs":1,"entityId":"sensor.guest_bedroom_echo_dot_next_timer","entityIdFilterType":"exact","property":"payload","comparator":"is_not","value":"unavailable","valueType":"str","timeout":0,"timeoutType":"num","timeoutUnits":"seconds","entityLocation":"payload","entityLocationType":"msg","checkCurrentState":true,"blockInputOverrides":true,"x":220,"y":120,"wires":[["c3e52440.095a5"]]},{"id":"5686790.84ada08","type":"ha-entity","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"gbr-nr-timer","server":"2a5e2ac3.7dd716","version":1,"debugenabled":true,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"gbr-nr-timer"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:clock"},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[{"property":"duration","value":"payload","valueType":"msg"}],"resend":true,"outputLocation":"payload","outputLocationType":"msg","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":510,"y":40,"wires":[[]]},{"id":"4f99b257.8ba294","type":"api-call-service","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"","server":"2a5e2ac3.7dd716","version":1,"debugenabled":false,"service_domain":"timer","service":"start","entityId":"timer.gbr","data":"{\"duration\": \"{{payload}}\",\"entity_id\":\"timer.gbr\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":680,"y":120,"wires":[["6957b1cf.2f2878"]]},{"id":"a613a8d4.fc5a98","type":"api-call-service","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"","server":"2a5e2ac3.7dd716","version":1,"debugenabled":false,"service_domain":"timer","service":"finish","entityId":"timer.gbr","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":680,"y":180,"wires":[[]]},{"id":"6f597882.044ab","type":"comment","z":"97a7aa7b.d1ab4","g":"eab341f9.2c961","name":"Guest Bedroom Timer","info":"","x":120,"y":40,"wires":[]},{"id":"2a5e2ac3.7dd716","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment