How do I get a 'template' switch as a light?

While I’m waiting for my two relay esp01s boards, I’ve started doing the small code for it.
The ‘double relay’ boards uses a template switch like this:

switch:
  - platform: template
    name: '${device_name} Mode change'
    id: relay_led
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3]
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]

I tried using this to get it show up as a light:

output:
  - platform: template
    type: binary
    id: light_relay
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3]
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]      
light:
  - platform: binary
    id: relay
    name: Floorlamp
    output: light_relay

But I can’t do a turn_on_action or turn_off_action apparently in output?

Yeah it looks like you only have on_write_action for a template output. https://esphome.io/components/output/template.html#output-template-on-write-action

So you will need condition(s) to determine which action to take.

I see, I’ve changed it to this, I hope it works, but I’ll have to see when the boards arrive, at least it’s accepted by the compiler now :smiley:

substitutions:
  device_name: raclette

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifissid
  password: !secret wifipw

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name} Hotspot"
    password: !secret appw

# Enable Home Assistant API
api:

ota:
  password: !secret otapw

time:
  - platform: homeassistant
    id: homeassistant_time

# Enable logging
logger:
  baud_rate: 0 #need this to free up UART pins

uart:
  baud_rate: 115200 # speed to STC15L101EW
  tx_pin: GPIO1
  rx_pin: GPIO3
  
web_server:
  port: 80
  auth:
    username: !secret webuser
    password: !secret webpw

# Text Sensor with general information
text_sensor:
  - platform: version
    name: '${device_name} ESPHome Version'
  - platform: wifi_info
    ip_address:
      name: '${device_name} ip'
    ssid:
      name: '${device_name} ssid'

binary_sensor:
  - platform: gpio
    id: button
    internal: true
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: true
    on_press:
      - switch.toggle: relay

sensor:
  - platform: wifi_signal
    name: '${device_name} wifi signal'
    update_interval: 60s
    accuracy_decimals: 0
  - platform: uptime
    name: '${device_name} uptime'
    unit_of_measurement: days
    update_interval: 300s
    accuracy_decimals: 1
    filters:
      - multiply: 0.000011574

script:
  id: countdown
  then:
  - delay: 120min  # timer length
  - switch.turn_off: relay

switch:
  - platform: template
    name: '${device_name} Power'
    id: relay
    turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
      - script.execute: countdown
      - light.turn_on: relay2
    turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
      - script.stop: countdown
      - light.turn_off: relay2
    optimistic: true

output:
  - platform: template
    type: binary
    id: light_relay
    write_action:
      - if:
          condition:
            light.is_on: relay2
          then:
            uart.write: [0xA0, 0x02, 0x00, 0xA2]
          else:
            uart.write: [0xA0, 0x02, 0x01, 0xA3]
            
light:
  - platform: binary
    id: relay2
    name: Floorlamp
    output: light_relay
2 Likes

That looks like it should work.

1 Like