Multiple "Get Template" in the same flow?

Hi

I implemented some flows a while back which has 2 “Get Templates” in it, and everything seemed to work. Yesterday I created a new one and couldn’t get it to work, so I started troubleshooting it and found out that my previous flows wasn’t working as intended.

When executing this flow with 2 “Get Template” the payload from the first template seems to overrun the second check.

The first check is true, but the second one is false, but as you can see in the debug window I get 2 “True” results.
What do I need to do to use 2 or more of the “Get Template”?

Here’s the code if you wanna try it yourself

[{"id":"e139afbb.2d9ca","type":"debug","z":"623c0f42.a6265","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":980,"wires":[]},{"id":"a19dbbb9.b50698","type":"inject","z":"623c0f42.a6265","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":900,"wires":[["e3a41c52.97d4c"]]},{"id":"e3a41c52.97d4c","type":"api-render-template","z":"623c0f42.a6265","name":"last motion >240 seconds","server":"6b0eff97.ad5e1","template":"{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.hall_rorelsesensor.last_changed)) > 240 }}","x":350,"y":900,"wires":[["978c6c63.a038b","e139afbb.2d9ca"]]},{"id":"978c6c63.a038b","type":"switch","z":"623c0f42.a6265","name":"True?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"},{"t":"eq","v":"False","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":900,"wires":[["d1e3194b.c4c378"],[]]},{"id":"d1e3194b.c4c378","type":"api-render-template","z":"623c0f42.a6265","name":"last motion >20000 seconds","server":"6b0eff97.ad5e1","template":"{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.hall_2_rorelsesensor.last_changed)) > 20000 }}","x":800,"y":900,"wires":[["e139afbb.2d9ca"]]},{"id":"e3465947.32db38","type":"comment","z":"623c0f42.a6265","name":"This is true","info":"This ","x":400,"y":860,"wires":[]},{"id":"9e795128.5fee9","type":"comment","z":"623c0f42.a6265","name":"This is false","info":"This ","x":850,"y":860,"wires":[]},{"id":"6b0eff97.ad5e1","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true}]

Oh and maybe I should give you some context.

This is all for controlling the lights based on doors, motion sensors and “manual” overrides.

  1. When light.garage turns on start the flow
  2. Wait 250 seconds
  3. First Get Template checks if the state for Motion sensor X hasn’t changed for the last 240 seconds
  4. If True, check if the state for motion sensor Y hasn’t changed for the last 240 seconds
  5. If True, turn off the lights since there is no one left in the garage.
  6. If any of 4 or 5 is false I’m using looptimer to wait for 30 seconds then check again.

Maybe there’s a better way of doing this? Here’s a bad print of a flow which can be triggered by 2 motion sensors. This was the one i thought was working but apparently isn’t, it ignores the “number 3” check. Sorry for the swedish, but I think you understand what it does anyway :slight_smile:

user @Chapapa at Discord found a solution as he had the same issue.
But if someone knows a cleaner way of doing this, please let me know :slight_smile:

[{"id":"11d50307.f432ad","type":"inject","z":"623c0f42.a6265","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1100,"y":960,"wires":[["ccf56cd6.bef5f","e6224dc7.25296","f07a0e5b.f9db9"]]},{"id":"ccf56cd6.bef5f","type":"api-render-template","z":"623c0f42.a6265","name":"240sec","server":"6b0eff97.ad5e1","template":"{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.hall_rorelsesensor.last_changed)) > 240 }}","x":1320,"y":960,"wires":[["c9528629.1bbc28"]]},{"id":"c9528629.1bbc28","type":"switch","z":"623c0f42.a6265","name":"True?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"},{"t":"eq","v":"False","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1490,"y":960,"wires":[["eb24ee92.c2986"],["bcbe151.ca7f4e8"]]},{"id":"e6224dc7.25296","type":"api-render-template","z":"623c0f42.a6265","name":"240sec","server":"6b0eff97.ad5e1","template":"{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.hall_2_rorelsesensor.last_changed)) > 240 }}","x":1320,"y":1000,"wires":[["69859b08.686894"]]},{"id":"44502cd6.fd9144","type":"function","z":"623c0f42.a6265","name":"Merge results","func":"// add a context.data for every check you have, 2 checks, 2 context data for example.\ncontext.data = context.data = context.data || {};\n\nswitch (msg.topic) {\n//copy line 6-9 for every check you have and update the name \"case \"hall1\" where hall1 is the name\n    case \"hall1\":\n        context.data.hall1 = msg.payload;\n        msg = null;\n        break;\n    case \"hall2\":\n        context.data.hall2 = msg.payload;\n        msg = null;\n        break;\n    case \"overrideon\":\n        context.data.overrideon = msg.payload;\n        msg = null;\n        break;        \n    default:\n        msg = null;\n    \tbreak;\n\n}\n//add the line && context.data.overrideon != null for each check you have and update the name\nif(context.data.hall1 != null && context.data.hall2 != null && context.data.overrideon != null ) {\n\tmsg2 = {};\n//add a \"+context.data.overrideon\" for each check you have and update the name\n    msg2.payload = context.data.hall1+context.data.hall2+context.data.overrideon;\n    \n    context.data=null;\n\treturn msg2;\n} ","outputs":1,"noerr":0,"x":1800,"y":980,"wires":[["ca250e02.c8ff5"]]},{"id":"eb24ee92.c2986","type":"function","z":"623c0f42.a6265","name":"hall1","func":"msg.topic=\"hall1\";\nmsg.payload=\"true\";\nreturn msg;","outputs":1,"noerr":0,"x":1610,"y":960,"wires":[["44502cd6.fd9144"]]},{"id":"2048171f.58bd98","type":"function","z":"623c0f42.a6265","name":"hall2","func":"msg.topic=\"hall2\";\nmsg.payload=\"true\";\nreturn msg;","outputs":1,"noerr":0,"x":1610,"y":1000,"wires":[["44502cd6.fd9144"]]},{"id":"ca250e02.c8ff5","type":"switch","z":"623c0f42.a6265","name":"truetruetrue","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"truetruetrue","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1970,"y":980,"wires":[[]]},{"id":"69859b08.686894","type":"switch","z":"623c0f42.a6265","name":"True?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"},{"t":"eq","v":"False","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1490,"y":1000,"wires":[["2048171f.58bd98"],["d47e796c.8be5b8"]]},{"id":"bcbe151.ca7f4e8","type":"stoptimer","z":"623c0f42.a6265","duration":"5","units":"Second","payloadtype":"num","payloadval":"0","name":"30sec","x":1490,"y":1080,"wires":[["ccf56cd6.bef5f"],[]]},{"id":"d47e796c.8be5b8","type":"stoptimer","z":"623c0f42.a6265","duration":"5","units":"Second","payloadtype":"num","payloadval":"0","name":"30sec","x":1490,"y":1040,"wires":[["e6224dc7.25296"],[]]},{"id":"f07a0e5b.f9db9","type":"api-render-template","z":"623c0f42.a6265","name":"override on?","server":"6b0eff97.ad5e1","template":"{{ states.input_boolean.garage_manual_lights_on.state }}","x":1330,"y":920,"wires":[["945232c5.8379a"]]},{"id":"3f6d39c0.2d24e6","type":"function","z":"623c0f42.a6265","name":"overrideon","func":"msg.topic=\"overrideon\";\nmsg.payload=\"true\";\nreturn msg;","outputs":1,"noerr":0,"x":1630,"y":920,"wires":[["44502cd6.fd9144"]]},{"id":"945232c5.8379a","type":"switch","z":"623c0f42.a6265","name":"off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1490,"y":920,"wires":[["3f6d39c0.2d24e6"]]},{"id":"6b0eff97.ad5e1","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true}]

