Measuring wind speed

Here is code I use for wind speed/gust measurement (on ESP32), works OK.
It calculates min/max gust over predefined periods + avg for wind speed.

in globals section:

globals:
# global variables for wind/rain sensors
  - id: g_wind_run_count
    type: int
    restore_value: no
    initial_value: '0'
  - id: g_rain_count
    type: int
    restore_value: no
    initial_value: '0'
  - id: gt_rain_hour
    type: byte[60]
    restore_value: no
  - id: g_minute
    type: byte
    restore_value: no
    initial_value: '0'
  - id: g_WindGust_10s
    type: float
    restore_value: no
    initial_value: '0.0'
  - id: g_WindSpeedMin_10s
    type: float
    restore_value: no
    initial_value: '9999.9'
  - id: g_WindGust_60s
    type: float
    restore_value: no
    initial_value: '0.0'
  - id: g_WindGust_10min
    type: float
    restore_value: no
    initial_value: '0.0'

wind sensor:

sensor:
  - platform: pulse_meter
  # # https://www.sparkfun.com/datasheets/Sensors/Weather/Weather%20Sensor%20Assembly..pdf
  # # The cup-type anemometer measures wind speed by closing a contact
  # # as a magnet moves past a switch. A wind speed of 1.492 MPH (2.4 km/h)
  # # causes the switch to close once per second.
  # # 1rps = 60rpm = 2.4kmh
  # # 1pulse/min = 2.4kmh/60 = 0.04kmh

  # It measures the time between rising edges on a pin, for each pulse it outputs the frequency in pulses/min.
    id: id_wind_speed
    pin: GPIO26
    internal: true
    # internal_filter_mode: EDGE
    internal_filter: 10 ms
    unit_of_measurement: 'km/h'
    name: "Roof Wind speed"
    icon: 'mdi:weather-windy'
    accuracy_decimals: 7
    timeout: '5s'
    filters:
    - multiply: 0.04 #kmh
    on_value:
      - lambda: |-
          //id(g_wind_run) += (x>0) ? 0.000666982 : 0;
          if(x > id(g_WindGust_10s)) {
            id(g_WindGust_10s) = x;
            id(id_wind_gust_10s).publish_state(id(g_WindGust_10s));
          };
          if(x > id(g_WindGust_60s)) {
            id(g_WindGust_60s) = x;
            id(id_wind_gust_60s).publish_state(id(g_WindGust_60s));
          };
          if(x > id(g_WindGust_10min)) {
            id(g_WindGust_10min) = x;
            id(id_wind_gust_10min).publish_state(id(g_WindGust_10min));
          };
          if(id(g_WindSpeedMin_Reset)){
            id(g_WindSpeedMin_10s) = x;
            id(g_WindSpeedMin_Reset) = false;
          } else {
            if(x < id(g_WindSpeedMin_10s)) {id(g_WindSpeedMin_10s) = x;};
          }
    total:
      name: "Roof Wind Run daily"
      id: id_wind_run_daily
      internal: false
      unit_of_measurement: "km"
      accuracy_decimals: 3
      filters:
        - multiply: 0.000666982
        - throttle: 30s

Different wind speeds/gusts calculations inside template sensors:

  - platform: template
    name: "Roof Wind Run daily"
    icon: "mdi:weather-windy"
    unit_of_measurement: "km"
    accuracy_decimals: 3
    update_interval: 30s
    internal: true
    lambda: |-
      return id(g_wind_run_count)*0.000666982;
    id: id_wind_run_daily_g

  - platform: template
    name: "Roof Wind gust 10s"
    unit_of_measurement: 'km/h'
    update_interval: 10s
    id: id_wind_gust_10s
    lambda: return id(g_WindGust_10s);
    

  - platform: template
    name: "Roof Wind gust 60s"
    unit_of_measurement: 'km/h'
    state_class: "measurement"
    update_interval: 60s
    id: id_wind_gust_60s
    lambda: return id(g_WindGust_60s);
    
  - platform: template
    name: "Roof Wind gust 10min"
    unit_of_measurement: 'km/h'
    state_class: "measurement"
    update_interval: 10min
    id: id_wind_gust_10min
    lambda: return id(g_WindGust_10min);

  - platform: template
    name: "Roof Wind speed avg 1s"
    unit_of_measurement: 'km/h'
    update_interval: 333ms
    lambda: |-
      return id(id_wind_speed).state;
    filters:
      throttle_average: 1s
    id: id_wind_speed_avg_1s

  - platform: template
    name: "Roof Wind speed avg 10s"
    unit_of_measurement: 'km/h'
    update_interval: 333ms
    lambda: |-
      return id(id_wind_speed).state;
    filters:
      throttle_average: 10s
    id: id_wind_speed_avg_10s

  - platform: template
    name: "Roof Wind Speed Min 10s"
    unit_of_measurement: 'km/h'
    id: id_wind_speed_min_10s

  - platform: template
    name: "Roof Wind speed avg 1min"
    unit_of_measurement: 'km/h'
    state_class: "measurement"
    update_interval: '1s'
    lambda: |-
      return id(id_wind_speed).state;
    filters:
      - throttle_average: 1min
    id: id_wind_speed_avg_1min

  - platform: template
    name: "Roof Wind speed avg 10min"
    unit_of_measurement: 'km/h'
    state_class: "measurement"
    update_interval: 1s
    lambda: |-
      return id(id_wind_speed).state;
    filters:
      - throttle_average: 10min
    id: id_wind_speed_avg_10min

with help of interval:

interval:
  - interval: 10s
    then:
      - lambda: |-
          //id(id_wind_gust_10s).publish_state(id(g_WindGust_10s));
          id(id_wind_speed_min_10s).publish_state(id(g_WindSpeedMin_10s));
          //id(id_wind_gust_60s).publish_state(id(g_WindGust_60s));
          //id(id_wind_gust_10min).publish_state(id(g_WindGust_10min));
          id(g_WindGust_10s) = 0;
          // id(g_WindSpeedMin_10s) = 9999.9;
          id(g_WindSpeedMin_Reset) = true;
  - interval: 60s
    then:
      - lambda: |-
          id(g_WindGust_60s) = 0;
          //maintain rain
          if (++id(g_minute) > 59) id(g_minute) = 0;
          id(gt_rain_hour)[id(g_minute)] = 0;
  - interval: 10min
    then:
      - lambda: |-
          id(g_WindGust_10min) = 0;
1 Like