Triggering events based on multiple states on node red

Hello
I tried several ways to trigger automations on node-red based on the states read from a motion shelly sensor. This sensor has both motion and luminosity, which I want to use to determine if I should turn on the light.

The problem is that home-assistant saves and exposes those as different entities, so you can not read the whole information at once using the Event state node

As I said, I tried several ways and none of them work as they should:

  • Filtering the event through a current_state node. In this case, I get old values from the sensor on current_state and therefore the automation doesn’t work as expected
  • Using two Event state nodes that feed a batch node-red node (like on the screenshot below) and use the resulting message. The thing is that I not always get the latest value from both, leading me to the same problem as before

Is there any known and reliable way to do something like this? Like the official and good way of doing it?

I see other people is using approaches similar to the ones I listed, but as I said, they are not getting up to date values.

Thanks

1 Like

Could you post your flow please? the code?

what are the conditions you wan to apply?

What do you mean that you get old values? do you find one value in HA and another value in the current state node at a given time? or is it just that it takes it some time to update anywhere?

thanks

I didn’t put the code because I think the flow is simple enough to understand it from a screenshot. But anyway, here it is:

[{"id":"8e02bdc4.bd48d","type":"server-state-changed","z":"d326cf3e.06bee","name":"Cocina Light","server":"244e8d39.7b4e02","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.shelly_motion_luminosity","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"lt","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":130,"y":1480,"wires":[["2e0dfa21.cf30d6"]]},{"id":"a3220237.f1f4e","type":"server-state-changed","z":"d326cf3e.06bee","name":"Cocina Motion","server":"244e8d39.7b4e02","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.shelly_motion_motion","entityidfiltertype":"exact","outputinitially":true,"state_type":"habool","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":1420,"wires":[["2e0dfa21.cf30d6"]]},{"id":"2e0dfa21.cf30d6","type":"join","z":"d326cf3e.06bee","name":"Combined","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":370,"y":1460,"wires":[["b3d07634.390988"]]},{"id":"b3d07634.390988","type":"debug","z":"d326cf3e.06bee","name":"cocina_multi_sensor","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":620,"y":1460,"wires":[]},{"id":"244e8d39.7b4e02","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I mean that I get a value that is not the real current value from the sensor. I can’t tell if it is the value from a minute ago or 3 seconds ago. So, for example, imagine that I want to use this information to turn a light on only if the lux values are below a threshold (AKA is dark enough). It happens that, I enter the room and the lights does not turn on. Why? Because it is “reading” the values that the sensor was reporting when the lights were on.

I don’t know how this sensor (shelly motion sensor) reports to home assistant, but when it reports to mqtt it provides all the values it has on every motion event: lux and motion.

Thanks

I asked because I have a Philips hue motion sensor with a built-in light sensor that updates every 20 seconds, no matter what… So it’s not like NR is getting an old state, it’s the polling interval of the sensor.

EDIT

try using the homeassistant.update_entity service on that light sensor and check whether it updates when you do that. If so, calling that service before every current state node check should give you the updated state.

So you need the light to go on with motion but only if lux is below a certain level.

With every motion event the lux is updated but node red is processing too fast for the value to update.

Sounds like you need a wait node and maybe a delay to let the lux update. PS as soon as you see the mqtt message the value should be updated in HA.


[{"id":"58250570.941c24","type":"server-state-changed","z":"d6a46901.ebee1","name":"motion event","server":"6b1110b5.183a4","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":370,"y":120,"wires":[["4a3f6a7c.f10aac"]]},{"id":"cbcd40cc.88a65","type":"ha-wait-until","z":"d6a46901.ebee1","name":"wait a few seconds for lux","server":"6b1110b5.183a4","version":0,"outputs":2,"entityId":"","entityIdFilterType":"exact","property":"","comparator":"lt","value":"","valueType":"num","timeout":"2","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":770,"y":120,"wires":[[],[]]},{"id":"4a3f6a7c.f10aac","type":"delay","z":"d6a46901.ebee1","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":550,"y":120,"wires":[["cbcd40cc.88a65"]]},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

That matches some of my observations.
Right now, with the flow I posted above I’m getting the following reads:

nred-msg
{
  payload: {
    'sensor.shelly_motion_luminosity': 13,
    'binary_sensor.shelly_motion_motion': false
  },
nred-msg
{
  payload: {
    'sensor.shelly_motion_luminosity': 132,
    'binary_sensor.shelly_motion_motion': true
  },

As you can see, values are inverted: motion detected is false and the lux are low and lux is high and motion is detected. The expected values should be motion detected and low lux.
This is not controlled right now by the node-red flow, it is only reading the values. Right now, the shelly motion sensor calls the lights by itself, that’s why here we are seeing the values inverted: shelly reports the values and then turns the lights on, home assistant gets the right motion value, but the lux are out of sync, getting the previous value when the light was on (reported when motion detection was about to end).

I’ll try the delay method, to see if that works. Thanks for the idea.

What I am supposed to put on the Wait Until property?

Most likely state

@danielo515 did you ever get to the bottom of this?

Nope. I just stopped using node-red for my automation lights. It is too unreliable and hard to debug to rely on it for something as critical as lighting. I use a home assistant automation that seems to understand that it needs to wait if I put both triggers as conditions.