# Bresser 5-in-1 / Bresser 6-in-1               -*-hass-*-

mqtt:

  sensor:

    - name: Bresser51 Temperature
      state_topic: rtl_433/bresser51/temperature_C
      unique_id: bresser51_temperature_C
      value_template: "{{ value | float }}"
      unit_of_measurement: "⁰C"
      device_class: temperature

    - name: Bresser51 Humidity
      state_topic: rtl_433/bresser51/humidity
      unique_id: bresser51_humidity
      value_template: "{{ value | int }}"
      unit_of_measurement: "%"
      device_class: humidity

    - name: Bresser51 Wind Gust
      state_topic: rtl_433/bresser51/wind_max_m_s
      unique_id: bresser51_wind_max_m_s
      value_template: "{{ value | float }}"
      unit_of_measurement: "m/s"
      icon: mdi:weather-windy

    - name: Bresser51 Wind Speed
      state_topic: rtl_433/bresser51/wind_avg_m_s
      unique_id: bresser51_wind_avg_m_s
      value_template: "{{ value | float }}"
      unit_of_measurement: "m/s"
      icon: mdi:weather-windy

    - name: Bresser51 Wind Direction Unadjusted
      state_topic: rtl_433/bresser51/wind_dir_deg
      unique_id: bresser51_wind_dir_deg_unadj
      value_template: "{{ value | int }}"
      unit_of_measurement: "⁰"
      icon: mdi:compass-rose

    - name: Bresser51 Rain
      state_topic: rtl_433/bresser51/rain_mm
      unique_id: bresser51_rain_mm
      value_template: "{{ value | float }}"
      unit_of_measurement: "mm"
      icon: mdi:weather-rainy

    - name: Bresser51 Time
      state_topic: rtl_433/bresser51/time
      unique_id: bresser51_time
      icon: mdi:clock-outline

  binary_sensor:

    - name: Bresser51 Battery
      state_topic: rtl_433/bresser51/battery_ok
      unique_id: bresser51_battery_ok
      payload_on: "0"
      payload_off: "1"
      device_class: battery

# Not as practical/reliable as I thought. See SQL approach below.
#
# sensor:
#
#     # Sample rate = 3/s max. 1h = 180, 24h = 4320, 168h = 30240.
#
#     - name: Bresser51 Rain 1H
#       platform: statistics
#       unique_id: bresser51_rain_1h
#       entity_id: sensor.bresser51_rain
#       sampling_size: 200
#       state_characteristic: change
#       max_age:
#         hours: 1
#
#     - name: Bresser51 Rain 24H    # day
#       platform: statistics
#       unique_id: bresser51_rain_24h
#       entity_id: sensor.bresser51_rain
#       sampling_size: 4500
#       state_characteristic: change
#       max_age:
#         hours: 24
#
#     - name: Bresser51 Rain 168H   # week
#       platform: statistics
#       unique_id: bresser51_rain_168h
#       entity_id: sensor.bresser51_rain
#       sampling_size: 32000
#       state_characteristic: change
#       max_age:
#         hours: 168

# Fetching history data from recorder db.
# Must now be done via UI now. Such a shame!

sensor:

  - platform: sql
    db_url: !secret recorder_db
    queries:
      - name: Bresser51 DB Rain 1H
        query: >-
          SELECT * FROM states
          WHERE entity_id = 'sensor.bresser51_rain'
                AND last_updated < CURRENT_TIMESTAMP - INTERVAL '1 hour'
          ORDER BY last_updated DESC
          LIMIT 1;
        column: state
        unit_of_measurement: mm
      - name: Bresser51 DB Rain 24H
        query: >-
          SELECT * FROM states
          WHERE entity_id = 'sensor.bresser51_rain'
                AND last_updated < CURRENT_TIMESTAMP - INTERVAL '24 hour'
          ORDER BY last_updated DESC
          LIMIT 1;
        column: state
        unit_of_measurement: mm
      - name: Bresser51 DB Rain 168H
        query: >-
          SELECT * FROM states
          WHERE entity_id = 'sensor.bresser51_rain'
                AND last_updated < CURRENT_TIMESTAMP - INTERVAL '168 hour'
          ORDER BY last_updated DESC
          LIMIT 1;
        column: state
        unit_of_measurement: mm

input_number:
  bresser51_dir_corr:
    name: Direction Correction
    initial: 0
    min: -359
    max: 359
    step: 1
    mode: slider
    unit_of_measurement: "⁰"
    icon: mdi:axis-z-rotate-clockwise
    
template:

  sensor:

    # Adjust for not being north-oriented.
    - name: Bresser51 Wind Direction
      state: >-
        {{ (
             (states('sensor.bresser51_wind_direction_unadjusted')|int)
           - (states('input_number.bresser51_dir_corr')|int)
           ) % 360 }}
      unit_of_measurement: "⁰"
      icon: mdi:compass-rose

    # Map heading in degrees to discrete values 0..16
    # Note that the bresser sensor returns values:
    #  0 22 45 68 90 112 135 158 180 202 225 248 270 292 315 338 360?
    - name: Bresser51 Wind Direction Sector
      icon: mdi:compass-rose
      state: >-
        {{ (
        (states('sensor.bresser51_wind_direction')|int) /
        22 ) | int % 16 }}

    # The adjusted direction, in degrees. 0 22.5 45 67.5 ...
    - name: Bresser51 Wind Direction Sector Adjusted
      unit_of_measurement: "⁰"
      icon: mdi:compass-rose
      state: >-
        {{ (states('sensor.bresser51_wind_direction_sector')|int) * 22.5 }}

# {% set names = "N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW N".split() %}

    # Map sector to symbolic name.
    - name: Bresser51 Wind Direction Name
      icon: mdi:compass-rose
      state: |
        {% set names = "N NNO NO ONO O OZO ZO ZZO Z ZZW ZW WZW W WNW NW NNW N".split() %}
        {{ names[states('sensor.bresser51_wind_direction_sector')|int] }}

    # Wind force.
    - name: Bresser51 Wind Beaufort
      unit_of_measurement: "Bft"
      icon: mdi:windsock
      state: |
        {{(((states('sensor.bresser51_wind_speed') | float) / 0.836) ** (2/3)) | round(0, 'ceil')}}

    # Rain (last hour);
    - name: Bresser51 Rain 1H
      unit_of_measurement: "mm"
      icon: mdi:weather-rainy
      state: |
        {{ ((states('sensor.bresser51_rain') | float)
           - (states('sensor.bresser51_db_rain_1h') | float) ) | round(1) }} 

    # Rain (last 24 hours);
    - name: Bresser51 Rain 24H
      unit_of_measurement: "mm"
      icon: mdi:weather-rainy
      state: |
        {{ ((states('sensor.bresser51_rain') | float)
           - (states('sensor.bresser51_db_rain_24h') | float) ) | round(1) }} 

    # Rain (last 168 hours (week));
    - name: Bresser51 Rain 168H
      unit_of_measurement: "mm"
      icon: mdi:weather-rainy
      state: |
        {{ ((states('sensor.bresser51_rain') | float)
           - (states('sensor.bresser51_db_rain_168h') | float) ) | round(1) }} 

