Regular Expression in Change Node Not Working

I have created a flow to send an e-mail when a battery level is lower than a certain percentage. I used a Change node:

The Change node gets a msg.payload from a State-Change node for a battery level, which works (e.g., msg.payload = 81 when the battery level is at 81%). I basically want to search/replace the number in msg.payload using a regular expression from:

"81"

to

"Front door lock battery level is below 50% ($1%). Please replace or recharge."
where $1 should be the "81" or whatever level is being received.

However, the message isn't being replaced (as shown from the outputs of the Debug nodes):

I have tried:

  • ^(.*)$
  • [0-9]*
  • [0-9][0-9]

BTW, if I use "Set" instead of "Change", it works to change it completely.

not being a regex expert (I'm not even a novice, it makes my brain hurt)
Why not do this:
the full text of the string is:
Front door lock battery level is below 50% (xx%). Please replace or recharge.


simple and easy for others to understand in 6 months.

Yes, that works, so I've learned a different approach. However, I am trying to use this as a stepping stone for harder expressions. I'm new to regEx, but it's like learning a new language--eventually it won't look like Greek (or regEx), haha.

2 Likes

I think the issue you came across is that payload 81 in a number not a string.

Try injecting "81" as a string as a test.

Why not just use a template node instead ?

This sounds like a bug to me? Haven't used capture groups yet either so can't be sure of the syntax in this case but it seems to be partly working as 81 is there.

@gctseng note that you have set the "replace with" (=output) as environment variable, not as string.

But looking further;

  • if input is a number, regex does not work
  • if input is a string, regex works

Screenshot 2020-08-07 at 07.32.22

Perhaps @knolleary knows how this behaves in the background, looks like regex is not treating the input as equal - or the payload type does not change, only its content.

2 Likes

Like @ristomatti said, this looks like a bug. I cannot get it to work with numeric inputs.

Not a bug as such - regular expressions only work on Strings. Whether the node should stringify non-string input is something to discuss as an enhancement to the node.

For the original example, the Template node would be a better fit, or a JSONata expression.

1 Like

@bakman2 Tried it, but it still doesn't seem to work.

[{"id":"a119360.8311ec8","type":"server-state-changed","z":"b4a77169.bab4f8","name":"Front Door Lock Battery %","server":"61613ba9.2caa64","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.front_door_lock_be469_battery_level","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":130,"y":720,"wires":[["3e149e54.6821ea","413a0873.0dea28"]],"icon":"font-awesome/fa-battery-2"},{"id":"3e149e54.6821ea","type":"debug","z":"b4a77169.bab4f8","name":"Battery Level","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":350,"y":780,"wires":[]},{"id":"e48f2f2b.08562","type":"debug","z":"b4a77169.bab4f8","name":"Change Msg to Create E-mail","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":650,"y":720,"wires":[]},{"id":"413a0873.0dea28","type":"change","z":"b4a77169.bab4f8","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(.*)$","fromt":"re","to":"Front door lock battery level is below 50% $1  %).  Please replace or recharge.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":720,"wires":[["e48f2f2b.08562"]]},{"id":"61613ba9.2caa64","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":false}]

Your input is still numeric, which does not work. Use a template node instead.

Thanks. I figured it was something like that.

BTW, when I set the state type to be a string on the first node, everything works as expected. Regardless, I guess I've got some learning to do with template nodes and JSON expressions

[{"id":"b4d57cfc.84d048","type":"server-state-changed","z":"50672f44.f293b8","name":"Front Door Lock Battery %","server":"61613ba9.2caa64","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.front_door_lock_be469_battery_level","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"50","halt_if_type":"num","halt_if_compare":"lt","outputs":2,"output_only_on_state_change":false,"x":130,"y":720,"wires":[["5f06c63d.7cd2c8","519ff7fc.30dbe"],["5f06c63d.7cd2c8"]],"icon":"font-awesome/fa-battery-2"},{"id":"5f06c63d.7cd2c8","type":"debug","z":"50672f44.f293b8","name":"Battery Level","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":350,"y":760,"wires":[]},{"id":"519ff7fc.30dbe","type":"change","z":"50672f44.f293b8","name":"Create e-mail message.","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(.*)$","fromt":"re","to":"Battery level is below 50% ($1%). Replace/recharge.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":720,"wires":[["464a6de3.ddc964"]]},{"id":"464a6de3.ddc964","type":"debug","z":"50672f44.f293b8","name":"Change Msg to Create E-mail","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":670,"y":760,"wires":[]},{"id":"61613ba9.2caa64","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":false}]

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