Behaviour of Template Switch

Hi everyone,
I’m trying my first baby steps with ESP-Home and ran into something I don’t understand. I’ve got the the following setup:

switch:
  - platform: template
    name: "Krippe"
    icon: "mdi:star-shooting"
    turn_on_action:
      - light.turn_on:
          id: red
          brightness: 100%
          effect: "Flicker"
      - light.turn_on:
          id: yellow
          brightness: 100%
          effect: "Flicker"
    turn_off_action:
      - light.turn_off:
          id: red
      - light.turn_off:
          id: yellow
      
light:
  - platform: monochromatic
    name: "Rot"
    id: red
    output: red_out
    effects:
      - flicker:
          intensity: 10%
    internal: true      
  - platform: monochromatic
    name: "Gelb"
    id: yellow
    output: yellow_out  
    effects:
      - flicker:
          intensity: 10%
    internal: true
    
output:
  - id: red_out
    platform: ledc
    pin: GPIO32
  - id: yellow_out
    platform: ledc
    pin: GPIO25

So basically a red and yellow LED to imitate a fireplace. I don’t want the LEDs to be exposed to HA, so I set them to internal. No I want a switch to flick the LEDs on and off. But the switch works not as expected: Whenever I switch it to ON in the frontend, the LEDs react as I want them to. But then the switch turns itself off after about a second. When I’m quick enough, I can switch it off myself and the LEDs will follow as expected.

Is there something about the internal state of switch I am missing perhaps?

The template switch has no way of knowing its state. You need to supply either:

  • lambda (Optional, lambda): Lambda to be evaluated repeatedly to get the current state of the switch.

Or one of these:

  • optimistic (Optional, boolean): Whether to operate in optimistic mode - when in this mode, any command sent to the template switch will immediately update the reported state. Defaults to false.
  • assumed_state (Optional, boolean): Whether the true state of the switch is not known. This will make the Home Assistant frontend show buttons for both ON and OFF actions, instead of hiding one of them when the switch is ON/OFF. Defaults to false.

Supplying a lambda state (Both LEDs on = switch is on) would be best.

Assumed state is a good option if you don’t mind having two lighting bolt icons, one for off and one for on instead of a toggle switch in Home Assistant.

Optimistic would be a second best option, as the LEDs can only be controlled by the template switch (and no other outside source like the individual LED controls) you only have to worry about the state being incorrect after loss of power.

1 Like

Thank you so much for the quick reply - got the point!
But sadly I’m still not able to make it work like I’m hoping:

switch:
  - platform: template
    name: "Krippe"
    icon: "mdi:star-shooting"
    lambda: !lambda |-
      if (id(red).current_values.is_on() and id(yellow).current_values.is_on()) {
        return true;
      } else {
        return true;
      }
    turn_on_action:
      - light.turn_on:
          id: red
          brightness: 100%
          effect: "Flicker"
      - light.turn_on:
          id: yellow
          brightness: 100%
          effect: "Flicker"
    turn_off_action:
      - light.turn_off:
          id: red
      - light.turn_off:
          id: yellow
      
light:
  - platform: monochromatic
    name: "Rot"
    id: red
    output: red_out
    effects:
      - flicker:
          intensity: 10%
    internal: false      
  - platform: monochromatic
    name: "Gelb"
    id: yellow
    output: yellow_out  
    effects:
      - flicker:
          intensity: 10%
    internal: false
    
output:
  - id: red_out
    platform: ledc
    pin: GPIO32
  - id: yellow_out
    platform: ledc
    pin: GPIO25

I tried different methods to get the state from the lights, but I can’t seem to get it working… The switch always turns itself on again; even if the lights are off.

Your else case in the lambda should return false.

1 Like

Oh god, this is really embarrassing :sweat_smile:

Thank you!!

2 Likes