How to access sensor data from mqtt template?

I have this template code from climate but states('sensor.AFB_desired_temp') is unknown

      temperature_high_command_topic: "AFB/JSONout"
      temperature_high_state_topic: "AFB/JSONout"
      temperature_high_state_template: >
        {% if value_json is not none and "ThermostatOvertemp" in value_json %}
            {{ value_json.ThermostatOvertemp|int + states('sensor.AFB_desired_temp')|int }}
        {% else %}
            0
        {% endif %}

What exactly is your question?

Is it how to find the sum of two entity’s values when one of them is unknown?

If that’s the question then supply the int filter with a default value. For example, if the int is unable to convert the supplied value, it will report 0.

{{ value_json.ThermostatOvertemp|int(0) + states('sensor.AFB_desired_temp')|int(0) }}