Assistance with consolidating a working RESTful sensor and respective template ...and REGEX

Assuming the below “working” code below. …I would like to move the template code at the bottom to in the REST sensor itself:

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: process_0_cmdline
        value_template: '{{ value_json.processlist.0.cmdline.0,value_json.processlist.0.cmdline.1 | default }}'
        json_attributes_path: "$.processlist.0.cmdline.[0,1]"


sensor:
  - platform: template
    sensors:
      process_0_exe:
        value_template: >
                {{ states('sensor.process_0_cmdline') | regex_findall(find="([^\\\\]+\.exe)",ignorecase=False) | join(' ') }}
        availability_template: >-
                {{ states("sensor.process_0_cmdline") not in ["unknown", "unavailable", "none"] }}

Unfortunately, when I do this (see below), it produces a syntax error complaining about the unexpected escape character ** before the dot character: \.exe

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: process_0_cmdline
        value_template: "{{ (value_json.processlist.0.cmdline.0,value_json.processlist.0.cmdline.1 | default) | regex_findall(find='([^\\\\]+\.exe)',ignorecase=False) | join(' ') }}"
        json_attributes_path: "$.processlist.0.cmdline.[0,1]"

…If I remove the escape character before the dot, regex_findall() will no longer treat the dot as literal. It means something completely different without the escape character.

Could someone please tell me what I’m missing? Also, I’m not sure how to move the availability template to the REST sensor either. My ultimate goal is to make the code a little shorter/more concise.

I’m guessing the issue is because in one case you’re surrounding it in double-quotes, and the other you’re not. So possibly the double-quoted string is seeing the backslash and escaping the ‘.’ before the “value_template” gets to see it.

So try putting it on a separate line like you did for the sensor, ie:

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: process_0_cmdline
        value_template: >
          {{ (value_json.processlist.0.cmdline.0,value_json.processlist.0.cmdline.1 | default) | regex_findall(find='([^\\\\]+\.exe)',ignorecase=False) | join(' ') }}
        json_attributes_path: "$.processlist.0.cmdline.[0,1]"

I managed to escape the dot by using the backslash Unicode character: \u005c See below. This syntax is considered acceptable by YAML validators. However, Home Assistant\s REST sensor not handling regex_findall() pattern correctly.

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: process_0_cmdline
        value_template: "{{ (value_json.processlist.0.cmdline.0,value_json.processlist.0.cmdline.1 | default) | regex_findall(find='([^\\\\]+\u005c.exe)',ignorecase=False) | join(' ') }}"

HA’s template editor proves that the above code is correct; yet, it is still not handled correctly:

{% set process_0_cmdline = "('C:\\Program Files\\Plex\\Plex Media Server\\Plex Media Server.exe', '-noninteractive')"  %}
{{ process_0_cmdline | regex_findall(find='([^\\\\]+\u005c.exe)',ignorecase=False) | join(' ')}}

RESULT: Plex Media Server.exe

See if this works any better:

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: process_0_cmdline
        value_template: >
          {{ [value_json.processlist.0.cmdline.0, value_json.processlist.0.cmdline.1 | default] 
            | regex_findall('([a-zA-Z ]*\.exe)', False) | join(', ') }}

Example

1 Like

Man, I’m so glad you replied to my post!

PS: I’ve seen official documentation using: regex_findall(find=‘()’,ignorecase=False) and regex_findall(’ ', False). Is there any reason to do it the long way? I’m not sure if there are any issues to using the shorter way.

If you omit the find and ignorecase argument tags, you must enter the two arguments in the correct order (the regex pattern first, for find, and the boolean value second, for ignorecase). If you include the tags, you can specify the two arguments in whatever order you want (it also allows you omit an argument such as in the situation where a filter accepts three arguments and you only need to specify the values of the first and third one).

The tags also serve to document the arguments (i.e. at a later date you would know that the False means to ignorecase).

1 Like