MQTT sensor, convert received message to simple on/off text

Dear guys.

I need your help, I use Pellmon to monitoring my Pellet burner in my house, I have connected the Pellmon system via Raspberry pi and MQTT, it sends message to Home Assistant with all the temperatur data and more.

What I need help for is to convert the state message to simple (“On / Off text”) the state of the pellet burner ( is it running or is it stopped) I get this message via MQTT from the Pellmon 2021-12-06 00:29:43,759 - INFO - ‘mode’ changed from ‘Running’ to ‘Stopped’)

How can I specify that I want the home assistant to concentrate only on the text Running and Stopped and translate it to ON / OFF so I can make a sensor out of it and log the state change.

Can it be done with som template coding ?

Best Regards

You could use a split on the text string and select the part that is always the same and then use that to make a template sensor.

Something like…
if ((states(‘sensor.something’).split(‘INFO’)[2])==" - ‘mode’ changed from ‘Running’ to ‘Stopped’")

{% set x = "INFO - ‘mode’ changed from ‘Running’ to ‘Stopped’" %}
{% set y = x.split(" to ")[1] %}
{% if y == "‘Stopped’" %}
  {{ "off" }}
{% elif y == "‘Running’" %}
  {{ "on" }}
{% endif %}

If the message is this:

2021-12-06 00:29:43,759 - INFO - 'mode' changed from 'Stopped' to 'Running'

This template will report ON when the last word is Running otherwise it will report OFF.

{{ 'ON 'if value[-8:-1] == 'Running' else 'OFF' }}

If the message has the trailing parenthesis ) shown in your first post then the template will need to be adjusted slightly.

I assume you intend to use this in an MQTT Binary Sensor (specifically in the value_template option) so that’s why the template employs the value variable.