Best place to create templates?

Been playing around with ESPHome to get some door and window sensors. is there a preferred location for creating the template sensors (either in ESPHome or as automations in HA)? Just as a reference, i started down the path of doing them in ESPHome, but wondering if there is a better/easier way since I have dozens of these. here is the config in ESPHome for the first 5 I created:


# Basic Config
esphome:
  name: sonoff-bridge
  #to helo with disconnects
  on_loop:
    then:
    - lambda: delay(5);

  on_boot:
    then:
      - lambda: !lambda |-
          if (id(window_dining_room_e_l).state) {
            id(window_dining_room_e_l).publish_state(true);
          } else {
            id(window_dining_room_e_l).publish_state(false);
          }
          if (id(window_dining_room_e_r).state) {
            id(window_dining_room_e_r).publish_state(true);
          } else {
            id(window_dining_room_e_r).publish_state(false);
          }
          if (id(window_living_room_e_l).state) {
            id(window_living_room_e_l).publish_state(true);
          } else {
            id(window_living_room_e_l).publish_state(false);
          }
          if (id(window_living_room_e_r).state) {
            id(window_living_room_e_r).publish_state(true);
          } else {
            id(window_living_room_e_r).publish_state(false);
          }
          if (id(door_living_room).state) {
            id(door_living_room).publish_state(true);
          } else {
            id(door_living_room).publish_state(false);
          }
  

esp8266:
  board: esp01_1m

uart:
  tx_pin: 1
  rx_pin: 3
  baud_rate: 19200


wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: redacted
    gateway: redacted
    subnet: 255.255.255.0
    dns1: redacted
  power_save_mode: none
  fast_connect: true

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Sonoff-Bridge Fallback Hotspot"
    password: "password"

ota:
  password: "password"

captive_portal:
    
web_server:
  port: 80

# Enable logging
logger:
  level: DEBUG
  baud_rate: 0

# Enable Home Assistant API
api:
  services:
    - service: send_rf_code
      variables:
        sync: int
        low: int
        high: int
        code: int
      then:
        - rf_bridge.send_code:
            sync: !lambda 'return sync;'0x373C 
            low: !lambda 'return low;'0x01E00x0564
            high: !lambda 'return high;'
            code: !lambda 'return code;'
    - service: learn
      then:0x14A10A
        - rf_bridge.learn
    - service: advanced_learn_on
      then:
        - rf_bridge.start_advanced_sniffing
    - service: advanced_learn_off
      then:
        - rf_bridge.stop_advanced_sniffing
    - service: bucket_learn_on
      then:
        - rf_bridge.start_bucket_sniffing


rf_bridge:
  on_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'return format_hex(data.sync);'
            low: !lambda 'return format_hex(data.low);'
            high: !lambda 'return format_hex(data.high);'
            code: !lambda 'return format_hex(data.code);'

#################################################
## Dining room windows EAST - Left and Right   ##
## Left and Right as you face them from inside ##
#################################################
    ## Living Room Window EAST
      - if:
        # ON -  Received RFBridge Code: sync=0x3799 low=0x0199 high=0x0599 code=0x17999A
          condition:
            lambda: |-
              return data.code == 0x17999A;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_l0x14A10E
                state: ON
      - if:
        # OFF - Received RFBridge Code: sync=0x3788 low=0x0199 high=0x0599 code=0x17888E
          condition:
            lambda: |-
              return data.code == 0x17888E;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_l
                state: OFF
      - if:
        # ON -  Received RFBridge Code: sync=0x36C4 low=0x01EA high=0x0599 code=0x14777A
          condition:
            lambda: |-
              return data.code == 0x14777A;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_r
                state: ON
      - if:
        # OFF - Received RFBridge Code: sync=0x3799 low=0x01EA high=0x0550 code=0x14666E
          condition:
            lambda: |-
              return data.code == 0x14666E;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_r
                state: OFF
#################################################
## Living room windows EAST - Left and Right   ##0xABBD0E
## Left and Right as you face them from inside ##
#################################################0xAF790A
      - if:
      # ON -  Received RFBridge Code: sync=0x3799 low=0x0199 high=0x0599 code=0x17999A
          condition:
            lambda: |-
              return data.code == 0x17999A;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_l
                state: ON
      - if:
      # OFF - Received RFBridge Code: sync=0x3788 low=0x0199 high=0x0599 code=0x17888E
          condition:
            lambda: |-
              return data.code == 0x17888E;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_l
                state: OFF
      - if:
      # ON -  Received RFBridge Code: sync=0x36EC low=0x01EA high=0x055A code=0xAB555A
          condition:
            lambda: |-
              return data.code == 0xAB555A;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_r
                state: ON
      - if:
      # OFF - Received RFBridge Code: sync=0x36EC low=0x01F4 high=0x0550 code=0xAB555E
          condition:
            lambda: |-
              return data.code == 0xAB555E;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_r
                state: OFF

