Is it possible to expose a switch as a light

Hello!

I have configured a D1 mini to transmit infrared to two GP mood lights in my window.

Everything works as expected (after countless hours of fiddling with a faulty IR receiver). The ESP8266 (D1 mini) is connected to a IR LED. There is a switch that toggles on and aff (and sends corresponding codes via transmitter). All other functions from the original remote control are set up as services so that I can send a service from Home Assistant. There is also a service to send RAW commands that I left after debugging, which may come in handy if I would discover more codes.

Hovewer. I wonder if there is a way to expose the on/off switch as a light instead? I know this is possible in Home assistant as a light with switch platform in lights.yaml, but I want to expose it directly from the node yaml.

What I’m hoping for is to have a light in Home assistant with a color picker and the rest of the functions as light effects.

Here is the yaml for my Esphome node. Thanks!

esphome:
  name: ir_transmitter_livingroom
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wlan_ssid
  password: !secret wlan_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: !secret wlan_fallback_ssid
    password: !secret wlan_fallback_password

captive_portal:

# Enable logging
logger:

substitutions:
  # use as ${freq}
  freq: 35.75kHz
  switch_id: "ir_transmitter_on_off"

# Enable Home Assistant API
api:
  password: !secret esphome_homeassistant_password
  services:
  - service: transmitter_raw
    variables:
      code: int[]
    then:
      - remote_transmitter.transmit_raw:
          carrier_frequency: ${freq}
          code: !lambda 'return code;'
  - service: send_on
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [9003, -4523, 536, -573, 560, -572, 560, -573, 535, -574, 560, -572, 560, -574, 534, -574, 559, -1707, 533, -1708, 557, -1684, 557, -1711, 529, -1737, 529, -1686, 556, -1737, 503, -1738, 529, -605, 503, -579, 554, -1738, 503, -579, 554, -605, 528, -1687, 555, -604, 529, -605, 503, -579, 554, -1738, 503, -579, 555, -1738, 503, -1737, 529, -605, 503, -1738, 528, -1687, 555, -1738, 503]
  - service: send_off
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8971, -4525, 534, -575, 558, -574, 559, -574, 534, -574, 559, -573, 559, -575, 533, -576, 557, -1708, 532, -1710, 556, -1685, 555, -1712, 528, -1713, 554, -1687, 554, -1713, 528, -1712, 554, -581, 527, -580, 554, -1712, 528, -1713, 554, -1687, 554, -1713, 528, -579, 554, -580, 553, -581, 527, -1713, 554, -580, 528, -580, 553, -580, 553, -580, 528, -1713, 554, -1687, 554, -1713, 528]
  - service: send_auto
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8997, -4500, 559, -575, 534, -574, 559, -573, 559, -574, 534, -575, 558, -574, 559, -575, 532, -1708, 558, -1684, 556, -1711, 529, -1712, 554, -1687, 555, -1712, 528, -1713, 554, -1687, 554, -580, 553, -1687, 555, -580, 553, -580, 527, -580, 554, -580, 553, -580, 528, -580, 554, -579, 553, -581, 527, -1713, 554, -1687, 554, -1713, 528, -1713, 554, -1687, 554, -1713, 528, -1713, 553]
  - service: send_pause
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8997, -4500, 559, -575, 533, -575, 559, -573, 559, -574, 534, -575, 558, -574, 558, -576, 532, -1707, 559, -1684, 556, -1710, 531, -1710, 555, -1687, 554, -1713, 528, -1713, 553, -1687, 555, -579, 554, -580, 527, -1713, 554, -580, 528, -579, 554, -580, 553, -581, 527, -580, 554, -580, 552, -1688, 554, -580, 553, -1687, 554, -1713, 528, -1713, 553, -1688, 554, -1713, 528, -1712, 554]
  - service: send_flash
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8972, -4525, 534, -575, 559, -573, 559, -574, 534, -575, 558, -574, 559, -574, 533, -576, 557, -1708, 532, -1709, 557, -1685, 555, -1712, 528, -1713, 554, -1687, 557, -1710, 528, -1712, 554, -581, 527, -1713, 554, -1687, 554, -580, 553, -580, 528, -579, 554, -580, 553, -581, 527, -580, 554, -580, 553, -580, 528, -1712, 554, -1687, 554, -1714, 527, -1713, 553, -1688, 554, -1713, 527]
  - service: send_increase_brightness
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8997, -4500, 559, -575, 533, -575, 559, -573, 559, -574, 534, -575, 558, -574, 559, -575, 532, -1708, 558, -1683, 557, -1710, 530, -1711, 555, -1687, 554, -1713, 528, -1712, 554, -1687, 555, -580, 553, -1687, 554, -1713, 528, -1712, 555, -580, 527, -580, 554, -580, 553, -580, 528, -579, 554, -580, 553, -580, 528, -580, 553, -1713, 528, -1713, 554, -1687, 554, -1713, 528, -1713, 553]
  - service: send_start_3h_timer
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [9054, -4472, 587, -547, 534, -575, 558, -573, 586, -548, 560, -548, 585, -547, 584, -550, 557, -1683, 558, -1684, 556, -1711, 529, -1712, 554, -1687, 554, -1713, 529, -1712, 554, -1687, 554, -579, 554, -580, 528, -579, 554, -580, 553, -1687, 554, -580, 554, -579, 528, -580, 554, -580, 553, -1687, 554, -1713, 528, -1713, 554, -580, 527, -1713, 554, -1687, 554, -1713, 528, -1713, 553]
  - service: send_white
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8998, -4500, 559, -573, 559, -574, 534, -575, 558, -574, 559, -574, 533, -576, 557, -575, 558, -1683, 557, -1709, 531, -1711, 554, -1687, 555, -1712, 528, -1713, 554, -1687, 554, -1713, 528, -579, 555, -1712, 528, -580, 554, -580, 553, -1687, 554, -579, 554, -580, 528, -579, 554, -580, 553, -580, 528, -1713, 554, -1687, 554, -580, 553, -1687, 554, -1713, 528, -1712, 554, -1687, 554]
  - service: send_red
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8997, -4500, 559, -575, 534, -574, 559, -573, 559, -574, 534, -575, 559, -573, 558, -576, 532, -1708, 557, -1685, 556, -1711, 529, -1712, 554, -1687, 554, -1713, 528, -1713, 553, -1687, 555, -580, 552, -581, 528, -1713, 553, -580, 528, -1713, 553, -581, 527, -580, 554, -580, 553, -580, 528, -1713, 553, -581, 527, -1713, 553, -581, 527, -1714, 552, -1688, 554, -1713, 528, -1713, 553]
  - service: send_green
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8972, -4525, 534, -575, 558, -574, 559, -574, 534, -574, 559, -573, 559, -575, 533, -575, 558, -1708, 532, -1709, 556, -1685, 556, -1711, 529, -1713, 553, -1687, 555, -1713, 527, -1713, 554, -581, 526, -1714, 553, -1687, 555, -604, 528, -1687, 555, -1713, 528, -579, 554, -580, 553, -581, 527, -579, 554, -581, 552, -1687, 555, -580, 552, -581, 527, -1714, 553, -1687, 554, -1714, 527]
  - service: send_blue
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8973, -4524, 535, -574, 559, -573, 559, -574, 534, -575, 559, -573, 559, -574, 533, -576, 557, -1708, 532, -1709, 556, -1686, 555, -1712, 528, -1713, 554, -1687, 554, -1713, 528, -1712, 554, -581, 527, -1713, 554, -1687, 554, -1713, 528, -1712, 554, -1687, 555, -580, 553, -580, 527, -580, 554, -580, 553, -580, 528, -579, 554, -580, 553, -581, 528, -1712, 554, -1687, 554, -1713, 528]
  - service: send_yellow
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [9002, -4525, 534, -574, 559, -573, 559, -574, 535, -574, 559, -573, 559, -575, 533, -575, 558, -1707, 532, -1710, 556, -1685, 555, -1712, 528, -1714, 553, -1687, 554, -1714, 527, -1714, 552, -581, 527, -580, 554, -580, 553, -1687, 554, -1714, 527, -580, 554, -604, 528, -581, 527, -580, 554, -1713, 527, -1714, 553, -581, 527, -580, 553, -1714, 527, -1713, 554, -1687, 554, -1713, 528]
  - service: send_purple
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8998, -4499, 560, -573, 559, -574, 534, -575, 558, -574, 559, -573, 534, -576, 558, -574, 558, -1683, 558, -1709, 530, -1710, 556, -1686, 555, -1712, 528, -1713, 554, -1687, 554, -1713, 528, -579, 555, -1712, 528, -580, 554, -1713, 527, -1714, 553, -580, 528, -579, 555, -579, 553, -581, 527, -580, 554, -1713, 527, -580, 554, -580, 553, -1687, 555, -1712, 528, -1713, 554, -1687, 554]
  - service: send_turquoise
    then:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [9023, -4499, 560, -573, 559, -574, 534, -575, 559, -573, 558, -575, 534, -574, 559, -574, 557, -1684, 557, -1710, 530, -1711, 554, -1687, 555, -1713, 527, -1713, 554, -1687, 554, -1713, 528, -579, 555, -580, 553, -1686, 555, -1713, 528, -1712, 554, -580, 528, -579, 554, -580, 553, -581, 527, -1713, 554, -580, 528, -579, 554, -581, 552, -1687, 554, -1713, 528, -1713, 554, -1687, 554]

