# Action based in hours and previous signals

**URL:** https://discourse.nodered.org/t/action-based-in-hours-and-previous-signals/65502
**Category:** General
**Created:** [23 July 2022 13:40 UTC](https://discourse.nodered.org/t/action-based-in-hours-and-previous-signals/65502 "2022-07-23T13:40:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gregolin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregolin/32/65109_2.png) [@gregolin](https://discourse.nodered.org/u/gregolin)
#### Post date: [23 July 2022 13:40 UTC](https://discourse.nodered.org/t/action-based-in-hours-and-previous-signals/65502/1 "2022-07-23T13:40:13Z")

</div>

Hi.  
I'm new with node red, but having some success with the most of my projects 🙂  
I'm facing issues with one of them where I want to turn the light on when I open my smart lock and the time is between 6pm and 4am. Immediately after I deploy my code and test, it seems working properly but it seems turn on the light with no smart locker action as well. I included the time verification in the middle of the node and I've thought it would be executed only with the smart lock action but it seems it is executed independent of the lock is opened or not. Could anyone help me with this issue, please?  
Thanks.

```auto
[{"id":"12f28c2354857f16","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"29efafe90a107056","type":"mqtt in","z":"12f28c2354857f16","name":"YDF 40A - Living","topic":"zigbee2mqtt/Sala Fechadura","qos":"2","datatype":"auto-detect","broker":"a5b05a5352bc5bae","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":140,"wires":[["b0e7bd1f5ac79d0d"]]},{"id":"35c1d12bd75dd19b","type":"function","z":"12f28c2354857f16","name":"Source","func":"const acao = msg.payload.action_source;\nvar acao_txt = '';\nswitch (acao) {\n case 0:\n acao_txt = 'Open - pass';\n break;\n case 1:\n acao_txt = 'Open - HA';\n break;\n case 4:\n acao_txt = 'Open - fingerprint';\n break;\n default:\n acao_txt = null;\n}\nmsg.payload.acao_txt = acao_txt;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":160,"wires":[["914288d5d8a361f5"]]},{"id":"914288d5d8a361f5","type":"function","z":"12f28c2354857f16","name":"User","func":"var comp_txt = '';\nvar usuario = msg.payload.action_user;\nswitch (usuario) {\n case 1:\n comp_txt = 'User A';\n break;\n case 2:\n comp_txt = 'User B';\n break;\n case 3:\n comp_txt = 'User C';\n break;\n case 4:\n comp_txt = 'User D';\n break;\n default:\n comp_txt = '';\n}\nif (comp_txt != '')\n comp_txt = ' - ' + comp_txt;\nmsg.payload.mensagem = msg.payload.acao_txt + comp_txt;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":160,"wires":[["6bbba2b80216eadd"]]},{"id":"d4caad3e112576bc","type":"api-call-service","z":"12f28c2354857f16","name":"PushNot","server":"fad61953.26ffa8","version":5,"debugenabled":false,"domain":"notify","service":"notify","areaId":[],"deviceId":[],"entityId":[],"data":"{\"message\":\"{{payload.mensagem}}\",\"title\":\"Locker - Living\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1100,"y":140,"wires":[[]]},{"id":"6bbba2b80216eadd","type":"switch","z":"12f28c2354857f16","name":"Validation (cont)","property":"payload.acao_txt","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"false","repair":false,"outputs":1,"x":900,"y":160,"wires":[["d4caad3e112576bc"]]},{"id":"b0e7bd1f5ac79d0d","type":"switch","z":"12f28c2354857f16","name":"User or Manual_Auto","property":"payload.action_source","propertyType":"msg","rules":[{"t":"eq","v":"2","vt":"str"},{"t":"neq","v":"2","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":140,"wires":[["3b1d309a38013302"],["35c1d12bd75dd19b","9cd65854d399e592"]]},{"id":"3b1d309a38013302","type":"function","z":"12f28c2354857f16","name":"Manual_Auto","func":"var comp_txt = '';\nvar forma = msg.payload.action;\nswitch (forma) {\n case 'auto_lock':\n comp_txt = 'Close - Automatic';\n break;\n case 'manual':\n comp_txt = 'Close - Manual';\n break;\n case 'unlock':\n comp_txt = 'Open - Manual';\n break;\n case 'lock':\n comp_txt = 'Close - Manual';\n break;\n}\nmsg.payload.mensagem = comp_txt;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":120,"wires":[["0baeb75e7391ef45"]]},{"id":"0baeb75e7391ef45","type":"switch","z":"12f28c2354857f16","name":"Validation (msg)","property":"payload.mensagem","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"false","repair":false,"outputs":1,"x":900,"y":120,"wires":[["d4caad3e112576bc"]]},{"id":"9cd65854d399e592","type":"time-range-switch","z":"12f28c2354857f16","name":"Turn On Light","lat":"","lon":"","startTime":"18:00","endTime":"04:00","startOffset":0,"endOffset":0,"x":580,"y":220,"wires":[["6dfd10db01f10f9c"],[]]},{"id":"6dfd10db01f10f9c","type":"api-call-service","z":"12f28c2354857f16","name":"Light - Living","server":"fad61953.26ffa8","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.sala_interru_center"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":850,"y":220,"wires":[[]]},{"id":"a5b05a5352bc5bae","type":"mqtt-broker","name":"MQTT HA","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"fad61953.26ffa8","type":"server","name":"Home Assistant","addon":true}]

```

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [23 July 2022 16:04 UTC](https://discourse.nodered.org/t/action-based-in-hours-and-previous-signals/65502/2 "2022-07-23T16:04:14Z")

</div>

You should add a `debug` node to the output of your `mqtt-in` node and check what the 'type'is for payload.action\_source. In the switch you have  
 ![Screen Shot 2022-07-23 at 11.59.48 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8ad7a944eacf3aed76358586c60fe09f474b4d97.png)

Notice you are testing for the `string` `2` not the `number` 2

---

<div class="post-metadata">

### Author: ![gregolin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregolin/32/65109_2.png) [@gregolin](https://discourse.nodered.org/u/gregolin)
#### Post date: [24 July 2022 17:02 UTC](https://discourse.nodered.org/t/action-based-in-hours-and-previous-signals/65502/3 "2022-07-24T17:02:32Z")

</div>

It worked, thanks!  
I think the main reason for the issue was related to the `!=`, where I accepted any value, so the process could start anytime with any value. I changed it for the 3 values that I need work with and using only comparison `==`, but also changing the `string` to `number`, thanks.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [7 August 2022 17:02 UTC](https://discourse.nodered.org/t/action-based-in-hours-and-previous-signals/65502/4 "2022-08-07T17:02:56Z")

</div>

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