[{"id":"b44a420f.6d0f","type":"trigger","z":"3b663990.143856","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"4","extend":true,"units":"min","reset":"","bytopic":"all","name":"","x":262,"y":496,"wires":[["2239fd48.457392"]]},{"id":"d9f3c029.6c5ed","type":"server-state-changed","z":"3b663990.143856","name":"motion?","server":"ef067c6f.620e6","entityidfilter":"binary_sensor\\.(hall_rorelsesensor|hall_2_rorelsesensor)","entityidfiltertype":"regex","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":false,"x":82,"y":496,"wires":[["b44a420f.6d0f"],[]]},{"id":"1786a7ff.384238","type":"inject","z":"3b663990.143856","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":92,"y":544,"wires":[["b44a420f.6d0f"]]},{"id":"816b0931.1c7a78","type":"debug","z":"3b663990.143856","name":"Turn off Light","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":566,"y":496,"wires":[]},{"id":"2239fd48.457392","type":"api-current-state","z":"3b663990.143856","name":"override?","server":"ef067c6f.620e6","halt_if":"off","halt_if_type":"str","halt_if_compare":"is_not","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"input_boolean.garage_manual_lights_on","state_type":"str","outputs":2,"x":412,"y":496,"wires":[["816b0931.1c7a78"],[]]}]