Subtract 5minuts from a time out of a state node

Hello,

This is my first post on this forum. I'm rather new to nodered and looked at severall youtube tutorials, but can't find the answer on my problem.

I'm creating a flow in what has to do the following:

  • When i change my alarm on my mobile it has to get the wakeup time;
  • After this it has to subtract 5minuts of the set time;
  • On the time minus 5minuts i want to have my lights go on, on 40%;
  • On the set time the lights go brighter on 70%.

I catch the new alarm time with a state node, and it looks like:

{"topic":"sensor.sm_g920f_volgende_alarm","payload":"2021-03-06T04:10:00.000Z","data":{"entity_id":"sensor.sm_g920f_volgende_alarm","old_state":{"entity_id":"sensor.sm_g920f_volgende_alarm","state":"unknown","attributes":{"friendly_name":"SM-G920F Volgende alarm","icon":"mdi:alarm","device_class":"timestamp"},"last_changed":"2021-03-05T18:51:55.255378+00:00","last_updated":"2021-03-05T18:51:55.255378+00:00","context":{"id":"b47d7b96fae6d771440480089578579a","parent_id":null,"user_id":null},"original_state":"unknown"},"new_state":{"entity_id":"sensor.sm_g920f_volgende_alarm","state":"2021-03-06T04:10:00.000Z","attributes":{"Local Time":"Sat Mar 06 05:10:00 GMT+01:00 2021","Package":"com.sec.android.app.clockpackage","Time in Milliseconds":1615003800000,"friendly_name":"SM-G920F Volgende alarm","icon":"mdi:alarm","device_class":"timestamp"},"last_changed":"2021-03-05T21:12:38.733236+00:00","last_updated":"2021-03-05T21:12:38.733236+00:00","context":{"id":"4791f4daa44c22405ea6143da5e9e78c","parent_id":null,"user_id":null},"original_state":"2021-03-06T04:10:00.000Z","timeSinceChangedMs":16}},"_msgid":"6b4035b8.08086c"}

