Trouble with Code/Template/Sensors for Wind Direction

Hello,

I’m new to all of this. I’m building a weather station with my students. I have a weather vane that has four hall sensors in it to measure wind direction. I have no issues with the binary_sensor code to get the weather vane to measure the four cardinal directions. It works perfectly.

However, I have another portion of code that is supposed to allow the weather vane to also measure NW, SW, NE, and SE (When the North and West hall sensors are both triggered, it will read NW for the direction), but I cannot get this code to work. I am borrowing this code from someone else and it appears to be correct. I run it through a yaml checker and it says the code is good. ChatGBT says it’s good. I’ve seen several people on this forum who have the exact same code, with the same indentation in the same blocks.

When I validate the code in ESP Home, it says, “[sensors] is an invalid option for [sensor.template]. Please check the indentation.” If I adjust the indentation to make the red line go away, it says “mapping values are not allowed here.” It does not like the “sensors” line.

I have tried to put this file in my main yaml file. I have tried to put it in the configuration.yaml file, like most people say to do and I don’t get any errors, but it doesn’t work. It shows the dash in red though. I’ve tried to put the code under binary_sensor and I’ve tried every other possible option, but it still doesn’t work.

Oddly enough, I do see the Wind Direction Sensor listed under Sensor now, but there is no reading.

I must be missing something. Am I supposed to create something for the “platform: template” somewhere else? Do I need to create something for “sensors” somewhere else? Is there some other process about creating a template that I’m not doing?

I have been trying to fix this issue for weeks. Any help will be greatly appreciated, but please talk to me like a child. This is not my area of expertise. Thank you for reading all of this! Also, if this should be posted in another topic, please let me know.

Here is my yaml file:

esphome:
  name: dht22
  friendly_name: DHT22

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_encryption_key

ota:
  password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "bla bla"
    password: "555555"
  
  manual_ip:
    static_ip: 555.555.5.555
    gateway: 555.555.5.5
    subnet: 555.555.555.5

captive_portal:

i2c:
  id: bus_a 
  sda: 21
  scl: 22
  scan: true


sensor:
  - platform: bmp085
    i2c_id: bus_a
    address: 0x77
    temperature:
      filters:
      - lambda: return x * (9.0/5.0) + 32.0;
      unit_of_measurement: "°F"
      name: "Outside Temperature"
    pressure:
      name: "Outside Pressure"
      unit_of_measurement: "mmHg"
      filters:
        - multiply: 0.75006375541921
    update_interval: 60s

  - platform: dht
    pin: 4
    temperature:
      filters:
      - lambda: return x * (9.0/5.0) + 32.0;
      unit_of_measurement: "°F"
      name: "Outside Temperature"
    humidity:
      name: "Outside Humidity"
    update_interval: 60s
    model: DHT22 

  - platform: pulse_counter    
    pin: 19    
    unit_of_measurement: 'mph'   
    name: 'Wind Speed'    
    filters:      
      - multiply: 0.18    
    update_interval: 5s 

  - platform: pulse_counter
    pin: 23
    count_mode:
      rising_edge: INCREMENT
      falling_edge: INCREMENT
    unit_of_measurement: 'in'
    name: 'Instant Rain'
    filters:
      - multiply: 0.173

    total:
      unit_of_measurement: 'in'
      name: 'Rain'
      accuracy_decimals: 3
      filters:
        - multiply: 0.173
    update_interval: 5s

  - platform: uptime
    name: Uptime
  
  - platform: ina219
    i2c_id: bus_a
    address: 0x40
    current:
     name: "Solar Panel Current"
    power: 
     name: "Solar Panel Power"
    bus_voltage: 
     name: "Solar Panel Bus Voltage"
    shunt_voltage: 
     name: "Solar Panel Shunt Voltage"
    max_voltage: 32.0V
    max_current: 3.2A
    update_interval: 60s

  - platform: ina219
    i2c_id: bus_a
    address: 0x41
    current:
     name: "Battery Current"
    power: 
     name: "Battery Power"
    bus_voltage: 
     name: "Battery Bus Voltage"
    shunt_voltage: 
     name: "Battery Shunt Voltage"
    max_voltage: 32.0V
    max_current: 3.2A
    update_interval: 60s
  
  - platform: template
    sensors: 
      wind_direction: 
        friendly_name: Wind Direction
        value_template: >-
          {% if states('binary_sensor.vento_direzione_ovest') == 'off' and states('binary_sensor.vento_direzione_nord') == 'off' %}
            NORTH-WEST
          {% elif states('binary_sensor.vento_direzione_est') == 'off' and states('binary_sensor.vento_direzione_nord') == 'off' %}
            NORTH-EAST
          {% elif states('binary_sensor.vento_direzione_ovest') == 'off' and states('binary_sensor.vento_direzione_sud') == 'off' %}
            SOUTH-WEST
          {% elif states('binary_sensor.vento_direzione_est') == 'off' and states('binary_sensor.vento_direzione_sud') == 'off' %}
            SOUTH-EAST	
          {% elif states('binary_sensor.vento_direzione_nord') == 'off' %}
            NORTH
          {% elif states('binary_sensor.vento_direzione_est') == 'off' %}
            EAST
          {% elif states('binary_sensor.vento_direzione_sud') == 'off' %}
            SOUTH
          {% elif states('binary_sensor.vento_direzione_ovest') == 'off' %}
            WEST
          {% endif %} 

