How to check dimmer state

How do I check the dimmer state of:

data.new_state.attributes.brightness

I can’t get is in a call service node… I need to distinct between a certain value or no. e.g. if dimmed to 150 then “true”

You can create a template sensor based on that attribute and then check the state of the sensor in an events: state node.

Hi thanks, the question was rather how to do this in node red… it was categorised in the node red section :grimacing:

Currently I tried this but I am not sure it is the correct way:

You are mostly doing all the right things, but you have an extra ‘data.new_state’ in there that you don’t need.

Try $entity().attributes.brightness = 255
Set ‘State Type’ to number

For the WebSocket nodes, we have several extra JSONata functions, including $entity(). This only works inside the nodes, and when called it returns an object containing the entity details. You can use this function anywhere you can use JSONata inside the node.

In the ‘Output properties’ section, there is also the extra ability to set msg.whatever to ‘entity’. This is exactly the same as using msg.whatever = $entity() in JSONata. By default, the node comes already set as msg.data = entity

The msg.data bit is only set for the output message. In the flow after the node, you get at the entity stuff using msg.data, Within the node, you only need your JSONata to be

$entity().state → state value
$entity().attributes → attributes object

therefore
$entity().attributes.brightness

for the value you want.

I would also add that, for the current state node, the return from $entity() is just the entity as it currently is. The Events: nodes (Events: state, Events: all) are special as they return both the current entity state using $entity() and the previous entity state using $prevEntity(). In these nodes (and only these nodes) you can set the output properties to ‘event data’ which bundles both current and previous into msg.data, using the ‘old_state’ and ‘new_state’ fields to nest both entity returns in the same data object.

Again, this is only for the Event nodes, and only for the output message, and only when setting to ‘event data’. Hence here you don’t need ‘new_state’.

Your ‘If State’ test is set to JSONata, so the J: expression should return a Boolean value so that the State condition is matched. You have a predicate expression, so this should work. When entity brightness is 255 it will be true.

The State Type is incorrectly set. This controls how the node picks up the state for the condition test and for output properties. As all states in HA are strings, this will turn the node state value to a Boolean, when it should be a number.
Edit - the state for a light is “on” or “off” and should be left as a string.

Setting State Type as number means that the incoming entity state is a string, but is converted by the node into a number, and this is what appears in msg.payload in the output properties when set to = ‘entity state’.

The confusion arises because, when the ‘If State’ is set to a condition (=, <, in, is etc) the right hand side must be a value for the condition to test against the entity state. If the condition results in true, then the node outputs a message, so if brightness was held in the entity state value, you would set the State Type to number, and use If State = 255. (you could also leave the state as string, and test If State = “255”)

When the ‘If State’ is set to JSONata, then the right hand side must be JSONata expression evaluating to a Boolean, and this has no implied connection with the entity state at all. You could have just “apples” = “oranges” and it would always return false, so the ‘If State’ test fails, and no output.

Sorry, while I was answering your question, I was thinking about an issue I had recently.

I was trying to find a way (in Node-RED) to check if an entity had one of its attributes in a value ‘x’ for ‘n’ minutes. The solution I found was the one I suggested in my previous message.

1 Like

image

OK, so I didn’t get it quite right…

You can try

$entity().attributes.brightness = "255"

or you can try

$number($entity().attributes.brightness) = 255

Edit:
I’m getting myself confused between the state, which will be a string for “on” or “off”, and the node needs to have State set to String (not number and not Boolean).

The JSONata code need to work with whatever the attribute.brightness is (I don’t have lights like this so I can’t check them myself).

I was probably right the first time :roll_eyes:

Set State Type to string. If the condition in ‘If State’ returns true, the node will send the message through the first output. And, if the condition returns false, through the second output.

EDIT: The payload of the message will be the state of the entity (on or off string), not the brightness value.

NaN

JSONata error

hmz…

this works :+1: