Calculating difference between two time stamps

I’m trying to make an automation that can calculate the remaining time left on my cooker, using the estimated time stamp and deducting the current time. I’m failing miserably, I just can’t get my head around it.

so far I’ve come up with this:

[{"id":"b0647e2c628fdda4","type":"debug","z":"24f9f29f4151076e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1110,"y":1600,"wires":[]},{"id":"d1e8bc70a60ff147","type":"function","z":"24f9f29f4151076e","name":"function 1","func":"let a = new Date().getHours() + 2 //the + 2 is for time zone correction\nlet b = msg.payload.a\nlet c = a - b\n\nmsg.payload = c\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":1600,"wires":[["b0647e2c628fdda4"]]},{"id":"36719144d1b6394e","type":"api-current-state","z":"24f9f29f4151076e","name":"","server":"4e7f96b.0675968","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.top_oven_remaining_program_time","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload.a","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":1600,"wires":[["d1e8bc70a60ff147"]]},{"id":"900273c38e1b8335","type":"inject","z":"24f9f29f4151076e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":1720,"wires":[["36719144d1b6394e"]]},{"id":"4e7f96b.0675968","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I’ve been trying to use the information from http://stevesnoderedguide.com/working-with-time but I think I’m too dumb to understand it. Can anyone point me in the right direction, please?

Can you post the output from the developer tools’s state tab of your sensor?

That was what I wanted, but the values is kinda useless.
% is hard to convert to timestamps. :slight_smile:

Then again. It looks like you use the sensor:
sensor.top_oven_remaining_program_time


Sorry wrong screen shot :man_facepalming:

Try this:

[{"id":"b0647e2c628fdda4","type":"debug","z":"be03f9025a1f137a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":480,"y":1125,"wires":[]},{"id":"d1e8bc70a60ff147","type":"function","z":"be03f9025a1f137a","name":"function 1","func":"msg.remain = new Date(global.get('homeassistant').homeAssistant.states[\"sensor.top_oven_remaining_program_time\"].state).valueOf();\nmsg.timestamp = msg.payload - msg.remain;\nmsg.milliseconds=msg.timestamp%1000;\nmsg.seconds = parseInt((msg.timestamp/1000))%60 ;\nmsg.minutes = parseInt((msg.timestamp/60000))%60;\nmsg.hours = parseInt((msg.timestamp/3600000)) % 60;\nmsg.days = parseInt((msg.timestamp / 86400000)) % 24;\nmsg.timestring = msg.days+\"D \"\nif (msg.hours<10) {\n    msg.timestring = msg.timestring+\"0\";\n}\nmsg.timestring = msg.timestring+msg.hours+\":\"\nif (msg.minutes < 10) {\n    msg.timestring = msg.timestring + \"0\";\n}\nmsg.timestring = msg.timestring + msg.minutes + \":\"\nif (msg.seconds < 10) {\n    msg.timestring = msg.timestring + \"0\";\n}\nmsg.timestring = msg.timestring + msg.seconds;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":1125,"wires":[["b0647e2c628fdda4"]]},{"id":"900273c38e1b8335","type":"inject","z":"be03f9025a1f137a","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":145,"y":1125,"wires":[["d1e8bc70a60ff147"]]}]
1 Like

Thank you much appreciated

Annoyingly after all that it seems like the trigger I intended to use is unreliable. I don’t really want to use a poll state node but I’m not too sure how else I can pull the data reliably.