ota:
  password: !secret esphome_ota_password

remote_transmitter:
  pin:
    number: D2
    mode: OUTPUT
  carrier_duty_percent: 50%

switch:
  - platform: template
    id: ${switch_id}
    turn_on_action:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [9003, -4523, 536, -573, 560, -572, 560, -573, 535, -574, 560, -572, 560, -574, 534, -574, 559, -1707, 533, -1708, 557, -1684, 557, -1711, 529, -1737, 529, -1686, 556, -1737, 503, -1738, 529, -605, 503, -579, 554, -1738, 503, -579, 554, -605, 528, -1687, 555, -604, 529, -605, 503, -579, 554, -1738, 503, -579, 555, -1738, 503, -1737, 529, -605, 503, -1738, 528, -1687, 555, -1738, 503]

    turn_off_action:
      remote_transmitter.transmit_raw:
        carrier_frequency: ${freq}
        code: [8971, -4525, 534, -575, 558, -574, 559, -574, 534, -574, 559, -573, 559, -575, 533, -576, 557, -1708, 532, -1710, 556, -1685, 555, -1712, 528, -1713, 554, -1687, 554, -1713, 528, -1712, 554, -581, 527, -580, 554, -1712, 528, -1713, 554, -1687, 554, -1713, 528, -579, 554, -580, 553, -581, 527, -1713, 554, -580, 528, -580, 553, -580, 553, -580, 528, -1713, 554, -1687, 554, -1713, 528]

  - platform: template
    name: "GP Bollar"
    id: temp_switch
    turn_on_action:
      - switch.template.publish:
          id: temp_switch
          state: ON
      - switch.turn_on: ${switch_id}
    turn_off_action:
      - switch.template.publish:
          id: temp_switch
          state: OFF
      - switch.turn_off: ${switch_id}

