Automation doesn't trigger on the regex template

I’m trying to get an automation to trigger on: state kept for a period of time, a short “click” and a regex on the state.

The state that is the issue is this:

trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.ns_panel_last_click_va') | regex_findall("\d+,\d+,\d+")
      }}
    id: color change
  - platform: state
    entity_id: sensor.ns_panel_last_click_va
  - platform: state
    entity_id: sensor.ns_panel_current_display_page
    to: '13.00'
    id: thermostat
  - platform: state
    entity_id: sensor.ns_panel_brightness_slider
    id: brightness change
  - platform: template
    value_template: >-
      {{ states('sensor.ns_panel_last_click_va') != "" and
      states('sensor.ns_panel_last_click_va') != "newtxt"}}
    for: '00:00:02'
    id: long

I use the “long” to enter a color/brightness change screen, and when I click on a color the rgb color is passed back to ESP-Home in a variable sensor.ns_panel_last_click_va.

The color change action is:

  - choose:
      - conditions:
          - condition: trigger
            id: color change
        sequence:
          - service: light.turn_on
            data:
              rgb_color: '{{ states("sensor.ns_panel_last_click_va").split(",") }}'
            target:
              entity_id: '{{ states("input_text.ns_panel_kitchen_selected_device") }}'
    default: []

Where selected device is set as you do the long press (long press = set input_text and change view to color change screen).

If I use the service call from above with the states it works fine. The issue is that the trigger is deemed as a state change, not the template regex.

When I look at the trace I see this (I’m not good at reading traces):

and

image

So it seems it triggers on the state change, not the regex.
What do I need to change to make it trigger on the regex which also is true?

You have 3 triggers on sensor.ns_panel_last_click_va, so the three of them will be evaluated each time its state changes.
The plain state trigger will always be true, and you cannot determine whether it’s that one or one of the others that will end up in trigger..

You should have mutually exclusive triggers…

I thought there was a hierarchy where they are evaluated from top to bottom.
That would make sense.

Thanks.
I may have to make them two regex triggers then, each matching their own states.

It seems the issue is not what I thought.
I replaced the state trigger with a template regex trigger that matches on the other states, which is entity IDs.

  - platform: template
    value_template: >-
      {{ states('sensor.ns_panel_last_click_va') | regex_findall("[a-z\.0-9\_]+")
      }}

This doesn’t trigger either and looking in the template tools it’s obvious.
It doesn’t evaluate to true/false.
It returns the match, either the RGB code or the entity ID depending on the trigger.
But how can I make an regex match return true/false?

I currently have these two patterns that does not conflict with each other:

If RGB:
{{ "255,125,3" | regex_findall("\d+,\d+,\d+") }} # works
{{ "light.davids_lampa" | regex_findall("\d+,\d+,\d+") }} # fails 

If entity
{{ "light.davids_lampa" | regex_findall("light.*|switch.*|climate.*" ) }} # works
{{ "255,125,3" | regex_findall("light.*|switch.*|climate.*" ) }} # fails

They return an array.
I have tried

{{ "255,125,3" | regex_findall("light.*|switch.*|climate.*" ) == "" }}
{{ "255,125,3" | regex_findall("light.*|switch.*|climate.*" ) == [] }}
{{ "255,125,3" | regex_findall("light.*|switch.*|climate.*" ) == [""] }}

I just can’t see if it has matched or not. Anyone that knows the syntax?

{{ "255,125,3" is match("\d+,\d+,\d+$") }}
{{ "light.foo" is match("light.*|switch.*|climate.*") }}

See the fine manual.

1 Like

I read that but didn’t understand that was the purpose of it.
I was so focused on the word “regex”.
Thank you!

1 Like