Best way to create conditional true/false outputs

Hi there,

I am trying to set up some things in Node RED for which I need nodes to conditionally output true or false. My current solution for e.g. a numerical threshold check is this trigger node:

But I find it kind of bloated, is there any easier way? I also tried it with state changed node, but there, I am struggling to output either true or false based on the conditions, as I have to hardcode the boolean output:

Any easier way to achieve this?

Use JSONata ?

This ‘language’ is available throughout Node-RED, and has several enhancement for the WebSocket nodes. It is a declarative-functional language, and most definitely not a prescriptive one, however basic string, number, and logic processing follow JavaScript structures (on which JSONata runs).

JSONata can be used anywhere you see J: (expression) in the drop down selector. In the WebSocket nodes (only) you can use the function call $entity() to refer to the subject of the node, and $entities(‘sensor.my_sensor’) to refer to any entity.

In the Event: state node, or the Current State node, you can create/use an Output property (as you have) to set any message field, using JSONata.

JSONata executes an expression, and returns the result, so we just need

$entity().state > "50"

As long as you have selected J: this will return the entity object for the subject of the node (your TEST Number), pick the state value, and perform a logic test, returning Boolean true or false accordingly.

Note that I am assuming that your entity state is a string. If it is a number, then you have to compare with a number. These nodes have a ‘State Type’ field, which performs pre-processing on the state value to make strings into numbers, so it is often worth just using $entity() in the output first to see exactly what the state and attributes are.

As an extra, you can also do this whilst working with a completely different entity. Since the Current state / Event: state etc nodes need an entity (the subject), you may be working with (eg triggering off) something else, so

$entities('sensor.test_number').state > 50

will return any entity state you wish, and it becomes possible with a bit of coding to perform quite a lot of things that would otherwise require multiple Current State nodes.