Compare two entities

I’m banging my head to no result trying to setup an automatism in node-RED to take some actions when my kids spend too much time in front of the TV. Essentially, I have a sensor providing the total screen time for today, and an input number in home assistant to define the maximum allowed time.

This automatism, if made in yaml, will just compare the two entities, like:
{{ states.sensor.screen_time_today.state > states.input_number.tv_max_hours.state }}

But i cannot find the way to implement it with node-RED.

Using events: state node, works perfectly if compared with a fix number, but cannot make it work with the input_number. I guess I should use the entity.

Any suggestion will be very much appreciated

You should put the entity that you are comparing to, and put state type number. I tested this and it doesn’t work for me, this was introduced last year but i never got to use it and is not in the docs yet

You can use a JSONata expression instead with the built in functions of the node

Select jsonata in ‘if state’, and then expression to the right field and field this statement, is a simple if/else

$number($entity().state) > $number($entities('input_number.tv_max_hours').state) ? true : false

Maybe @Kermit can give some hint on ‘entity.’ on how to use it properly

1 Like

entity and prevEntity are references to the entity that triggered the node. Basically in this case allowing you to compare two properties of the trigger entity.

You could shorten your example to

image

5 Likes

Sorry the confusion I was referring to the drop down menu “entity.” As he shows in the ss.

Awesome, it’s working perfectly. Thanks!

Thanks @Kermit, I like the simplicity of your alternative. Clearly I have to get stronger in JSONata.

I think I understood what you were asking but probably didn’t do a good job explaining it. You would use it to check the state of the entity against another property of the entity like entity.attributes.temperature

image

1 Like

Everything good then. I’ll see if I can pr the docs for this feature

Since a few days I try to achieve the same, but I get stuck somewhere. First I of all ma my journey into solving this topic started on the wrong forum (node-red). The explained solutions there didn’t work for HA entities for me, because they are using msg.topic to transfer they payloads. The described solution here seems to be pretty obvious, but somewhere I didn’t catch the train. The goal is pretty much the same as the one from @fxreina, I need to compare a temperature with a temp setpoint (as input_number). But my temp is single entity.


The content of the node looks as this.

But I don’t get any output on the debug screen?
Note: Node-red is pretty new to me, so I may miss something obvious.

Hi @lucode, have a closer look to the icon preceeding the formula in your screenshot and compare with the one that worked for me. I believe that’s the problem, you should change it from a numeric value into to a jsonata.

THX @fxreina indeed the small things need also to be taken care of.

Hi all,

Trying to do something similar - the above has been really helpful and I’ve actually found two slightly different ways of doing it which I’m keeping in my back pocket

So I have the comparison working, but what I really would like to do is actually output the differential between the two values on the payload…

Ignoring the code in the custome message box (as it’s psuedo guff at the moment as a placeholder for what I want to do), I assume/hope/maybe think that since it’s already comparing it is possible to take the two values (entity.state and state of other sensor) to output on the message?

Can anyone advise how I might do this?

EDIT: Also, fwiw I do have a working version of this, but I was just trying to clean it up… here is the working version:

[{"id":"aeafd804e5662aa9","type":"debug","z":"df5b0eaf65475057","name":"debug 15","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":660,"wires":[]},{"id":"ca2ed22e98ff14ee","type":"trigger-state","z":"df5b0eaf65475057","name":"Shower Room Humidity Changed and is > reference","server":"3aa13fed.bf745","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.shower_room_sensor_humidity","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"entity_id","targetValue":"sensor.reference_landing_sensor_humidity","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"<","comparatorValueDatatype":"entity","comparatorValue":"state"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"num","enableInput":false,"x":230,"y":620,"wires":[["d61f40ad57792408","976301a2d4146ba9"],["aeafd804e5662aa9"]]},{"id":"d61f40ad57792408","type":"api-current-state","z":"df5b0eaf65475057","name":"Reference Humidity","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.reference_landing_sensor_humidity","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"low","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":550,"y":560,"wires":[["84e4b437d693d1ea"]]},{"id":"976301a2d4146ba9","type":"api-current-state","z":"df5b0eaf65475057","name":"Shower Room Humidity Value","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.shower_room_sensor_humidity","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"high","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":590,"y":600,"wires":[["84e4b437d693d1ea"]]},{"id":"95f4c30e81f9d06c","type":"function","z":"df5b0eaf65475057","name":"","func":"msg.topic = \"diff\";\nmsg.payload = msg.payload.high - msg.payload.low;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":880,"y":560,"wires":[["9ff78f5999eb3754"]]},{"id":"84e4b437d693d1ea","type":"join","z":"df5b0eaf65475057","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":750,"y":560,"wires":[["95f4c30e81f9d06c"]]},{"id":"9ff78f5999eb3754","type":"switch","z":"df5b0eaf65475057","name":">15","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"15","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1010,"y":560,"wires":[["0a6943fbfc8ba808"],["fb7868f58d703110"]]},{"id":"fb7868f58d703110","type":"debug","z":"df5b0eaf65475057","name":"No need for Fan","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1180,"y":580,"wires":[]},{"id":"3aa13fed.bf745","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]
1 Like

Hi.
Is possible to add some value value to entity?
I need to incrase value of compared humidity.