Trigger different HA Services dependent by ESPhome on a Integer Variable

Hi,

I want to trigger the HA Cover Service from a ESPhome Node with different Arguments.
I used this:

State 0:

   on_press:
      then:
        - logger.log: Roller All Open
        - homeassistant.service:
            service: cover.open_cover
            data: 
              entity_id: cover.wg_rollo_ost_tuer_wg_rollo_ost_tuer

State 1:

   on_press:
      then:
        - logger.log: Roller All Stop
        - homeassistant.service:
            service: cover.stop_cover
            data: 
              entity_id: cover.wg_rollo_ost_tuer_wg_rollo_ost_tuer

State 2:

   on_press:
      then:
        - logger.log: Roller All close
        - homeassistant.service:
            service: cover.close_cover
            data: 
              entity_id: cover.wg_rollo_ost_tuer_wg_rollo_ost_tuer

State 3:

   on_press:
      then:
        - logger.log: Roller All Stop
        - homeassistant.service:
            service: cover.stop_cover
            data: 
              entity_id: cover.wg_rollo_ost_tuer_wg_rollo_ost_tuer

I defined a Int - Global and tried this

   on_press:
        - lambda: |
            id(keyfunktion_R1) = id(keyfunktion_R1) + 1;
            if ( id(keyfunktion_R1) == 1 ) {
               - logger.log: Roller Stop
            }

but only with the logger-command I got This Error.

error: expected primary-expression before ‘.’ token

I tried also to define a 2nd Global to set the Command in the if Statement and use ist outside the if-Statement, but there was no reaction at the Logger Command.

    on_press:
        - lambda: |
            id(keyfunktion_R1) = id(keyfunktion_R1) + 1;
            if ( id(keyfunktion_R1) == 1 ) {
            id(rolleraction) = "cover.stop_cover";
            } else {
            id(rolleraction) = "cover.close_cover";
            } 
        - logger.log: Roller All getting Function
        - logger.log: id(rolleraction)

The logger writes only:
[20:50:47][D][main:260]: Roller All getting Function
[20:50:47][D][main:263]: rolleraction->value()

I don’t have any ideas now, that I get the value to the logger and HA-Service.

I would be happy if someone can help me

You can’t use logger.log in a lambda AFAIK. Try ESP_LOGI("main", "Roller Stop");

   on_press:
        - lambda: |
            id(keyfunktion_R1) = id(keyfunktion_R1) + 1;
            if ( id(keyfunktion_R1) == 1 ) {
                ESP_LOGI("main", "Roller Stop");
            }
1 Like

Hello Mikefila,

thanks very much for your help. The logger is working now.