binary_sensor:  
      - platform: gpio    
        pin: 33    
        name: "Wind Direction North"      
      - platform: gpio    
        pin: 25    
        name: "Wind Direction East"      
      - platform: gpio    
        pin: 26    
        name: "Wind Direction West"      
      - platform: gpio    
        pin: 27    
        name: "Wind Direction South" 

      - platform: status
        name: "Wind Direction"

ESPHome uses a different syntax for both its lambda-based Template Sensors and YAML configuration… what you have posted is using the Jinja templating syntax and Home Assistant YAML configuration. They are not interchangable.

Thank you. So my syntax is all wrong in the configuration file that I posted a screenshot? But all of the rest of my code is okay in my yaml file that I posted and isn’t it written in Jinja too? So I will need to learn how to translate my Jinja code to the code in the link you sent?

I found the solution. Thanks to Dideridrew for pointing me in the right direction. He was correct, my formatting was incorrect for the platform: template and it was using Jinga syntax instead of ESPHome syntax, which I think uses C++ for its lambdas.

The solution:

  1. I needed to create IDs for my four binary sensors.

  2. I needed to create a text_sensor with the correct formatting of the platform: template for ESPHome.

  3. I needed to figure out the correct syntax for my “if, else” statements underneath the lamba. Bingo! Everything works properly. I hope this will help someone else.

I put this code in my main file (my only file) with all of my sensors, etc. I did not put it in the configuration.yaml file that I posted as a screenshot.

Here is the code that I needed to fix my issue:

text_sensor:
  - platform: template
    name: "Wind Direction"
    lambda: |-
      if (id(north_binary_sensor).state && !id(east_binary_sensor).state && !id(south_binary_sensor).state && !id(west_binary_sensor).state) {
        return {"NORTH"};
      } else if (!id(north_binary_sensor).state && id(east_binary_sensor).state && !id(south_binary_sensor).state && !id(west_binary_sensor).state) {
        return {"EAST"};
      } else if (!id(north_binary_sensor).state && !id(east_binary_sensor).state && id(south_binary_sensor).state && !id(west_binary_sensor).state) {
        return {"SOUTH"};
      } else if (!id(north_binary_sensor).state && !id(east_binary_sensor).state && !id(south_binary_sensor).state && id(west_binary_sensor).state) {
        return {"WEST"};
      } else if (id(north_binary_sensor).state && id(east_binary_sensor).state && !id(south_binary_sensor).state && !id(west_binary_sensor).state) {
        return {"NORTH-EAST"};
      } else if (id(north_binary_sensor).state && !id(east_binary_sensor).state && !id(south_binary_sensor).state && id(west_binary_sensor).state) {
        return {"NORTH-WEST"};
      } else if (!id(north_binary_sensor).state && id(east_binary_sensor).state && id(south_binary_sensor).state && !id(west_binary_sensor).state) {
        return {"SOUTH-EAST"};
      } else if (!id(north_binary_sensor).state && !id(east_binary_sensor).state && id(south_binary_sensor).state && id(west_binary_sensor).state) {
        return {"SOUTH-WEST"};
      } else if (id(north_binary_sensor).state && id(east_binary_sensor).state && !id(south_binary_sensor).state && id(west_binary_sensor).state) {
        return {"NORTH"};
      } else if (id(north_binary_sensor).state && id(east_binary_sensor).state && id(south_binary_sensor).state && !id(west_binary_sensor).state) {
        return {"EAST"};
      } else if (!id(north_binary_sensor).state && id(east_binary_sensor).state && id(south_binary_sensor).state && id(west_binary_sensor).state) {
        return {"SOUTH"};
      } else if (id(north_binary_sensor).state && !id(east_binary_sensor).state && id(south_binary_sensor).state && id(west_binary_sensor).state) {
        return {"WEST"};
      } else {
        return {"UNKNOWN"};
      }
    update_interval: 5s

binary_sensor:  
      - platform: gpio    
        pin: 33    
        name: "Wind Direction North" 
        id: north_binary_sensor    
      - platform: gpio    
        pin: 25    
        name: "Wind Direction East" 
        id: east_binary_sensor    
      - platform: gpio    
        pin: 26    
        name: "Wind Direction West"   
        id: west_binary_sensor  
      - platform: gpio    
        pin: 27    
        name: "Wind Direction South" 
        id: south_binary_sensor

      - platform: status
        name: "Wind Direction"
1 Like