There are something called binary lights in esphome.


Is that what you’re looking for.
/Mattias

Thanks for your reply.

Yes, and no. I tried that one, but I get an error when trying to map the switch as output for the light.

This does not work:

light:
  - platform: binary
    output: ${switch_id}
    name: "My lamp"

I see that the binary light only works on output components.

There is an output template.

So in theory if you could just change the template in the switch to be output template.
Like this.

switch:
  - platform: template

to 
output:
   - platform: template

I have not tested so I don’t know if it works. But maybe worth a try.
/Mattias

Thanks! It works, kinda.

It solves a part of the problem, namely exposing a light instead of a switch and the output of the light is output to a template output.

This works

remote_transmitter:
  pin:
    number: D2
    mode: OUTPUT
  carrier_duty_percent: 50%

output:
  - platform: template
    # no name here, will not be exposed to frontend
    id: "gp_bollar_test_template"
    type: binary
    write_action:
      - if:
          condition:
            light.is_on: gp_bollar_test_light
          then:
            # On
            remote_transmitter.transmit_raw:
              carrier_frequency: 38kHz
              code: [224, -6013, 64, -5582, 9073, -4425, 689, -468, 616, -492, 640, -493, 666, -467, 615, -494, 668, -464, 691, -443, 641, -1602, 610, -1631, 639, -1628, 589, -1651, 690, -1553, 613, -1652, 613, -1606, 685, -1579, 613, -519, 638, -494, 641, -1602, 610, -523, 615, -492, 667, -1601, 612, -495, 640, -493, 665, -467, 616, -1627, 611, -522, 640, -1602, 663, -1578, 639, -492, 641, -1584, 632, -1652, 641, -1579, 633]
      - if:
          condition:
            light.is_off: gp_bollar_test_light
          then:
            # Off
            remote_transmitter.transmit_raw:
              carrier_frequency: 38kHz
              code: [228, -6351, 104, -3166, 9073, -4425, 689, -468, 639, -470, 692, -440, 691, -443, 640, -468, 640, -493, 689, -444, 639, -1604, 634, -1607, 666, -1601, 638, -1602, 663, -1580, 717, -1548, 661, -1580, 637, -1589, 708, -421, 683, -468, 639, -1604, 611, -1630, 665, -1602, 665, -1575, 662, -471, 615, -493, 666, -466, 640, -1602, 666, -467, 638, -497, 664, -443, 640, -492, 640, -1584, 737, -1551, 637, -1602, 691]

light:
  id: gp_bollar_test_light
  # name is explicitly set to expose the entity to the frontend
  name: "GP bollar test light"
  output: gp_bollar_test_template
  platform: binary

But effects will not work. Effects seem to only work for RGB/RGBW/RGBWW lights. And they all require separate outputs for the color channels. Which could possibly be solved using templates. But that would probably require some sort of template ninja.