Regex to filter raw data from dutch gas

Hi,
I am not able to get the gas value out of my “slimmemeter”

Tried al kinds of settings, different mbus etc.
The raw data however is available, and looks like this:
(211230120000)(00)(60)(1)(0-1:24.2.1)(m3)(14958.845)
I found the regex to be this:
/.(m3)((.))/gm

Now all I need is a template in HA that creates the right sensor. :slight_smile:

Is there anyone who can help me with this?

Would be great,

Rob

I highly doubt that is the correct regex.
What value are you trying to get?

() is special characters in regex and needs to be escaped to be read literal.
Dot means one single any value.

This regex and that string would result in m3) as the output where m3 is in the first capture and ) is in the second capture.

Are you sure that is correct?

I am not sure to be honest,
I checked the regex here:

I am trying to get 14958.85 (the value between brackets)

But I am also not quite sure how to create a template that creates a sensor with the value. (if we assume the regex is right.)

So that is quite a bit different than what you posted above then.

But yes that seems to work. See here: Template Sensor with regex_findall_index not working - Configuration - Home Assistant Community (home-assistant.io)

I think this regex pattern will serve you better:

\((\d*\.?\d*)\)$

It will work if the value is a float, like 14958.845 or an integer, like 14958.

Will it be used in a Template Sensor or MQTT Sensor or what?

1 Like

The pattern he has on the regex101 has .* in the capture so it will work too. But I generally prefer to not be greedy when I use .*

Yes, I agree with you, but like you said it’s ‘greedy’ so it matches anything including a non-numeric string. Perhaps the data will never contain anything other than a float value but, just to be extra careful, the suggested regex pattern looks exclusively for a numeric string (with or without a decimal point).

This is the sensor that is comming in via espHome integration.
So I was thinking a template would be the easiest way to get this working.

The whole idea is to get the gas sensor in the engergy dashboard.

  - platform: template
    sensors:
      gasmeter:
        value_template: "{{ states('sensor.gas_delivered_raw') | regex_findall_index('\((\d*\.?\d*)\)$') }}"
        friendly_name: gasmeter
        unit_of_measurement: m³
        icon_template: mdi:oil
        device_class: gas
        state_class: total_increasing

image

I think we are almost there. but unfortunately an error.

I don’t have an easy means of testing the Template Sensor but the template can be tested in the Template Editor and it works:

Yes, it works in the template tester, but gives an error in the configuration.yaml

As a quick experiment, try this version and see if it produces the same error. It uses double-quotes inside the template and single-quotes outside the template:

        value_template: '{{ states("sensor.gas_delivered_raw") | regex_findall_index("\((\d*\.?\d*)\)$") }}'

Don’t forget to execute Reload Template Entities after making the modification.

@123 thanks, that helped a lot.
Last thing remaining seems the state_class.
image

I could add this in a customization, but why does it not work in the template?

  - platform: template
    sensors:
      gasmeter:
        value_template: '{{ states("sensor.gas_delivered_raw") | regex_findall_index("\((\d*\.?\d*)\)$") }}'
        friendly_name: gasmeter
        unit_of_measurement: m³
        icon_template: mdi:tank
        device_class: gas
        state_class: total_increasing

Because you are using the legacy method of configuring a Template Sensor; it doesn’t directly support the state_class option (only indirectly when included via customization). If you change the Template Sensor’s configuration to the “modern” format, state_class is a valid option.

template:
  - sensor:
      - name: gasmeter
        unique_id: gasmeter
        state: '{{ states("sensor.gas_delivered_raw") | regex_findall_index("\((\d*\.?\d*)\)$") }}'
        unit_of_measurement: m³
        icon: mdi:tank
        device_class: gas
        state_class: total_increasing

O boy,
I am learning a lot today. :slight_smile:
Thanks again.
The sensor seems to work fine on my energy dashboard. data is coming in.

I would like a bar on my regular dashboard as wel, that shows the gas in m3 for that day.
A bit like the original energy dashboard.
So I would need to do some math on the sensor? or is there an easy way?
The sensor should already be in HA, because the energy dashboard is using it, or creating it. but I can’t find it.
Any clue?

Glad to hear that the original problem has been solved.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been solved. It will also place a link below your first post that leads to the Solution post. All of this helps users find answers to similar questions. For more information, please refer to guideline 21 in the FAQ.

It would be best to ask this question in a separate topic where it will be read by a wider audience. This topic draws the attention of people interested in employing “Regex to filter raw data”.

I received a notification that you marked my post with the Solution tag.

SOLVED regex to filter raw data from dutch gas

However, you promptly removed the tag. Did the suggested modern-style Template Sensor fail to solve the original problem?

Hi Taras,

I am not sure why the solution tag got removed, that was not intended.
The post containing the solution tag contains all the info needed.

Thanks again.

1 Like

Hi, thanks for this topic it made a lot of things clear to me!
But unfortunately it still don’t work for me… The sensor says its no available. Maybe I forgot something?

  1. I made the sensor in configuration.yaml
  2. I don’t know where to change the regex pattern?
  3. The error’s : image
    image

Sorry, I don’t know the answer. I suggest you search the forum for ‘statistics_not_defined’ and see if anyone else fixed the problem.