(solved) Template to filter out part of a MQTT-message

I’ve got an EPShome camera that sends images to HA and also publish MQTT messages.

I need to know if the camera is sending images to HA and want to use the MQTT-messages to do it.

The problem is that it not only sends image-info, but all sorts of log info, so I need to make a template that takes out only a part of the message.

The message I want to take a part from looks like this:

e[0;36m[D][esp32_camera:157]: Got Image: len=53798e[0m

I also go messages like this and some others in the same mqtt-topic which I’m not interested in:

e[0;36m[D][sensor:092]: 'ttgocam_1 WiFi Signal': Sending state -24.00000 dB with 0 decimals of accuracye[0m

Since the number is unique for each message I want just the “got image”-part in a separate sensor. I want is to use this to look at the timestamp for last received image to know if my camera sends images or not.

How can I make a template sensor to do this?

sensor:
  - platform: mqtt
    name: "Image Length"
    state_topic: "your/topic/here"
    unit_of_measurement: bytes # not actually sure if this is the right unit and you only need it for graphs. Can be omitted.
    value_template: "{{ value|regex_findall_index(find='Got Image: len=(\d+)' }}"

In your example this will return 53798 as the sensor state.

Alternatively, this:

 value_template: "{{ value|regex_findall_index(find='Got Image: len=([^\s]+)' }}"

will return 53798e[0m as the state.

Hi :slight_smile:

Thanks for your quick answer!

You didn’t exactly solve my problem, but you put me in the right direction :slight_smile:

This is my solution:

First I made a sensor that keeps the latest raw mqtt-data:

  - platform: mqtt
    name: ttgocam_1_mqtt_debug
    state_topic: "ttgocam_1/debug"

Then I made a new template binary sensor that is “true” if the last message contains the text “Image:”:

  - platform: template
    sensors:
      ttgocam_1_image:
        value_template: "{{ 'Image:' in states.sensor.ttgocam_1_mqtt_debug.state }}"

Works as intended and I am now able to automate monitoring and automatic restarts of the camera if it stops sending images after a given amount of time :slight_smile:

You might have wanted to lead with that in your original post to minimize people wasting their time coming up with a solution to a problem that you didn’t want and not coming up with a solution for the real problem that you wanted all along.

However, I’m not sure if the sensor you created will do what you want.

Does the message change if the camera stops sending images or does the message just completely stop being sent at all?

If it’s the latter then the last message will always contain “image” and you still won’t know your camera is dead. You will just know that the last message received (whenever that was) contains “image”.

If it’s the former and it still sends messages but they don’t contain “image” then your solution should work.

Hi

Yes I know, but I also think it’s a good thing when the title is more precise when searching for solutions in the forum. Templating in HA doesn’t have anything with configuring in ESPhome so i thought it was best to do it separate. I still wanted the other solution the most, but was working on this alternative at the same time. In general I agree with you :slight_smile:

Well, it turns out that the board keeps sending data, but stops sending images of some reason. I was thinking that what you are pointing out would be the case, but it’s not. I really want to know why it behave like this, but haven’t found anything other than people having the same problem with no solution.

The PIR, Status and WiFi-sensor keeps sending data and the board is responding to the restart-switch. I even get reboot-info via MQTT-debug under the whole reboot-process.

With my restart-automation it gets back up sending images after just 20 seconds. That results in under 4 minutes loss of images when having the automation triggered by state false with for 3 minutes condition. After some time I will know how low I can set the minutes-condition.