Moving OpenHAB rules to Node Red

Hey all,

First post as I’m migrating from OpenHAB to HA + NodeRed. Loving it so far but stuck on getting an automation I had running on OpenHAB doing the same in NodeRed.

I use a Sonoff POW R2 to monitor our washing machine power usage to alert when it has finished its cycle. My OpenHAB rule went like this…

// Washing Machine States
val Number MODE_OFF = 0
val Number MODE_STANDBY = 1
val Number MODE_ACTIVE = 2
val Number MODE_FINISHED = 3

rule "Washing Machine Consumption State"
when
	Item WashingMachine_Power changed
then 
	if (WashingMachine_Power.state < 0.7) {
		WashingMachine_OpState.postUpdate(MODE_OFF)
	}
    else if (WashingMachine_Power.state > 10) {
		WashingMachine_OpState.postUpdate(MODE_ACTIVE)
	}
    else if (WashingMachine_Power.state < 2.5) {
        if (WashingMachine_OpState.state == MODE_OFF) {
			WashingMachine_OpState.postUpdate(MODE_STANDBY)
		}
        else if (WashingMachine_OpState.state == MODE_ACTIVE) {
			WashingMachine_OpState.postUpdate(MODE_FINISHED)
		}
    }
end

So basically when the power shifted from being ACTIVE to FINISHED I would get an alert.

Can anyone suggest a way of monitoring my “sensor.washingmachine_energy_power” so that when it moves from > 10 to < 10 for a period of time I can then send a telegram alert?

Trying the following but I suspect that because the power usage “bounces” it needs to be monitored over a couple of minutes…

[{"id":"77463abea3de96cf","type":"trigger-state","z":"df447a6efee3d089","name":"Moves from Running To Standby","server":"ae820eaf.1b0e8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.washingmachine_energy_power","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<","comparatorValueDatatype":"str","comparatorValue":"10","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","comparatorType":">","comparatorValueDatatype":"str","comparatorValue":"10","propertyValue":"old_state.state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":250,"y":280,"wires":[["048ab50663283aff"],[]]},{"id":"ae820eaf.1b0e8","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}]

Any assistance greatly appreciated…

Cheers, Tim

Here is an example of getting the average of 10 values.
You change it in the join node.

[{"id":"e47bef34190fee8d","type":"function","z":"ebaa69a9.649708","name":"","func":"const sum = msg.payload.reduce((a, b) => a + b, 0);\nconst avg = (sum / msg.payload.length) || 0;\n\nmsg.sum = sum;\nmsg.avg = avg;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":760,"wires":[["322d53240ba1e965"]]},{"id":"72b1659a7d2ef096","type":"join","z":"ebaa69a9.649708","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"60","count":"10","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":760,"wires":[["e47bef34190fee8d"]]},{"id":"322d53240ba1e965","type":"switch","z":"ebaa69a9.649708","name":"","property":"avg","propertyType":"msg","rules":[{"t":"btwn","v":"0.7","vt":"num","v2":"2.5","v2t":"num"},{"t":"gt","v":"10","vt":"str"},{"t":"lt","v":"0.7","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":770,"y":760,"wires":[["11a179ff2a2ae21b"],["88d84b7acbf82b05"],["3190d2db8ff7a12a"]]},{"id":"5098fba4938f3b00","type":"inject","z":"ebaa69a9.649708","name":"0.5","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.5","payloadType":"num","x":230,"y":680,"wires":[["72b1659a7d2ef096"]]},{"id":"1db1571e9244caca","type":"inject","z":"ebaa69a9.649708","name":"1.5","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.5","payloadType":"num","x":230,"y":760,"wires":[["72b1659a7d2ef096"]]},{"id":"8ca0100a75e95be4","type":"inject","z":"ebaa69a9.649708","name":"15","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"15","payloadType":"num","x":230,"y":840,"wires":[["72b1659a7d2ef096"]]},{"id":"88d84b7acbf82b05","type":"debug","z":"ebaa69a9.649708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":760,"wires":[]},{"id":"11a179ff2a2ae21b","type":"debug","z":"ebaa69a9.649708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":700,"wires":[]},{"id":"3190d2db8ff7a12a","type":"debug","z":"ebaa69a9.649708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":820,"wires":[]}]

Don’t you mean this? Note the for clause:

[{"id":"a0f651ee.4422f","type":"server-state-changed","z":"937ef78.ec6b408","name":"energy","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.washingmachine_energy_power","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"10","halt_if_type":"num","halt_if_compare":"lt","outputs":2,"output_only_on_state_change":true,"for":"2","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":250,"y":740,"wires":[["575efb8d.159624"],[]]},{"id":"575efb8d.159624","type":"api-call-service","z":"937ef78.ec6b408","name":"Notify","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":400,"y":740,"wires":[[]]},{"id":"9405c3fe.d0a6c","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}]

Disregard the last message, I believe I misunderstood what you needed. Set the period of time you want whithin the trigger node, purplish one.

[{"id":"5dc82bc5.65a7d4","type":"trigger-state","z":"dd367079.a83e7","name":"Moves from Running To Standby","server":"ae820eaf.1b0e8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.washingmachine_energy_power","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<=","comparatorValueDatatype":"str","comparatorValue":"10","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","comparatorType":">","comparatorValueDatatype":"str","comparatorValue":"10","propertyValue":"old_state.state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":230,"y":540,"wires":[["13be03b.06d07fc"],[]]},{"id":"516e1081.72107","type":"trigger-state","z":"dd367079.a83e7","name":"Moves from Standby To Running","server":"ae820eaf.1b0e8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.washingmachine_energy_power","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":">","comparatorValueDatatype":"str","comparatorValue":"10","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","comparatorType":"<=","comparatorValueDatatype":"str","comparatorValue":"10","propertyValue":"old_state.state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":230,"y":600,"wires":[["4d51f884.a23908"],[]]},{"id":"4d51f884.a23908","type":"change","z":"dd367079.a83e7","name":"Set stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":600,"wires":[["13be03b.06d07fc"]]},{"id":"13be03b.06d07fc","type":"trigger","z":"dd367079.a83e7","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"2","extend":false,"overrideDelay":false,"units":"min","reset":"stop","bytopic":"all","topic":"topic","outputs":1,"x":640,"y":540,"wires":[["45c22214.0a770c"]]},{"id":"45c22214.0a770c","type":"api-call-service","z":"dd367079.a83e7","name":"Notify","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":790,"y":540,"wires":[[]]},{"id":"ae820eaf.1b0e8","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":"9405c3fe.d0a6c","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}]

Problem with the last solution I posted was that if your sensor reading moved from 9 to 8, it would also start the countdown. You want to start the countdown only when it goes from above 10 to below 10 and viceversa.

1 Like

I see what you’ve done here. Thanks, I’ll give it a shot.