Can I compare a substring from Home Assistant in ESPHome

I’m trying to make a smartest possible binary sensor :slight_smile:

If my alarm is armed, the sensor reads “armed_night”, “armed_…” etc.
I want to have ESPHome turn on a LED if it is any kind of ‘armed’.

So my first thought was to make a binary sensor, and have that placed in the ESPHome device, and just have the LED react on that.

So I made a template:

{{ states('alarm_control_panel.alarmo') [:5] }}

This will just return the first 5 letters.
So I thought I could make a simple binary sensor like

{{ states('alarm_control_panel.alarmo') [:5] = 'armed' }}

This will make a binary sensor that reflects the armed state as true/false, and I can react on that in ESPHome.

But is there a better way, so I don’t make an extra sensor in HA, and just let ESPHome determine the substring from the sensor itself?

I’m sure it’s possible to extract the “armed” part of the text sensor with a lambda but I don’t know enough about C to help you. However you don’t really need to do this. Just use an or condition to check the full string states:

on_value:
  - if:
      condition:
        or:
          - text_sensor.state:
              id: my_text_sensor
              state: 'armed_away'
          - text_sensor.state:
              id: my_text_sensor
              state: 'armed_night'
          - text_sensor.state:
              id: my_text_sensor
              state: 'armed_whatever'
      then:
        - output.turn_on: my_led
  - if:
      condition:
          text_sensor.state:
            id: my_text_sensor
            state: 'disarmed'
      then:
        - output.turn_off: my_led
1 Like

Hi @tom_l
You are absolutely right, I just wanted to do it the ‘clever’ way :slight_smile: