Troubleshooting an entity state "Changing back"

So, I’m working on using an email trigger to set an entity state, then setting it BACK afterwards. I only get one email, “Door open”, for example, and need to tell HA that the door is CLOSED afterwards.
I’m using the imap_content_sensor to set the state of the entity, then a python script to change it back. … Then, on it’s own, it’s changing BACK to the imap-state. I’ve got some logs as an example but I cannot for the life of me figure out where to go next.

*** Python script runs to set state of entity: ***

2020-01-02 12:48:55 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1782192304] Received {'type': 'call_service', 'domain': 'python_script', 'service': 'sensor_set_state', 'service_data': {'entity_id': 'sensor.email_for_test', 'state': 'clear'}, 'id': 33}

2020-01-02 12:48:55 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=python_script, service=sensor_set_state, service_data=entity_id=sensor.email_for_test, state=clear>

2020-01-02 12:48:55 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1819085584] Sending {'id': 166, 'type': 'event', 'event': {'event_type': 'call_service', 'data': {'domain': 'python_script', 'service': 'sensor_set_state', 'service_data': {'entity_id': 'sensor.email_for_test', 'state': 'clear'}}, 'origin': 'LOCAL', 'time_fired': datetime.datetime(2020, 1, 2, 20, 48, 55, 964301, tzinfo=<UTC>), 'context': {'id': 'b7e64775c52b42a2b85bc267e5337087', 'parent_id': None, 'user_id': '5a1f5d8ae9884f948766bb849a2aa7b4'}}}

2020-01-02 12:48:56 INFO (SyncWorker_3) [homeassistant.components.python_script] Executing sensor_set_state.py: {'entity_id': 'sensor.email_for_test', 'state': 'clear'}

*** Python script fires an event for logging (senor_set_state_fired) which appears in logs: ***

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event sensor_set_state_fired[L]: entity=sensor.email_for_test, old_state=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended, new_state=clear>

*** Bus event for state_changed shows state is now "clear" as expected ***

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>

*** The next two events on the api both have the same ID, but different time-fiired and different context IDs ***

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1819085584] Sending {'id': 166, 'type': 'event', 'event': {'event_type': 'sensor_set_state_fired', 'data': {'entity': 'sensor.email_for_test', 'old_state': 'motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended', 'new_state': 'clear'}, 'origin': 'LOCAL', 'time_fired': datetime.datetime(2020, 1, 2, 20, 48, 56, 5086, tzinfo=<UTC>), 'context': {'id': 'd314423f8798499f9579bcc0770a17a0', 'parent_id': None, 'user_id': None}}}


2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1819085584] Sending {'id': 166, 'type': 'event', 'event': {'event_type': 'state_changed', 'data': {'entity_id': 'sensor.email_for_test', 'old_state': <state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, 'new_state': <state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>}, 'origin': 'LOCAL', 'time_fired': datetime.datetime(2020, 1, 2, 20, 48, 56, 13378, tzinfo=<UTC>), 'context': {'id': '8745e9c81b744a718f97686f98e48a30', 'parent_id': None, 'user_id': None}}}


***** Now for a bunch of repeated event state_changed entries that all have ID 2, and no contextID property *****

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1818660112] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}


2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1817740272] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}


2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1782923184] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1893015280] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1882212048] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1780315632] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1781282992] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1781282992] Sending {'id': 22, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1782192304] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:47:37.362602-08:00>, new_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>>}

***** End of repeated event state_changed entries *****

2020-01-02 12:48:56 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1782192304] Sending {'id': 33, 'type': 'result', 'success': True, 'result': {'context': Context(user_id='5a1f5d8ae9884f948766bb849a2aa7b4', parent_id=None, id='b8d143686f4642f39a51250e3595d6d1')}}


***** NOW it's back and not clear anymore *****

2020-01-02 12:49:00 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>

2020-01-02 12:49:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1819085584] Sending {'id': 166, 'type': 'event', 'event': {'event_type': 'state_changed', 'data': {'entity_id': 'sensor.email_for_test', 'old_state': <state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, 'new_state': <state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>}, 'origin': 'LOCAL', 'time_fired': datetime.datetime(2020, 1, 2, 20, 49, 0, 939526, tzinfo=<UTC>), 'context': {'id': '36d5449ef8994aaa9ce3c329786cb7ce', 'parent_id': None, 'user_id': None}}}

2020-01-02 12:49:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1818660112] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1817740272] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1782923184] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1893015280] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:00 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1882212048] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:01 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1780315632] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:01 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1781282992] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:01 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1781282992] Sending {'id': 22, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}

2020-01-02 12:49:01 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1782192304] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=sensor.email_for_test, old_state=<state sensor.email_for_test=clear @ 2020-01-02T12:48:56.013181-08:00>, new_state=<state sensor.email_for_test=motion_detected - -42.03309106826782, the subject is: Re: RandomString. 1 minute ago is: 1577997279.966086. Right now is: 2020-01-02 12:35:39.970034-08:00 --- , DateDiff true, subject true, subjectIf ended, dateIf ended; [email protected], subject=Re: RandomString, date=Thu, 2 Jan 2020 12:35:22 -0800, body=, friendly_name=email_for_test @ 2020-01-02T12:49:00.939212-08:00>>}