Hi there, I am new to Node-RED.
I wanted to create an home automation to notify me when the internal pm2.5 is higher than certain level, say >80 for 30 mins, during day time.
Below is my existing setup (first time posting the code here so I hope it worked). However, I would like to ask the following:
-
How do I improve this automation so that it will announce /notify again after certain time (say 1 hour) if the air quality is still above the threshold?
-
Further from 1), how do I improve it to announce /notify only if the air quality is worse than the previous check?
Thanks a lot.
code:
[{"id":"66fadf29cf86532a","type":"subflow","name":"Volume Adj 50%","info":"","category":"","in":[{"x":420,"y":240,"wires":[{"id":"eac82526765ddfc9"}]}],"out":[{"x":1180,"y":220,"wires":[{"id":"29de2114963e347f","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"29de2114963e347f","type":"api-call-service","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"Set volume to 50%","server":"ab275ce0.0e1d7","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["media_player.living_room_2"],"data":"{\"volume_level\":0.5}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"data"}],"queue":"none","x":910,"y":220,"wires":[[]]},{"id":"fef2dcc69bd58180","type":"switch","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"50% ?","property":"data.attributes.volume_level","propertyType":"msg","rules":[{"t":"neq","v":"0.5","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":730,"y":220,"wires":[["29de2114963e347f","ad09c5a32fc0fb1a"]]},{"id":"eac82526765ddfc9","type":"api-current-state","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"Speaker","server":"ab275ce0.0e1d7","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"media_player.living_room_2","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"for":0,"forType":"num","forUnits":"minutes","x":580,"y":220,"wires":[["fef2dcc69bd58180"]]},{"id":"ad09c5a32fc0fb1a","type":"function","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"","func":"msg.payload =\n{\n \"data\":{\n \"entity_id\":\"media_player.living_room_speaker\",\n \"volume_level\": msg.data.attributes.volume_level\n }\n}\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":320,"wires":[["b4ce0345a5f79159","2ba8cc6a9e41bd1b"]]},{"id":"b4ce0345a5f79159","type":"delay","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"","pauseType":"delay","timeout":"20","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":800,"y":320,"wires":[["bc32f868005ab90e"]]},{"id":"bc32f868005ab90e","type":"api-call-service","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"Set volume to Previous","server":"ab275ce0.0e1d7","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["media_player.living_room_2"],"data":"{\"volume_level\":\"{{msg.payload}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"data"}],"queue":"none","x":1010,"y":320,"wires":[["c4512046ab4f318a"]]},{"id":"c4512046ab4f318a","type":"debug","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":280,"wires":[]},{"id":"2ba8cc6a9e41bd1b","type":"debug","z":"66fadf29cf86532a","g":"f22053755624b07a","name":"debug 13","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":380,"wires":[]},{"id":"f22053755624b07a","type":"group","z":"66fadf29cf86532a","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["29de2114963e347f","fef2dcc69bd58180","eac82526765ddfc9","ad09c5a32fc0fb1a","b4ce0345a5f79159","bc32f868005ab90e","c4512046ab4f318a","2ba8cc6a9e41bd1b"],"x":494,"y":179,"w":652,"h":242},{"id":"8caa74290a4b6db1","type":"api-current-state","z":"a8f290d6aab3d932","name":"current state of PM2.5","server":"ab275ce0.0e1d7","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.ikea_air_sensor_pm25","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"air_pm","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":200,"wires":[["d47954667195ea24","29403aedb8b65e77"]]},{"id":"75f22ec0d9603bbc","type":"server-state-changed","z":"a8f290d6aab3d932","name":"air quality drops (PM2.5)","server":"ab275ce0.0e1d7","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"sensor.ikea_air_sensor_pm25","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"80","ifStateType":"num","ifStateOperator":"gt","outputOnlyOnStateChange":true,"for":"30","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":150,"y":160,"wires":[["3589c3d21f6e47a3"],[]]},{"id":"728e1a97eab895a0","type":"api-call-service","z":"a8f290d6aab3d932","name":"TTS current state","server":"ab275ce0.0e1d7","version":5,"debugenabled":true,"domain":"tts","service":"edge_tts_say","areaId":[],"deviceId":[],"entityId":["media_player.living_room_2"],"data":"{\"message\":\"The air quality is poor. PM 2.5 level is at {{air_pm}}, watch out\",\"language\":\"zh-HK-HiuGaaiNeural\",\"options\":{\"rate\":\"-20%\",\"volume\":\"+0%\"}}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":690,"y":280,"wires":[["68cdce0d8cd4f470"]]},{"id":"68cdce0d8cd4f470","type":"debug","z":"a8f290d6aab3d932","name":"debug 6","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":280,"wires":[]},{"id":"d47954667195ea24","type":"api-call-service","z":"a8f290d6aab3d932","name":"Notification","server":"ab275ce0.0e1d7","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_iphone_e","areaId":[],"deviceId":[],"entityId":[],"data":"{\"message\":\"Air quality is poor, the pm2.5 level is at {{payload}}\",\"title\":\"Testing title send\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":630,"y":200,"wires":[["23eea19ae4531699"]]},{"id":"23eea19ae4531699","type":"debug","z":"a8f290d6aab3d932","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":200,"wires":[]},{"id":"29403aedb8b65e77","type":"subflow:66fadf29cf86532a","z":"a8f290d6aab3d932","name":"","x":480,"y":280,"wires":[["728e1a97eab895a0"]]},{"id":"3589c3d21f6e47a3","type":"time-range-switch","z":"a8f290d6aab3d932","name":"Day time","lat":"22.3445","lon":"114.12111","startTime":"10:00","endTime":"21:00","startOffset":0,"endOffset":0,"x":220,"y":220,"wires":[["8caa74290a4b6db1"],[]]},{"id":"ab275ce0.0e1d7","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]