Filter values from sensor

Hi,

I am pulling data from an Alpha ESS inverter, using a custom node in Node Red. The values are polled by the node and populate sensors which i display in HA.

What I want to do however is create 2 new sensors, which record wattage (imported) and wattage (exported). This is represented by the node as either a positive value (200) or a negative value (-200). This part works as it should.

The issue is that the “less than” value when used in HA will work backwards, it shows as a negative value as the inverter has 1 CT. I would like to take this “less than” value and change it to a positive number, then, if inverter is exporting, the import should send 0 to the import sensor. If the inverter is importing, it should be send 0 to the export sensor.

Here is what I have so far:

[{"id":"c99cbbb8e48d5306","type":"tab","label":"Battery Storage","disabled":false,"info":"","env":[]},{"id":"aac032fbea181ea5","type":"ha-entity","z":"c99cbbb8e48d5306","name":"Grid Feed","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_grid_feed"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.grid","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":320,"y":100,"wires":[[]]},{"id":"7209d3d7fda1196b","type":"ha-entity","z":"c99cbbb8e48d5306","name":"Battery State of Charge","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_soc"},{"property":"device_class","value":"battery"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"%"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.battery.soc","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":370,"y":140,"wires":[[]]},{"id":"079216b83270ba48","type":"ha-entity","z":"c99cbbb8e48d5306","name":"Battery Load","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_load"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":330,"y":180,"wires":[[]]},{"id":"1ba9b87e8c963f3b","type":"ha-entity","z":"c99cbbb8e48d5306","name":"House Consumption","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_house_consumption"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.consumption","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":360,"y":60,"wires":[[]]},{"id":"c7ffecec2bda02eb","type":"change","z":"c99cbbb8e48d5306","name":"Battery Load","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.battery.load","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":240,"wires":[["ebc22e19088a3f4e"]]},{"id":"ebc22e19088a3f4e","type":"switch","z":"c99cbbb8e48d5306","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"str"},{"t":"lte","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":520,"y":240,"wires":[["feb337d065f2db13"],["0b50b7e24846053b"]]},{"id":"0b50b7e24846053b","type":"ha-entity","z":"c99cbbb8e48d5306","name":"b3_power_imported","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_power_imported"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":740,"y":260,"wires":[[]]},{"id":"feb337d065f2db13","type":"ha-entity","z":"c99cbbb8e48d5306","name":"b3_power_exported","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_power_exported"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":740,"y":220,"wires":[[]]},{"id":"2278bc1a32e13768","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 ideas?

Thanks.

Use a function node:

[{"id":"d1206ab20da16bde","type":"ha-entity","z":"14ca354715bc92f3","name":"b3_power_exported","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_power_exported"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"msg.payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":460,"y":480,"wires":[[]]},{"id":"8722f1f7d7d5e4c6","type":"function","z":"14ca354715bc92f3","name":"","func":"if(msg.payload.battery.load > 0){\n    return [msg, null];\n}else{\n    msg.payload.battery.load = Math.abs(msg.payload.battery.load);\n    return [null, msg];\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":220,"y":500,"wires":[["d1206ab20da16bde"],["e03a90d1f725be4b"]]},{"id":"e03a90d1f725be4b","type":"ha-entity","z":"14ca354715bc92f3","name":"b3_power_imported","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_power_imported"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"msg.payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":460,"y":520,"wires":[[]]},{"id":"2278bc1a32e13768","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
if(msg.payload.battery.load > 0){
    return [msg, null];
}else{
    msg.payload.battery.load = Math.abs(msg.payload.battery.load);
    return [null, msg];
}

If I understand correctly this will set the other sensor to zero, depending on import/export. Also change the negative value to positive.

[{"id":"c7ffecec2bda02eb","type":"change","z":"c99cbbb8e48d5306","name":"Battery Load","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.battery.load","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":240,"wires":[["ebc22e19088a3f4e"]]},{"id":"ebc22e19088a3f4e","type":"switch","z":"c99cbbb8e48d5306","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"0","vt":"str"},{"t":"lte","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":520,"y":240,"wires":[["c0f536f3029c5647","6db211610bb23bd0"],["0b50b7e24846053b","5e3e39fa2456cc2f"]]},{"id":"0b50b7e24846053b","type":"ha-entity","z":"c99cbbb8e48d5306","name":"b3_power_imported","server":"","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_power_imported"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":980,"y":340,"wires":[[]]},{"id":"feb337d065f2db13","type":"ha-entity","z":"c99cbbb8e48d5306","name":"b3_power_exported","server":"","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_power_exported"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":980,"y":120,"wires":[[]]},{"id":"c0f536f3029c5647","type":"change","z":"c99cbbb8e48d5306","name":"Change value positive","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload * -1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":120,"wires":[["feb337d065f2db13"]]},{"id":"6db211610bb23bd0","type":"change","z":"c99cbbb8e48d5306","name":"Set import to 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":180,"wires":[["0b50b7e24846053b"]]},{"id":"5e3e39fa2456cc2f","type":"change","z":"c99cbbb8e48d5306","name":"Set export to 0","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":240,"wires":[["feb337d065f2db13"]]}]
1 Like

Thanks, this works! However when the inverter goes from discharging to charging, it does not set the value of the other sensor back to 0, it remains at the last value.

When the inverter switches to charge mode, the import sensor needs to read 0 (the same the other way around).

[{"id":"c99cbbb8e48d5306","type":"tab","label":"Battery Storage","disabled":false,"info":"","env":[]},{"id":"aac032fbea181ea5","type":"ha-entity","z":"c99cbbb8e48d5306","name":"Grid Feed","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_grid_feed"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.grid","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":320,"y":100,"wires":[[]]},{"id":"7209d3d7fda1196b","type":"ha-entity","z":"c99cbbb8e48d5306","name":"Battery State of Charge","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_soc"},{"property":"device_class","value":"battery"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"%"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.battery.soc","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":370,"y":140,"wires":[[]]},{"id":"079216b83270ba48","type":"ha-entity","z":"c99cbbb8e48d5306","name":"Battery Load","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_load"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":330,"y":180,"wires":[[]]},{"id":"1ba9b87e8c963f3b","type":"ha-entity","z":"c99cbbb8e48d5306","name":"House Consumption","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_house_consumption"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload.consumption","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":360,"y":60,"wires":[[]]},{"id":"c7ffecec2bda02eb","type":"change","z":"c99cbbb8e48d5306","name":"Battery Load","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.battery.load","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":220,"wires":[[]]},{"id":"d1206ab20da16bde","type":"ha-entity","z":"c99cbbb8e48d5306","name":"B3 Battery Discharge","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_discharge"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"msg.payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":560,"y":240,"wires":[[]]},{"id":"8722f1f7d7d5e4c6","type":"function","z":"c99cbbb8e48d5306","name":"","func":"if(msg.payload.battery.load > 0){\n    return [msg, null];\n}else{\n    msg.payload.battery.load = Math.abs(msg.payload.battery.load);\n    return [null, msg];\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":260,"wires":[["d1206ab20da16bde"],["e03a90d1f725be4b"]]},{"id":"e03a90d1f725be4b","type":"ha-entity","z":"c99cbbb8e48d5306","name":"B3 Battery Charge","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_charge"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"msg.payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":550,"y":300,"wires":[[]]},{"id":"2278bc1a32e13768","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"}]

Thanks.

There was an issue with the change of negative to positive, on the wrong sensor - swapped and now works perfectly! Thanks.

1 Like

I didn’t see that as a requirement from your post.
Perhaps this works:

[{"id":"ddc6a13bcc6af3cb","type":"ha-entity","z":"8fcb391d52de1280","name":"B3 Battery Discharge","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_discharge"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"msg.payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":680,"y":140,"wires":[[]]},{"id":"9d8fcb5aa2e7a56c","type":"function","z":"8fcb391d52de1280","name":"","func":"if(msg.payload.battery.load > 0){\n    msg.positive = true;\n    return [msg, null];\n}else{\n    msg.positive = false;\n    msg.payload.battery.load = Math.abs(msg.payload.battery.load);\n    return [null, msg];\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":260,"wires":[["ddc6a13bcc6af3cb","51a8ba0eb1b38dd4"],["c80692ed412910db","51a8ba0eb1b38dd4"]]},{"id":"c80692ed412910db","type":"ha-entity","z":"8fcb391d52de1280","name":"B3 Battery Charge","server":"2278bc1a32e13768","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"b3_battery_charge"},{"property":"device_class","value":"power"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"W"},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"msg.payload.battery.load","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":670,"y":380,"wires":[[]]},{"id":"51a8ba0eb1b38dd4","type":"function","z":"8fcb391d52de1280","name":"","func":"if(msg.postitve == false){\n    msg.payload.battery.load = 0;\n    return [msg, null];\n}else{\n    msg.payload.battery.load = 0;\n    return [null, msg]\n}\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":260,"wires":[["ddc6a13bcc6af3cb"],["c80692ed412910db"]]},{"id":"2278bc1a32e13768","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]