Trying to set up Air Quality Automation

I got a air quality sensor on my balcony and I want to use a delta of the sensor as trigger. My neigbors in the apartment below me are heavy smokers so wenn they start to smoke I want informed so I don’t go outside and can close the windows.

I struggle the the function node. I changed the topic of the message to the variable name to get 2 sensors and wrote some code to subtract them fropme each other but I don’t know how to declare them a variables in the code.

Capture_2

This looks a bit messy.
You get a value from your sensor and then send it in two directions. One direction is delayed 10secs, but that is all.
You then make some evaluations on the values in the function node, but I have. Ahars time figuring out what you are trying to do.
Node Red handles messages as they arrive, so you will get two distinct evaluations of the same sensor value just with 10 secs interval.
You will not be able to compare the two flows because the function node will have finished evaluating the first message long before the second one arrive and if they were merged into on message, then the two values would be the same anyway.

If you really want help then export the nodes to the clipboard and paste it in here.
Mark then pasted text and click the </> icon to make it a formatted text.

I got the automation working.

Here is the flow if someone has a similar problem.

[{"id":"542304b23c76704f","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"ed558064e11fc7b3","type":"server-state-changed","z":"542304b23c76704f","name":"Air Balcony","server":"14819af3.534ef5","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.particulate_matter_2_5um_concentration","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","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":120,"wires":[["6414a766cce67880","8b8d647651988a81"]]},{"id":"8b8d647651988a81","type":"function","z":"542304b23c76704f","name":"First Function","func":"global.set(\"sensor1\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":80,"wires":[["3afacbb1a57ae17d"]]},{"id":"3afacbb1a57ae17d","type":"debug","z":"542304b23c76704f","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":80,"wires":[]},{"id":"21e971c3e87a82b5","type":"api-call-service","z":"542304b23c76704f","name":"Assi-Alarm","server":"14819af3.534ef5","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_b3n_phone","areaId":[],"deviceId":[],"entityId":[],"data":"{\"message\":\"Balkon ist vergifted\",\"title\":\"Assi-Alarm\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1150,"y":140,"wires":[[]]},{"id":"6414a766cce67880","type":"delay","z":"542304b23c76704f","name":"","pauseType":"delay","timeout":"60","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":320,"y":140,"wires":[["7361048a0bf4a83c"]]},{"id":"7361048a0bf4a83c","type":"function","z":"542304b23c76704f","name":"Second Function","func":"// get the value of the first sensor from the global variable\nvar sensor1 = global.get(\"sensor1\");\n\n// subtract the value of the second sensor from the first sensor\nvar result = sensor1 - msg.payload;\n\n// check if the result is greater than 18\nif (result > 18) {\n    // return true if the result is greater than 20\n    msg.payload = true;\n} else {\n    // return false if the result is not greater than 20\n    msg.payload = false;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":140,"wires":[["34b030c9d560a730"]]},{"id":"34b030c9d560a730","type":"StatusUltimate","z":"542304b23c76704f","name":"Status","property":"payload","x":670,"y":140,"wires":[["e517aedb3ddb33a4"]]},{"id":"e517aedb3ddb33a4","type":"switch","z":"542304b23c76704f","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":790,"y":140,"wires":[["b6b691d2d3b1e3b2"],[]]},{"id":"b6b691d2d3b1e3b2","type":"delay","z":"542304b23c76704f","name":"","pauseType":"rate","timeout":"10","timeoutUnits":"minutes","rate":"1","nbRateUnits":"10","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":960,"y":140,"wires":[["21e971c3e87a82b5"]]},{"id":"14819af3.534ef5","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

I am going the replace the notification with some LEDs at the balcony door in the future.

1 Like