YAML / Lambda - If string is not empty

I created a Helper in HA - wash_person
In my ESPHome YAML program, I have a global string - laundry_person_wash

In my nested loop, I want to assign whatever/whomever is in my global string laundry_person_wash to my HA helper - wash_person. I can’t get my my second IF to work

     'return !id(laundry_person_wash).state.empty();' 

This line keeps throwing an error.

/config/esphome/cyd-esp32.yaml: In lambda function:
/config/esphome/cyd-esp32.yaml:218:60: error: expected ';' before '}' token
  218 |                     'return !id(laundry_person_wash).state.empty();'
      |                                                            ^
      |                                                            ;
  219 |                 then:
      |                                                             
/config/esphome/cyd-esp32.yaml:219:3: warning: no return statement in function returning non-void [-Wreturn-type]
  219 |                 then:

I also tried this (below) without the lambda, but that did not work either. This is what I tried before try Google’s recommendation above.

id(laundry_person_wash) != ""

Here is my full interval code

interval:
  - interval: 15s
    then:
      - if:
          condition:
            - lambda: |-
                return !id(recent_touch);
          then: 
            - light.turn_off:
                id: backlight
            - if:
                condition:     # id(laundry_person_wash) != ""
                  lambda:  |-
                    'return !id(laundry_person_wash).state.empty();' 
                then:
                  homeassistant.service:
                    service: input_text.wash_person #HELPER input_text in HA
                    data:
                      value: id(laundry_person_wash) #Global string in ESPHome
            - lambda: |-
                id(bool_wash) = false;
                id(bool_dry) = false;
                id(laundry_person_wash) = "";
                id(laundry_person_dry) = "";
          else:
            - lambda: |-
                id(recent_touch) = false;

What is code that will work for me ?

I don’t even know this language, but what I can see is that you have quoted string for the lambda that is causing problems and in the remaining minds you don’t. Could that be relevant?

1 Like

That makes two of us. About the only thing that i really know about YAML is it gets really picky with code alignment. 1 too many or too few TAB and the compiler gets mad.

I saw that as well with the quote. I did also try to compile my program without the ‘ ‘ only to get a different error

/config/esphome/cyd-esp32.yaml: In lambda function:
/config/esphome/cyd-esp32.yaml:218:44: error: ‘class std::__cxx11::basic_string’ has no member named ‘state’
218 | return !id(laundry_person_wash).state.empty();
| ^~~~~

In YAML, |-and >- is like adding implicit quoting.
Lambdas are C++ code.

So you are basically double-quoting your C++ code, which the compiler doesn’t understand.

2 choices:

  • keep the quotes, but remove line continuation:
lambda: 'return !id(laundry_person_wash).state.empty();' 
  • Or keep the line continuation and remove the quotes:
lambda: |-
  return !id(laundry_person_wash).state.empty();
1 Like

Both of those options result in the same error

/config/esphome/cyd-esp32.yaml: In lambda function:
/config/esphome/cyd-esp32.yaml:217:44: error: 'class std::__cxx11::basic_string<char>' has no member named 'state'
  217 |                   lambda: 'return !id(laundry_person_wash).state.empty();'
      |                                            ^~~~~

Further up is my global declaration

globals:
  - id: laundry_person_wash
    type: std::string
    initial_value: ''
    restore_value: no # Optional: saves value across reboots

Ah, you don’t need the .state part

1 Like

This worked. Thanks so much!