Okay, maybe dum question. I just tried to import it in clipboard and it seems to work!
Marcus,
I have done following flow, based on your example. But I can not get it to work, nothing happends.
[{"id":"7d0928f6.1a5a98","type":"function","z":"205212a7.3d0f4e","name":"home/not_home","func":"newmsg = {};\nif (msg.payload == \"home\")\n{\n newmsg.payload = \"on\";\n}else {\n newmsg.payload = \"off\";\n}\n\nreturn newmsg;","outputs":1,"noerr":0,"x":314,"y":212,"wires":[["9e825670.89d5f8","c0a30562.ddb448"]]},{"id":"9e825670.89d5f8","type":"stoptimer","z":"205212a7.3d0f4e","duration":"3","units":"Hour","payloadtype":"str","payloadval":"on","name":"","x":530,"y":212,"wires":[["d8974a3b.733858","5cb3bf1d.956bb","b8e389dd.7d7a28"],["ba71ecf2.1b796"]]},{"id":"c0a30562.ddb448","type":"debug","z":"205212a7.3d0f4e","name":"Laptop on/off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":152,"wires":[]},{"id":"d8974a3b.733858","type":"debug","z":"205212a7.3d0f4e","name":"Timer on","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":760,"y":152,"wires":[]},{"id":"5cb3bf1d.956bb","type":"api-call-service","z":"205212a7.3d0f4e","name":"Fibaro Wall Plug 2 on","server":"da215a6e.38deb8","service_domain":"homeassistant","service":"turn_on","data":"{\"entity_id\":\"switch.fibaro_system_fgwpe_f_wall_plug_switch\"}","mergecontext":"","output_location":"","output_location_type":"none","x":826,"y":200,"wires":[[]]},{"id":"b8e389dd.7d7a28","type":"stoptimer","z":"205212a7.3d0f4e","duration":"1","units":"Hour","payloadtype":"str","payloadval":"on","name":"","x":776,"y":260,"wires":[["9aa9de98.2791","fc57f7a0.544948"],["fc57f7a0.544948"]]},{"id":"9aa9de98.2791","type":"api-call-service","z":"205212a7.3d0f4e","name":"Fibaro Wall Plug 2 off","server":"da215a6e.38deb8","service_domain":"homeassistant","service":"turn_off","data":"{\"entity_id\":\"switch.fibaro_system_fgwpe_f_wall_plug_switch\"}","mergecontext":"","output_location":"","output_location_type":"none","x":1026,"y":253,"wires":[[]]},{"id":"fc57f7a0.544948","type":"debug","z":"205212a7.3d0f4e","name":"Timer off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":986,"y":300,"wires":[]},{"id":"30358d55.da2522","type":"debug","z":"205212a7.3d0f4e","name":"Laptop test1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":310,"y":160,"wires":[]},{"id":"ba71ecf2.1b796","type":"debug","z":"205212a7.3d0f4e","name":"Laptop test3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":716,"y":300,"wires":[]},{"id":"a45610e.3e286f","type":"server-state-changed","z":"205212a7.3d0f4e","name":"Ipod","server":"da215a6e.38deb8","entityidfilter":"device_tracker.freddas_ipod","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"x":110,"y":212,"wires":[["30358d55.da2522","7d0928f6.1a5a98"]]},{"id":"da215a6e.38deb8","type":"server","z":"","name":"Home Assistant"}]
Do you have any suggestions how I should fault trace it?
The Ipod is at home, the timer should start!
I have used the Node-Red Light Scheduler, which is covering most of your requirements (I think) ā¦ maybe you wanna have a check as it is very simple to use
Hi Fredrik,
I think the home/not_home check should be a switch rather then a function.
Just checking the function, seems you have if msg.payload == āhomeā. Try it with just one equal sign.
I think a simple switch will do it. I have a few node red flows that run or donāt run based on my location.
But it is only triggered when the state CHANGES to home. If you are already home, and you deploy, the state hasnāt changed.
Thatās not the Javascript way.
Ah in this case simply check the box āOutput on Connectā
Greg,
Yes maybe it should be a switch rather then a function.
But if I change to a switch will I achieve the functionality that the timmer only counts when the Ipod is at home? Or will it trig the timmer to start, then the timmer will continue to count for 3 hours. Although the Ipod is not at home?
Is there any HA node in node red that can read the HA status without state changes?
Well, something has to be a TRIGGER at some point.
If you read the node, you will see that you can cause it to trigger on deploy.
Or you can use something else to trigger (time?) and have it CHECK STATE of another entity.
You can use a CHANGE node to stop the timer so that if the ipod changes state to not_home you can stop the 3 hour timer.
In this example a flow is triggered that will open the blinds if it is daytime. If it is not daytime it will wait 5 minutes and then check again. If it is then daytime it will open the blinds and stop the timer.
Hi Greg,
I did following flow as a test.
And it works fine with home/not_home.
But I would like the timer to not start from zero time each time it starts. Rather it should start count from itās old value. In that way I get the total time the Ipod/Laptop has been on.
Hi Fredrik,
Not sure how to do that one. Sorry.
The stop timer is used to delay the message not to time how long something has been in a certain state.
Hi,
Now I have created a new flow with some functions.
[{"id":"53d110b4.b1453","type":"debug","z":"205212a7.3d0f4e","name":"Timer on2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1260,"y":820,"wires":[]},{"id":"618e7570.e255ac","type":"debug","z":"205212a7.3d0f4e","name":"Timer on3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1260,"y":860,"wires":[]},{"id":"b9d9880d.84b318","type":"debug","z":"205212a7.3d0f4e","name":"Timer on1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1060,"y":760,"wires":[]},{"id":"40a5bd13.4f75c4","type":"inject","z":"205212a7.3d0f4e","name":"home","topic":"","payload":"home","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":762,"wires":[["a2371297.2b944"]]},{"id":"a9f43eab.814ea","type":"inject","z":"205212a7.3d0f4e","name":"not_home","topic":"","payload":"not_home","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":802,"wires":[["a2371297.2b944"]]},{"id":"a2371297.2b944","type":"switch","z":"205212a7.3d0f4e","name":"home/not home","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"home","vt":"str"},{"t":"eq","v":"not_home","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":360,"y":786,"wires":[["551150f1.4cbaf"],["2a8c3243.73ab2e"]]},{"id":"2a8c3243.73ab2e","type":"change","z":"205212a7.3d0f4e","name":"send OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":808,"wires":[["3518cdeb.3d17c2"]]},{"id":"551150f1.4cbaf","type":"change","z":"205212a7.3d0f4e","name":"send ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":573,"y":760,"wires":[["835b46de.038cc8"]]},{"id":"835b46de.038cc8","type":"function","z":"205212a7.3d0f4e","name":"creating & saving start time","func":"context.global.startTime = new Date().getTime();\nreturn msg;","outputs":1,"noerr":0,"x":807,"y":760,"wires":[["b9d9880d.84b318"]]},{"id":"3518cdeb.3d17c2","type":"function","z":"205212a7.3d0f4e","name":"time elapsed","func":"msg2 = {};\ncontext.global.g_timeElapsedTot;\nvar v_currentTime = new Date().getTime();\nvar v_timeElapsed = (v_currentTime - context.global.startTime);\nvar v_timeElapsedTot = 0;\n\nif (v_timeElapsedTot == 60) //1 min\n//if (v_timeElapsedTot == 10800) //3 hours\n{\n v_timeElapsedTot = 0;\n g_timeElapsedTot = 0;\n}else {\n v_timeElapsedTot = (context.global.g_timeElapsedTot + v_timeElapsed);\n context.global.g_timeElapsedTot = v_timeElapsedTot;\n}\n\nmsg.payload = \"Time elapsed is: \" + v_timeElapsed + \" seconds.\";\nmsg2.payload = \"Total time elaped is: \" + v_timeElapsedTot + \" seconds.\";\n\nreturn [ msg, msg2 ];\n","outputs":2,"noerr":0,"x":1060,"y":809,"wires":[["53d110b4.b1453"],["618e7570.e255ac"]]}]
The first function just save the start time and the second function should calculate the elapsed time.
But I do something wrong in the code because the total elapsed time does not reset after 60sec.
Instead it just count up, never ends!
How do I get the total elapsed time to be reseted at 60sec?
And start all over again.
First of all, donāt use msg2
and are you sure you want to override your payload? Create an object in msg
like msg.timeElapsed
and msg.totalTimeElapsed
.
Personally I would use a flow variable, but just resetting one of those objects to 0 when the timer starts will work.
Hi flamingm0e,
Iām new to the programming with Javascript, but Iām learning
I donāt really know how to create objects and so on.
Could you please help me with this?
I have done this.
msg2 = {};
context.global.g_timeElapsedTot;
var v_currentTime = new Date().getTime();
var v_timeElapsed = (v_currentTime - context.global.startTime);
var v_timeElapsedTot = 0;
var timeElapsed = Object.create(null);
var totalTimeElapsed = Object.create(null);
if (v_timeElapsedTot == 60) //1 min
//if (v_timeElapsedTot == 10800) //3 hours
{
v_timeElapsedTot = 0;
g_timeElapsedTot = 0;
}
else
{
v_timeElapsedTot = (context.global.g_timeElapsedTot + v_timeElapsed);
context.global.g_timeElapsedTot = v_timeElapsedTot;
}
msg.timeElapsed = "Time elapsed is: " + v_timeElapsed + " seconds.";
msg.totalTimeElapsed = "Total time elaped is: " + v_timeElapsedTot + " seconds.";
return msg;
But the totalTimeElapsed dose not get reset at 60 sec, just keep on counting up.
This is the debug message I receives.
I donāt understand the units I receives, it is not in seconds !
Okay, now I have made some more updates.
But still I have problems with the reset of the timer/counter.
context.global.g_timeElapsedTot;
var timeElapsed = Object.create(null);
var totalTimeElapsed = Object.create(null);
var v_currentTime = new Date().getTime();
var v_timeElapsed = (v_currentTime - context.global.g_startTime);
if (context.global.g_timeElapsedTot >= 60000) //1 min (60.000sec)
//if (v_timeElapsedTot == 10800000) //3 hours
{
context.global.g_timeElapsedTot = 0;
}
else
{
context.global.g_timeElapsedTot = (context.global.g_timeElapsedTot + v_timeElapsed);
}
msg.timeElapsed = "Time elapsed is: " + v_timeElapsed + " seconds.";
msg.totalTimeElapsed = "Total time elaped is: " + context.global.g_timeElapsedTot + " seconds.";
return msg;
When I run the flow I can see following thinks in the debug window.
Why is not the Total time elapsed reset at 60000 ?
If I restart and stop the flow again the Total time elapsed will be reset.
Maybe this is okay, only the debug function that is not display everthing in a correct way ?
What do you think ?