Possible to match version numbers in jinja with some sort of regex?

I have a sensor set to pull tasmota firmware versions. The version number it gets back comes back as something like “6.5.0”. I have another sensor that pulls current firmware from all my tasmota devices, but it comes back as something like “6.5.0(release-sonoff)” I tried the following:

- platform: mqtt
    name: "Sonoff S31-9 Firmware version"
    state_topic: "stat/sonoffS31-9/STATUS2"
    value_template: "{{ value_json['StatusFWR'].Version | regex_search('^[0-9]\\.{1,2}[0-9]\\.{1,2}[0-9]{1,2}', ignorecase=TRUE) }}"
    qos: 0
    availability_topic: "tele/sonoffS31-9/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

but that just returns “True” because it matches. Basically I want to extract just the X.X.X version from the front of the string, and strip off the rest. The problem is I dont know what the next version will be (might be 6.10.2, might be 6.6.10(silly-rabbit), etc)

According to https://www.home-assistant.io/docs/configuration/templating/ there are very few regular expression options.

Thanks.

What you want is called regex_findall_index.

The regex pattern you want is: (\d+\.\d+\.\d+)

It will match any number that’s in this format: number.number.number
For example: 6.5.0, 6.55.21, 476.345.123, etc.

  • The parentheses () form a capture group.
  • \d means match a number.
  • \d+ means match one or more numbers.
  • \. means match a literal period.
    This pattern repeats two more times.
value_template: "{{ value_json['StatusFWR'].Version | regex_findall_index('(\d+\.\d+\.\d+)', ignorecase=True) }}"

If you’re interested, I provided a more detailed explanation of regex_findall_index in this post.

1 Like

Damn i was close. Thanks! I’ll read up on your link. Thanks again!