Action only when changing specifically from not_home to home

I want to do this

The idea is that when I arrive at home at night and I open the door, the livingroom light should turn on. But what happens when I’m already home and I open the door? I don’t want the livingroom light to turn on again.

So the light should turn on only when my phone state changes from not_home to home.

For some reason I found it very difficult to do this in node-red.

Any guidance wil help.

Most likely the state is already home when you open the door.
You need to check when you arrived home instead using current state and when the last state change happened

Use the device tracker to trigger the flow and then wait for the door to open.

[{"id":"f36bee7a6dfde5c5","type":"trigger-state","z":"8e6ef4f6cbb13d83","name":"","server":"","version":5,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["device_tracker.dahua"],"substring":[],"regex":[]},"debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"not_home"},{"targetType":"entity_id","targetValue":"sun.sun","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"below_horizon"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":false,"x":340,"y":2310,"wires":[["33d574a172e5ac4e"],[]]},{"id":"33d574a172e5ac4e","type":"ha-wait-until","z":"8e6ef4f6cbb13d83","name":"","server":"","version":3,"outputs":2,"entities":{"entity":["binary_sensor.door"],"substring":[],"regex":[]},"property":"state","comparator":"is","value":"on","valueType":"str","timeout":"300","timeoutType":"num","timeoutUnits":"seconds","checkCurrentState":true,"blockInputOverrides":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"date"}],"x":620,"y":2310,"wires":[[],[]]},{"id":"dcbc2b2e8ca85eb9","type":"global-config","env":[],"modules":{"node-red-contrib-home-assistant-websocket":"0.80.3"}}]

Edited to add sun condition to trigger.