How can i use the newly set time ("attributes":{"Local Time":"Sat Mar 06 05:10:00 GMT+01:00 2021") and subtract the 5 minuts of of it, to turn the light on on the subtracted time?
Hoping someone could give me some plain and simpel examples.

Thanks in advance

Here is a demo showing you how to mess about with date/time in JavaScript (look in the function node)...

image

the flow...

[{"id":"2ae8a960.ed9c36","type":"inject","z":"2680e2cc.e6dace","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"topic\":\"sensor.sm_g920f_volgende_alarm\",\"payload\":\"2021-03-06T04:10:00.000Z\",\"data\":{\"entity_id\":\"sensor.sm_g920f_volgende_alarm\",\"old_state\":{\"entity_id\":\"sensor.sm_g920f_volgende_alarm\",\"state\":\"unknown\",\"attributes\":{\"friendly_name\":\"SM-G920F Volgende alarm\",\"icon\":\"mdi:alarm\",\"device_class\":\"timestamp\"},\"last_changed\":\"2021-03-05T18:51:55.255378+00:00\",\"last_updated\":\"2021-03-05T18:51:55.255378+00:00\",\"context\":{\"id\":\"b47d7b96fae6d771440480089578579a\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unknown\"},\"new_state\":{\"entity_id\":\"sensor.sm_g920f_volgende_alarm\",\"state\":\"2021-03-06T04:10:00.000Z\",\"attributes\":{\"Local Time\":\"Sat Mar 06 05:10:00 GMT+01:00 2021\",\"Package\":\"com.sec.android.app.clockpackage\",\"Time in Milliseconds\":1615003800000,\"friendly_name\":\"SM-G920F Volgende alarm\",\"icon\":\"mdi:alarm\",\"device_class\":\"timestamp\"},\"last_changed\":\"2021-03-05T21:12:38.733236+00:00\",\"last_updated\":\"2021-03-05T21:12:38.733236+00:00\",\"context\":{\"id\":\"4791f4daa44c22405ea6143da5e9e78c\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"2021-03-06T04:10:00.000Z\",\"timeSinceChangedMs\":16}},\"_msgid\":\"6b4035b8.08086c\"}","payloadType":"json","x":690,"y":620,"wires":[["fdbcdff.c59dc2"]]},{"id":"fdbcdff.c59dc2","type":"function","z":"2680e2cc.e6dace","name":"","func":"//msg.payload\nvar fiveMinsAsMS = (60*1000)*5;\nvar AlarmSetLocalTimeStr = msg.payload.data.new_state.attributes[\"Local Time\"];\nvar alarmSetTimeMS = msg.payload.data.new_state.attributes[\"Time in Milliseconds\"];\nvar alarmSetTimeDT = new Date(alarmSetTimeMS)\nvar alarmSetTimeLess5mMS = alarmSetTimeMS - fiveMinsAsMS;\nvar alarmSetTimeLess5mDT = new Date(alarmSetTimeLess5mMS);\nvar alarmSetTimeLess5mLocal = alarmSetTimeLess5mDT.toString();\nvar alarmSetTimeLess5mLocale = alarmSetTimeLess5mDT.toLocaleString(\"en-GB\", { timeZone: 'UTC' });\n\nmsg.payload = {\n    AlarmSetLocalTimeStr: AlarmSetLocalTimeStr,\n    AlarmSetTimeMS: alarmSetTimeMS,\n    alarmSetTimeDT: alarmSetTimeDT,\n    alarmSetTimeLess5mMS: alarmSetTimeLess5mMS,\n    alarmSetTimeLess5mDT: alarmSetTimeLess5mDT,\n    alarmSetTimeLess5mLocale: alarmSetTimeLess5mLocale\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":620,"wires":[["f598d951.3b3d88"]]},{"id":"f598d951.3b3d88","type":"debug","z":"2680e2cc.e6dace","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1030,"y":620,"wires":[]}]

You could do this a number of ways. Here is a demo using cron-plus. It takes the datetime in your data, minuses 5m, prepares a command to add/update a dynamic schedule in the cron-plus node...


↑ NOTE I had to overwrite the datetime from your sample alarm data as it was in the past (and the cron node cannot time travel) :wink:

the flow...

[{"id":"9631a70f.fa3998","type":"cronplus","z":"275d89c3.163556","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[],"x":1400,"y":600,"wires":[["42c23eb7.6f6be"]]},{"id":"1f3cd0a4.6dd34f","type":"inject","z":"275d89c3.163556","name":"sm_g920f_volgende_alarm (fake)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"topic\":\"sensor.sm_g920f_volgende_alarm\",\"payload\":\"2021-03-06T04:10:00.000Z\",\"data\":{\"entity_id\":\"sensor.sm_g920f_volgende_alarm\",\"old_state\":{\"entity_id\":\"sensor.sm_g920f_volgende_alarm\",\"state\":\"unknown\",\"attributes\":{\"friendly_name\":\"SM-G920F Volgende alarm\",\"icon\":\"mdi:alarm\",\"device_class\":\"timestamp\"},\"last_changed\":\"2021-03-05T18:51:55.255378+00:00\",\"last_updated\":\"2021-03-05T18:51:55.255378+00:00\",\"context\":{\"id\":\"b47d7b96fae6d771440480089578579a\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unknown\"},\"new_state\":{\"entity_id\":\"sensor.sm_g920f_volgende_alarm\",\"state\":\"2021-03-06T04:10:00.000Z\",\"attributes\":{\"Local Time\":\"Sat Mar 06 05:10:00 GMT+01:00 2021\",\"Package\":\"com.sec.android.app.clockpackage\",\"Time in Milliseconds\":1615003800000,\"friendly_name\":\"SM-G920F Volgende alarm\",\"icon\":\"mdi:alarm\",\"device_class\":\"timestamp\"},\"last_changed\":\"2021-03-05T21:12:38.733236+00:00\",\"last_updated\":\"2021-03-05T21:12:38.733236+00:00\",\"context\":{\"id\":\"4791f4daa44c22405ea6143da5e9e78c\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"2021-03-06T04:10:00.000Z\",\"timeSinceChangedMs\":16}},\"_msgid\":\"6b4035b8.08086c\"}","payloadType":"json","x":1390,"y":480,"wires":[["875aa68.212fa58"]]},{"id":"875aa68.212fa58","type":"function","z":"275d89c3.163556","name":"Minus 5m and prepare cron command","func":"//msg.payload\nvar fiveMinsAsMS = (60*1000)*5;\nvar AlarmSetLocalTimeStr = msg.payload.data.new_state.attributes[\"Local Time\"];\nvar alarmSetTimeMS = msg.payload.data.new_state.attributes[\"Time in Milliseconds\"];\n\n//temporary override to demonstrate dynamic cron setting\nalarmSetTimeMS = Date.now() + (60*1000)*10\n\nvar alarmSetTimeDT = new Date(alarmSetTimeMS)\nvar alarmSetTimeLess5mMS = alarmSetTimeMS - fiveMinsAsMS;\nvar alarmSetTimeLess5mDT = new Date(alarmSetTimeLess5mMS);\nvar alarmSetTimeLess5mLocal = alarmSetTimeLess5mDT.toString();\nvar alarmSetTimeLess5mLocale = alarmSetTimeLess5mDT.toLocaleString(\"en-GB\", { timeZone: 'UTC' });\n\n\n\n\nmsg.payload = {\n    \"command\": \"add\",\n    \"name\": \"alarm\",\n    \"expression\": alarmSetTimeLess5mDT,\n    \"expressionType\": \"dates\",\n    \"payloadType\": \"json\",\n    \"payload\": {\n        AlarmSetLocalTimeStr: AlarmSetLocalTimeStr,\n        AlarmSetTimeMS: alarmSetTimeMS,\n        alarmSetTimeDT: alarmSetTimeDT,\n        alarmSetTimeLess5mMS: alarmSetTimeLess5mMS,\n        alarmSetTimeLess5mDT: alarmSetTimeLess5mDT,\n        alarmSetTimeLess5mLocale: alarmSetTimeLess5mLocale\n    }\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1410,"y":540,"wires":[["1ffcc612.4b721a","9631a70f.fa3998"]]},{"id":"1ffcc612.4b721a","type":"debug","z":"275d89c3.163556","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1670,"y":540,"wires":[]},{"id":"42c23eb7.6f6be","type":"debug","z":"275d89c3.163556","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1670,"y":600,"wires":[]}]

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.