NOOB Question - Chained conditions (IF / ELSEIF)

Hi ESPHome Experts!

I’m trying ESPHome for the last 4 days and I’ve accomplished a lot within a few hours of investigating and programming the yaml file.

But for the last 2 days I’m stuck within a problem. Let me explain:

I need to whenever a sensor in Home Assistant change it’s value, ESPHome should run a pre determined ESPHome script for each value from the sensor.

Example:
If HA Sensor value is “AAA”, than I need to run script “script_aaa”;
If HA Sensor value is “BBB”, than I need to run script “script_bbb”.

I’ve already tried:

text_sensor:
  - platform: homeassistant
    id: ha_sensor1
    entity_id: $ha_sensor1
    on_value:
      then:
        - logger.log:
            format: "DEBUG - Sensor 1 Value = %s"
            args: [ x.c_str() ]
        - if:
            condition:
              lambda: 'return x.c_str() == "aaa";'
            then:
              - script.execute: script_aaa
        - if:  
            condition:
              lambda: 'return x.c_str() == "bbb";'
            then:
              - script.execute: script_bbb

But this doesn’t work!

I can convert the logic to lambda, but I don’t know how to run a script in this mode. Is there any possibility to run a script within lambda ??

How can I get this working?

Thanks in advance!
MrFroggy

I’ve solved by my self.
The problem was related to several problems. Here it is the final code:

text_sensor:
  - platform: homeassistant
    id: ha_sensor1
    entity_id: $ha_sensor1
    on_value:
      then:
        - logger.log:
            format: "DEBUG - Sensor 1 Value = %s"
            args: [ x.c_str() ]
        - if:
            condition:
              lambda: return (x == "aaa");
            then:
              - script.execute: script_aaa
        - if:  
            condition:
              lambda: return (x == "bbb");
            then:
              - script.execute: script_bbb

That wasn’t a noob question. :slight_smile: You seem to have a good handle on how things are done.

Tip for future: be more descriptive in your title.

Thanks for your feedback. I’ve changed the post title.

1 Like