Automation question - turn on switch with on_value_range

I am slowly building out my project piece by piece and learning on the way (kinda :wink:). Currently it works as anticipated but the next portion is not doing anything. I am sure I am probably off in my logic so any suggestions is appreciated. I want to monitor my probe and fire one switch for a value below a range, fire another switch for a value above a range. Only have below in right now. It never fires. At one point I thought I saw on the terminal after installing it claimed it fired but I was away and so I did not hear pump kick in. thoughts?

esphome:
  name: ph-controller

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "kKF2ppGTmvTHXSz/kUSrtsV9Tu63i4XMzbwe4Ko6he4="

ota:
  password: "8b852dd72ca12db2f515b5c0a3311032"

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

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

captive_portal:


switch:
  - platform: gpio
    pin: 
      number: GPIO14
      inverted: False
    name: ph_up
    id: ph_up_pn2222_base
    on_turn_on:
    - delay: 4000ms
    - switch.turn_off: ph_up_pn2222_base
    - logger.log: "1Tsp Fill PH UP"
  - platform: gpio
    pin: 
      number: GPIO12
      inverted: False
    name: ph_down
    id: ph_down_pn2222_base
    on_turn_on:
    - delay: 4000ms
    - switch.turn_off: ph_down_pn2222_base
    - logger.log: "1Tsp Fill PH DOWN"
  - platform: gpio
    pin: 
      number: GPIO2
    name: led_pin
    id: led_pinz 
i2c:
  sda: GPIO4
  scl: GPIO5
  scan: True
  id: bus_a
  setup_priority: -200
sensor:
  - platform: ezo
    id: ph_ezo
    address: 99
    unit_of_measurement: "pH"
    accuracy_decimals: 2
    name: ph_reading
    on_value_range:
      below: 5
      then:
        - switch.turn_on: ph_up_pn2222_base
    update_interval: 10s

I might send data to HA where I can monitor the states of the sensor and then use Threshold or Bayesian Helper in an automation. That way I can see exactly what is going from on from either the ESPHome logs and HA history. Or keep trying with the code! I am still not an expert but I might have used a lambada

Thanks. I started down that path just using a script - still learning it all and was not successful. I am sending that value to the Dashboard for graphing. I have only used a bit of lambda for oled display but did come across using it for averaging that number. The value reported is pretty accurate, so I have not dug into more on lambda. I decided against chasing the script as I thought it could get kind of messy with as many as I might need and confusing. Have not heard of Threshold or Bayesian helper but I will look into that.

[16:44:11][D][sensor:126]: 'ph_reading': Sending state 3.56100 pH with 2 decimals of accuracy

From on_value_range: docs:

if a sensor value passes from outside a defined range of values to inside a range. 

So using this will only trigger when the sensor value crosses your value.

If you want to trigger when value below range - check in lambda instead.

1 Like

Ah, I see. Thanks! I will look into lambda.

Here’s an unfortunately slightly complex one from my code but gives you an example of some tests:

  - platform: dallas
    address: 0x84011441BF60AA28
    id: proof_temp
    name: "Proofing Box Temperature"  
    unit_of_measurement: "°C"
    on_value:
      then:
        # if (system enabled AND HA override off AND above or below
        # target) THEN turn OFF heat or cool
        - if: 
            condition:
              and:
                - binary_sensor.is_on: enabled
                - switch.is_off: override  
            then:
              if: 
                condition:
                  - lambda: 'return id(proof_temp).state > id(target_temp).state;'
                  - lambda: 'return id(mode_flag);'
                then:
                  - switch.turn_off: proof_heat_ena
                else:
                  if:
                    condition:
                      - lambda: 'return id(proof_temp).state < id(target_temp).state;'
                    then:
                      - switch.turn_off: proof_cool_ena        
        # if (system enabled AND HA override off AND below 
        # target) THEN turn ON heat or cool
        - if: 
            condition:
              and:
                - binary_sensor.is_on: enabled
                - switch.is_off: override  
            then:
              if: 
                condition:
                  - lambda: 'return id(proof_temp).state < id(target_temp).state;'
                  - lambda: 'return id(mode_flag);'
                then:
                  - switch.turn_on: proof_heat_ena
                else:
                  if:
                    condition:
                      - lambda: 'return id(proof_temp).state > id(target_temp).state;'
                    then:
                      - switch.turn_on: proof_cool_ena    
1 Like

Thanks,
ended up being simpler than i expected. obviously, a lot of additional work to refine.