Power measurement only if the load is ON!

Hello folks, this is my 1st post :slight_smile:
I have looked around for days but I was not able to find useful suggestion.
Apparently I have a silly problem.
I own a water purifier and any time the water tap is opened an automation turn on the UV lamp which will kill possible bacteria.
This automation is because I don't want the UV always ON, as it was on the original system, to save electricity.
Now the issue. Sometimes, opening and closing repeatedly the tap, the system measure the power consumption when the lamp is OFF reporting that the lamp is broken.
This is a bug and I'm not able to read the sensor only when the LAMP is only ON!!!
I mean, I want to drop the measurement if the lamp is off.
How can I do it?
Ideally I would do something like:

//// pseudo code ////
var boolean isLampOn;
var power_consumption;
if (isLampOn) {
   wait 3 sec; // this waiting time is necessary to let the measurement be stable.
   power_consumption = read_sensor();
} else {
   return;
}
if (power_consumption < 5w ){
   send_alexa_alert('Please check UV lamp');
} else {
   return;
}
////////////////////

On Node red I made a complex flow, using even the gate node and playing with waiting time but still I-m not able to do it properly.
Here my flow:

[{"id":"c3a0f328cb151d3a","type":"tab","label":"Lampada UV","disabled":false,"info":"Verifica che l'assorbimento di potenza quando è accesa.\nNormalmente assorbe circa 12W.\nSe l'assorbimento è < 5W manda un messaggio telegram per avvisare di controllarne lo stato."},{"id":"52bf50a672470ef1","type":"api-call-service","z":"c3a0f328cb151d3a","name":"Notifica Telegram di verifica","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"telegram_bot","service":"send_message","entityId":"","data":"{\t   \"title\":\"Lampada UV:\",\t   \"message\": \"Assorbimento = \" &  msg.payload & \"W.\\nVerificare lo stato della lampada.\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":600,"y":420,"wires":[[]]},{"id":"162d12da4a6d6b82","type":"api-call-service","z":"c3a0f328cb151d3a","name":"Notifica Alexa di Verifica","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"notify","service":"alexa_media","entityId":"","data":"{\"message\":\"Verificare la Lampada del depuratore\",\"data\":{\"type\":\"announce\",\"method\":\"spoken\"},\"target\":\"media_player.echo_dot_cucina\"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":480,"wires":[[]]},{"id":"03db512d3e8c0892","type":"inject","z":"c3a0f328cb151d3a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":280,"wires":[["68da5a1212142c93"]]},{"id":"af5586fa2930987b","type":"delay","z":"c3a0f328cb151d3a","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":640,"y":200,"wires":[["77be12e4ec2ebbea"]]},{"id":"94f77e3830c366b7","type":"api-current-state","z":"c3a0f328cb151d3a","name":"Consumo Lamp 10 sec < 5W","server":"19ed11da.49f22e","version":3,"outputs":2,"halt_if":"5","halt_if_type":"num","halt_if_compare":"lt","entity_id":"sensor.consumo_lampada_uv","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":160,"y":540,"wires":[["52bf50a672470ef1","162d12da4a6d6b82","06eeebac93870e9d","fc4e62968c729957"],["06eeebac93870e9d","fc4e62968c729957","361e86823e56c07d"]]},{"id":"06eeebac93870e9d","type":"mqtt out","z":"c3a0f328cb151d3a","name":"","topic":"home/uvlamp/power","qos":"","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ae202893.c26c1","x":580,"y":540,"wires":[]},{"id":"fc4e62968c729957","type":"api-call-service","z":"c3a0f328cb151d3a","name":"Spegni UV dopo misura","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.lampada_uv","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":660,"wires":[[]]},{"id":"ac88cb78bb26b2fd","type":"api-call-service","z":"c3a0f328cb151d3a","name":"Manda MSG Ok","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"telegram_bot","service":"send_message","entityId":"","data":"{\t   \"title\":\"Lampada UV: controllo.\",\t   \"message\": \"Assorbimento = \" &  msg.payload & \"W\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":560,"y":600,"wires":[[]]},{"id":"32cb64055afc4461","type":"comment","z":"c3a0f328cb151d3a","name":"Detached mode","info":"Lo shelly lampada UV è configurato in detached mode per lasciare il controllo del relè esclusivamente a node-red","x":120,"y":40,"wires":[]},{"id":"89e844fd7ea09f3f","type":"mqtt in","z":"c3a0f328cb151d3a","name":"UV Input","topic":"shellies/shellyswitch25-8CAAB505B075/input/0","qos":"2","datatype":"auto","broker":"ae202893.c26c1","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":100,"wires":[["970591f51057ba35"]]},{"id":"e661b370d328f79d","type":"api-call-service","z":"c3a0f328cb151d3a","name":"UV Turn On","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.lampada_uv","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":650,"y":60,"wires":[[]]},{"id":"0975904219c72046","type":"switch","z":"c3a0f328cb151d3a","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":100,"wires":[["e661b370d328f79d","5835e4a0948f869c"],["af5586fa2930987b","4735085d467372d0"]]},{"id":"361e86823e56c07d","type":"debug","z":"c3a0f328cb151d3a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":360,"wires":[]},{"id":"970591f51057ba35","type":"rbe","z":"c3a0f328cb151d3a","name":"","func":"rbei","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":270,"y":100,"wires":[["0975904219c72046"]]},{"id":"0af3fc564e13f0ef","type":"comment","z":"c3a0f328cb151d3a","name":"Logga ultimo consumo","info":"Solo a scopo informativo loggo via mqtt l'ultimo consumo misurato.","x":820,"y":540,"wires":[]},{"id":"399d7d07cf4a3aac","type":"comment","z":"c3a0f328cb151d3a","name":"Filtro","info":"Lascia passare il messaggio mqtt solo se cambia stato.","x":270,"y":40,"wires":[]},{"id":"5b143bfafd654df3","type":"comment","z":"c3a0f328cb151d3a","name":"Spegni UV","info":"Dopo la misura spegni la lampada.","x":540,"y":720,"wires":[]},{"id":"68da5a1212142c93","type":"api-call-service","z":"c3a0f328cb151d3a","name":"UV Turn On","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.lampada_uv","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":250,"y":280,"wires":[[]]},{"id":"dccae971881b8b1a","type":"inject","z":"c3a0f328cb151d3a","name":"Turn ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":100,"y":160,"wires":[["0975904219c72046"]]},{"id":"831e1ee19ff37926","type":"inject","z":"c3a0f328cb151d3a","name":"Turn OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":100,"y":200,"wires":[["0975904219c72046"]]},{"id":"77be12e4ec2ebbea","type":"gate","z":"c3a0f328cb151d3a","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":830,"y":160,"wires":[["94f77e3830c366b7","361e86823e56c07d"]]},{"id":"5835e4a0948f869c","type":"change","z":"c3a0f328cb151d3a","name":"Close","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":120,"wires":[["77be12e4ec2ebbea"]]},{"id":"8a0d3f59fe39b4ac","type":"inject","z":"c3a0f328cb151d3a","name":"Close Gate","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Control","payload":"close","payloadType":"str","x":520,"y":300,"wires":[[]]},{"id":"4735085d467372d0","type":"change","z":"c3a0f328cb151d3a","name":"Open","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":160,"wires":[["77be12e4ec2ebbea"]]},{"id":"19ed11da.49f22e","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30},{"id":"ae202893.c26c1","type":"mqtt-broker","name":"home-assistant-mqtt-addon","broker":"192.168.1.11","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

ADMIN Note: Use triple backticks above and below flows and code to prevent forum formatting corrupting the code.

Welcome to the Forum.
please use Ctrl + E to post the flow code

Use the below link to see how it is done.

1 Like

If I understand correctly, you want to block sensor readings when the lamp is off. If that is correct then have a look at node-red-contrib-simple-gate. You can use the lamp on/off state to open and close the gate, allowing the sensor messages through when it is on, and blocking them when it is off.

I guess I did it!
I replaced the delay node with stoptimer node.
It seems to work now.
I will keep it monitored.
At the end, the forum is always inspiring :slight_smile:

Thank you all!

[{"id":"daef49b18b55d561","type":"tab","label":"Debug UV","disabled":false,"info":"","env":[]},{"id":"d28a19a86dfa97f7","type":"comment","z":"daef49b18b55d561","name":"Detached mode","info":"Lo shelly lampada UV è configurato in detached mode per lasciare il controllo del relè esclusivamente a node-red","x":120,"y":80,"wires":[]},{"id":"923a84501f0d55ae","type":"mqtt in","z":"daef49b18b55d561","name":"UV Input","topic":"shellies/shellyswitch25-8CAAB505B075/input/0","qos":"2","datatype":"auto","broker":"ae202893.c26c1","nl":false,"rap":true,"rh":0,"inputs":0,"x":100,"y":140,"wires":[["c23d6f8df7b4e2af"]]},{"id":"ebd10b090135e9f2","type":"comment","z":"daef49b18b55d561","name":"Filtro","info":"Lascia passare il messaggio mqtt solo se cambia stato.","x":270,"y":80,"wires":[]},{"id":"c23d6f8df7b4e2af","type":"rbe","z":"daef49b18b55d561","name":"","func":"rbei","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":270,"y":140,"wires":[["d2bc480b3a6a57a1"]]},{"id":"d2bc480b3a6a57a1","type":"switch","z":"daef49b18b55d561","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":140,"wires":[["71d14996a06fd42a"],["c5d46e1d1d19ab3c"]]},{"id":"71d14996a06fd42a","type":"api-call-service","z":"daef49b18b55d561","name":"UV Turn On","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.lampada_uv","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":550,"y":100,"wires":[["38c3a84ea6e067e2"]]},{"id":"c5d46e1d1d19ab3c","type":"stoptimer","z":"daef49b18b55d561","duration":"5","units":"Second","payloadtype":"num","payloadval":"999","name":"","x":560,"y":200,"wires":[["f1da7fe87e2a2a7e"],[]]},{"id":"f1da7fe87e2a2a7e","type":"api-current-state","z":"daef49b18b55d561","name":"Consumo Lamp 10 sec < 5W","server":"19ed11da.49f22e","version":3,"outputs":2,"halt_if":"5","halt_if_type":"num","halt_if_compare":"lt","entity_id":"sensor.consumo_lampada_uv","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":180,"y":460,"wires":[["390b4c5a49c0a4bf","566d4e3938132ccb","dc47fbbab0e97e14","53e1ad9036f65163"],["566d4e3938132ccb","dc47fbbab0e97e14"]]},{"id":"390b4c5a49c0a4bf","type":"api-call-service","z":"daef49b18b55d561","name":"Notifica Telegram di verifica","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"telegram_bot","service":"send_message","entityId":"","data":"{\t   \"title\":\"Lampada UV:\",\t   \"message\": \"Assorbimento = \" &  msg.payload & \"W.\\nVerificare lo stato della lampada.\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":620,"y":340,"wires":[[]]},{"id":"566d4e3938132ccb","type":"mqtt out","z":"daef49b18b55d561","name":"","topic":"home/uvlamp/power","qos":"","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ae202893.c26c1","x":600,"y":460,"wires":[]},{"id":"dc47fbbab0e97e14","type":"api-call-service","z":"daef49b18b55d561","name":"Spegni UV dopo misura","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.lampada_uv","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":610,"y":580,"wires":[[]]},{"id":"6974fb44fd1febab","type":"inject","z":"daef49b18b55d561","name":"Turn ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":100,"y":200,"wires":[["d2bc480b3a6a57a1"]]},{"id":"f12bbf7a9060bcc2","type":"inject","z":"daef49b18b55d561","name":"Turn OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":100,"y":240,"wires":[["d2bc480b3a6a57a1"]]},{"id":"edd45fe662c19c5c","type":"debug","z":"daef49b18b55d561","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":240,"wires":[]},{"id":"38c3a84ea6e067e2","type":"change","z":"daef49b18b55d561","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":100,"wires":[["c5d46e1d1d19ab3c"]]},{"id":"53e1ad9036f65163","type":"api-call-service","z":"daef49b18b55d561","name":"Notifica Alexa di Verifica","server":"19ed11da.49f22e","version":3,"debugenabled":false,"service_domain":"notify","service":"alexa_media","entityId":"","data":"{\"message\":\"Verificare la Lampada del depuratore\",\"data\":{\"type\":\"announce\",\"method\":\"spoken\"},\"target\":\"media_player.echo_dot_cucina\"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":610,"y":400,"wires":[[]]},{"id":"19941c7cf9619127","type":"comment","z":"daef49b18b55d561","name":"Spegni UV","info":"Dopo la misura spegni la lampada.","x":560,"y":640,"wires":[]},{"id":"3443a1b68afc894f","type":"comment","z":"daef49b18b55d561","name":"Logga ultimo consumo","info":"Solo a scopo informativo loggo via mqtt l'ultimo consumo misurato.","x":840,"y":460,"wires":[]},{"id":"ae202893.c26c1","type":"mqtt-broker","name":"home-assistant-mqtt-addon","broker":"192.168.1.11","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"19ed11da.49f22e","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

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