Put the template for statex in a binary template sensor and trigger when the sensor goes from ‘on’ to ‘off’
Or use a template trigger with reversed output logic:
{% set statex = stateb > statea %}
{{ statex }}
Also please format your pasted config correctly in future. Use the </>
button in the post toolbar or this: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16