########################################
## Living room door - Top Deck Door   ##
########################################
      - if:
      # ON -  Received RFBridge Code: sync=0x3799 low=0x01F4 high=0x055A code=0xAF444A
          condition:
            lambda: |-
              return data.code == 0xAF444A;
          then:
            - binary_sensor.template.publish:
                id: door_living_room
                state: ON
      - if:
      # OFF - Received RFBridge Code: sync=0x3714 low=0x01EA high=0x055A code=0xAF444E
          condition:
            lambda: |-
              return data.code == 0xAF444E;
          then:
            - binary_sensor.template.publish:
                id: door_living_room
                state: OFF


  on_advanced_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_advanced_code_received
          data:
            length: !lambda 'char buffer [10];return itoa(data.length,buffer,16);'
            protocol: !lambda 'char buffer [10];return itoa(data.protocol,buffer,16);'
            code: !lambda 'return data.code;'


# Device Specific Config
binary_sensor:
  - platform: status
    name: "RF Bridge Status"

# Dining room windows facing east
  - platform: template
    # ON -  Received RFBridge Code: sync=0x3799 low=0x0199 high=0x0599 code=0x17999A
    # OFF - Received RFBridge Code: sync=0x3788 low=0x0199 high=0x0599 code=0x17888E
    name: "ESP Dining Room Window - East Left"
    id: window_dining_room_e_l
    device_class: window
  - platform: template
    # ON -  Received RFBridge Code: sync=0x36C4 low=0x01EA high=0x0599 code=0x14777A
    # OFF - Received RFBridge Code: sync=0x3799 low=0x01EA high=0x0550 code=0x14666E
    name: "ESP Dining Room Window - East Right"
    id: window_dining_room_e_r
    device_class: window

# Living Room Windows facing east
  - platform: template
    # ON -  Received RFBridge Code: sync=0x3799 low=0x0199 high=0x0599 code=0x17999A
    # OFF - Received RFBridge Code: sync=0x3788 low=0x0199 high=0x0599 code=0x17888E
    name: "ESP Living Room Window - East Left"
    id: window_living_room_e_l
    device_class: window
  - platform: template
    # ON -  Received RFBridge Code: sync=0x36EC low=0x01EA high=0x055A code=0xAB555A
    # OFF - Received RFBridge Code: sync=0x36EC low=0x01F4 high=0x0550 code=0xAB555E
    name: "ESP Living Room Window - East Right"
    id: window_living_room_e_r
    device_class: window

# Living Room Door
  - platform: template
    # ON -  Received RFBridge Code: sync=0x3799 low=0x01F4 high=0x055A code=0xAF444A
    # OFF - Received RFBridge Code: sync=0x3714 low=0x01EA high=0x055A code=0xAF444E
    name: "ESP Living Room Door"
    id: door_living_room
    device_class: door

I do mine in HA. There are a bunch of threads discussing the best way to do this, I personally use a Python script to parse the codes but there are plenty of other ways.

I prefer to keep everything in ESPHome these days if possible and it’s not too honerous.

HA can be down but the Esp keeps doing its thing. So typically more reliable.

2 Likes

Thanks Gents. It seems to be a tossup. Probably easier to code in HA, but better availability in ESPHome. For me, the availability is not as much a concern since ESPHome runs in the same machine as HA

The availability/reliability benefits I’m thinking of tend to be more for when you have a sensor and something to control attached to the ESP.

Eg for example bright light could control blinds. Or heat/humidity sensor could close windows. Device beeps/flashes when CO2 is high.

That could all keep working even if HA and modems are turned off and internet is down etc.

Yepp, a good example seen is an ESP with am BME sensor.
Some really ask HA for doing a dewpoint calculation and more.

Whereas all what’s needed could be done on the ESPdevice itself, better said if you need to do calculations/templates by values generated by the ESP the resulting actions could get handled by that device aswell. Even in case of additional state need to be taken into account fetching them from HA since provided by a differnt device is easy.

I got lots of smaller yaml being “!incude”(ed) from a main.yaml per ESP device, for the above example things such as dewpoint, humiindex, heatindex, summersimmerindex (last one does require fetching the actual wind-strength provided by another device not by the one with the bme sensor).

And in the end all what happens is the ESP does send states, the one from the hardware sensors aswell as the ones resulting from doing calculations.

Not to mention that informations calculated on the HA side are free from sense at the moment the device providing the required source informations suffered an error. No dewpoint without temperature and humidity and since so no benefit splitting it up.

For me that depends:

If the esphome nodes is able to “calculate”/build the template on it’s own I always prefer to do it directly on the node. Just because I don’t need to clutter HA more than necessary.

For example having a esphome node with temperature, humidity and pressure sensor I calculate the template sensors for absolute humidity, dew point and altitude directly in esphome. :point_down:

Calculating in HA should give the same results but it will 100% depend on that esphome node to deliver it’s data - so why not directly send the “correct”/prepared/processed data from the esphome node to HA?

You mixing up things here. You run the ESPHome dashboard on your HA machine which is just to create/edit/debug esphome nodes but it doesn’t do anything “on it’s own”.

All logic you have in your yaml’s (like templates sensor) are processed on that very esphome node - even when HA (together with your esphome dashboard addon) is down :bulb:

1 Like

I feel similarly about putting things in ESPHOME to help keep HA clutter down.

It’s probably a preference thing, but I find having things in the ESPHOME config is tidier as it groups related logic in one file.

And once you get used to the ESPHome way of writing automations (on_…) and using filters I think it really grows on you.

Back on the template topic, not being able to template icons in ESPHOME annoys me sometimes.

thanks all, appreciate the insights!

1 Like