volca / april-soil-esphome-module

Custom april soil moisture sensor module for esphome
https://wiki.aprbrother.com/en/april_soil_moisture_sensor.html
2 stars 1 forks source link

Updating april-soil.yaml to work on two devices #3

Closed peterbarbosa closed 3 months ago

peterbarbosa commented 3 months ago

Hello,

I have successfully flashed your code onto one of my April Soil Sensors, but when I create a new project, copy the code and change the device name, it seems like the new device doesnt work.

I can see the new entry on MQTT HA, but it doesn't show any sensors listed.

Here is the code between the two devices:

esphome:
  name: 1-soil-moisture
  includes:
      - april_soil_sensor.h
  libraries:
      - Wire

esp32:
  board: esp32-s2-saola-1

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

mqtt:
  broker: 192.168.40.109
  username: zigbee
  password: !secret mqtt_password

logger:
  level: DEBUG

sensor:
  - platform: adc
    pin: 7
    attenuation: 11dB
    raw: true
    id: "vbatt"
    name: "Battery Voltage"
    update_interval: 30s
    disabled_by_default: true
    device_class: "voltage"
    state_class: "measurement"
    icon: "mdi:battery-outline"
    accuracy_decimals: 2
    entity_category: "diagnostic"

# derive battery percentage 3300mV=0%, 3950mV=100%
  - platform: template
    lambda: |-
      float x = id(vbatt).state;
      if (x < 3300) {
        x = 3300;
      }
      if (x > 3950) {
        x = 3950;
      }
      float y = (x - 3300) / 650 * 100;
      return y;
    name: "Battery Percentage"
    update_interval: 5s
    unit_of_measurement: "%"
    device_class: "battery"
    state_class: "measurement"
    icon: "mdi:battery"
    accuracy_decimals: 0

  - platform: adc
    pin: 9
    raw: true
    id: rMoisture
    name: "Raw Moisture reading"
    update_interval: never
    unit_of_measurement: raw
    disabled_by_default: true
    state_class: "measurement"
    icon: "mdi:water-alert"
    entity_category: "diagnostic"

#derive moisture level 3800=100% 6800=0%
  - platform: template
    name: "Moisture Level (Instant)"
    id: "iMoisture"
    unit_of_measurement: "%"
    update_interval: never
    state_class: "measurement"
    disabled_by_default: true
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      float x = id(rMoisture).state;
      if (x < 3800) {
        x = 3800;
      }
      if (x > 6800) {
        x = 6800;
      } 
      float y = (x - 3800) / 3000 * 100;
      y = 100 - y;
      return y;

# average over 5 readings to smooth
  - platform: template
    name: "Moisture Level"
    id: "aMoisture"
    unit_of_measurement: "%"
    update_interval: 5s
    state_class: "Measurement"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(iMoisture).state;
    filters:
    - quantile:
        window_size: 5
        send_every: 5
        send_first_at: 5
        quantile: 0.9

output:
  - platform: ledc
    pin: 17
    frequency: 1500000Hz
    id: moisture_gen

interval:
  - interval: 1500ms
    then:
      - output.turn_on: moisture_gen
      - output.set_level:
          id: moisture_gen
          level: 34%
      - delay: 500ms
      - component.update: rMoisture
      - component.update: iMoisture
      - component.update: aMoisture
      - output.turn_off: moisture_gen

deep_sleep:
  run_duration:
    default: 1min
  sleep_duration: 360min
  wakeup_pin: 2
esphome:
  name: 2-soil-moisture
  includes:
      - april_soil_sensor.h
  libraries:
      - Wire

esp32:
  board: esp32-s2-saola-1

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

mqtt:
  broker: 192.168.40.109
  username: zigbee
  password: !secret mqtt_password

logger:
  level: DEBUG

sensor:
  - platform: adc
    pin: 7
    attenuation: 11dB
    raw: true
    id: "vbatt"
    name: "Battery Voltage"
    update_interval: 30s
    disabled_by_default: true
    device_class: "voltage"
    state_class: "measurement"
    icon: "mdi:battery-outline"
    accuracy_decimals: 2
    entity_category: "diagnostic"

# derive battery percentage 3300mV=0%, 3950mV=100%
  - platform: template
    lambda: |-
      float x = id(vbatt).state;
      if (x < 3300) {
        x = 3300;
      }
      if (x > 3950) {
        x = 3950;
      }
      float y = (x - 3300) / 650 * 100;
      return y;
    name: "Battery Percentage"
    update_interval: 5s
    unit_of_measurement: "%"
    device_class: "battery"
    state_class: "measurement"
    icon: "mdi:battery"
    accuracy_decimals: 0

  - platform: adc
    pin: 9
    raw: true
    id: rMoisture
    name: "Raw Moisture reading"
    update_interval: never
    unit_of_measurement: raw
    disabled_by_default: true
    state_class: "measurement"
    icon: "mdi:water-alert"
    entity_category: "diagnostic"

#derive moisture level 3800=100% 6800=0%
  - platform: template
    name: "Moisture Level (Instant)"
    id: "iMoisture"
    unit_of_measurement: "%"
    update_interval: never
    state_class: "measurement"
    disabled_by_default: true
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      float x = id(rMoisture).state;
      if (x < 3800) {
        x = 3800;
      }
      if (x > 6800) {
        x = 6800;
      } 
      float y = (x - 3800) / 3000 * 100;
      y = 100 - y;
      return y;

# average over 5 readings to smooth
  - platform: template
    name: "Moisture Level"
    id: "aMoisture"
    unit_of_measurement: "%"
    update_interval: 5s
    state_class: "Measurement"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(iMoisture).state;
    filters:
    - quantile:
        window_size: 5
        send_every: 5
        send_first_at: 5
        quantile: 0.9

output:
  - platform: ledc
    pin: 17
    frequency: 1500000Hz
    id: moisture_gen

interval:
  - interval: 1500ms
    then:
      - output.turn_on: moisture_gen
      - output.set_level:
          id: moisture_gen
          level: 34%
      - delay: 500ms
      - component.update: rMoisture
      - component.update: iMoisture
      - component.update: aMoisture
      - output.turn_off: moisture_gen

deep_sleep:
  run_duration:
    default: 1min
  sleep_duration: 360min
  wakeup_pin: 2

Is there something I'm missing or should be changing so I can have more then 1 April soil sensor?

peterbarbosa commented 3 months ago

Here is what Im seeing in MQTT/HA.

Both devices show up, just only 1 has sensors. Ive tested both boards, and both were OK until I connect the other to the network.

Screenshot 2024-07-27 124326 cedar soil
peterbarbosa commented 3 months ago

Okay, I've renamed the entities and entities ID for each sensors project - and that seems to have fixed it!

volca commented 3 months ago

Sorry for reply delay. Thanks for you update also!