Separating event data from async_track_state_change_event

I am trying to split up the data in _event from async_track_state_change_event so I can use it, and found I can use _event.data[“X”] to get entity_id, new_state or old_state.
However, new_state and old_state are another class, <class ‘homeassistant.core.State’>, and I can’t figure out how to split up <state binary_sensor.door_sensor=off; device_class=door, icon=mdi:door, friendly_name=Door Sensor @ 2026-01-01T00:00:00.000000+00:00>, because everything I try gives an error message, “X” is not a known attribute of “None”, or similar.
How do I use it despite it treating the class as None?

Post your python code. If it’s not python, post your automation.

anomaly-here/custom_components/anomaly_here/init.py at main · DewStep/anomaly-here

lines roughly 170-200 are me trying to figure out how to extract useful information, and what I’m asking about.
I can separate _event with _event.data[“new_state”], but when I try that with the resulting class, vscode just gives an error.

I have no idea what your vscode error is talking about, but all you’d need to do to assert that you have old and new states is

    async def activity_noticed(self, _event: Event[EventStateChangedData]) -> None:
        """
        Restart inactivity timer.

        Parameters
        ----------
        event : Event[EventStateChangedData]
            The event that triggered the callback.

        """
        new_state: State | None
        if (new_state := _event.data["new_state"]) is None:
           return

Then do all the rest of your code using new_state, you won’t need to get the entity_id because it’s a property on the state object.

The error vscode is giving is when I try to do anything with new_state, it gives this response


when if I just try to use new_state itself, it says the type is <class ‘homeassistant.core.State’>

Right, that’s a state object.

If you want the state from the state object, you’d use new_state.state. Try inspecting the State object.

It seems like you’re trying to code without understanding basic object orientation in python. Have you looked at any object oriented programming tutorials with basic python?
https://realpython.com/python3-object-oriented-programming/

Yes, that’s what I understood, except I was trying to use new_state.state, and it wasn’t working
image

Because you aren’t annotating it and blocking against none. Which is what the code I posted above does.