Trigger automation when sensor remains below a certain value using ESPHome

Hi all,

I’m trying to turn off a “indicator” helper switch when my sensor stays below 175 for 4 mins. straight and only if the indicator switch is in the “ON” position for those 4 mins.

Here’s my attempt:

name: "Existence Energy"
      id: eval
      on_value:
        if:
          condition:
            for:
              time: 240s
              condition:
                and:                  
                   - switch.is_on: indicator
                   - sensor.in_range:
                       id: eval
                       below: 175
          then:
            - switch.turn_off: indicator
            - uart.write:
                data: "Indicator off"
                id: indicator_bus

It turns off when it should, but then the uart bus randomly sends the “Indicator off” message every now and then regardless of whether the indicator switch is on or off. There’s no real fixed time interval either - eg. it will randomly send “indicator off” once after 10 mins. and then again after 2 mins., and again after another 15 mins., etc. Not sure what’s happening.

You probably need to capture some logs and post them. The whole yaml config rather than just the bit you think is the issue is also sometimes handy - unless it’s enormous…

Here’s the full code. Not sure how to access logs - when device connects to wifi, it doesn’t show up as ‘Online’ on my esphomebuilder.

substitutions:
  name: "mmwave2"
  friendly_name: "mmwave2"

esphome:
  name: "${name}"
  friendly_name: "${friendly_name}"
  name_add_mac_suffix: true
  project:
    name: "seeedstudio.mmwave_kit"
    version: "3.0"
  platformio_options:
    board_build.flash_mode: dio
    board_build.mcu: esp32c3
  on_boot:
      then:
        - delay: 210s
        - repeat:
           count: 2
           then:
            - uart.write:
               id: uart_lorawan
               data: "AT+JOIN"
            - delay: 7s

# external_components:
#   - source: github://limengdu/mmwave-kit-external-components@main
#     refresh: 0s

esp32:
  board: esp32-c3-devkitm-1
  variant: esp32c3
  framework:
    type: esp-idf

logger:
  hardware_uart: USB_SERIAL_JTAG
  level: DEBUG

api:
  reboot_timeout: 0s    

ota:
  - platform: esphome

wifi:
  enable_on_boot: false
  ssid: *****
  password: *****
  reboot_timeout: 0s

uart:
  - id: uart_bus
    baud_rate: 115200
    rx_pin: 4
    tx_pin: 5
    parity: NONE
    stop_bits: 1
  - id: indicator_bus
    baud_rate: 9600
    rx_pin: 3
    tx_pin: 2
    parity: NONE
    stop_bits: 1

seeed_mr24hpc1:
  id: my_seeed_mr24hpc1
  uart_id: uart_bus

text_sensor:
  - platform: seeed_mr24hpc1
    heart_beat:
      name: "Heartbeat"
    product_model:
      name: "Product Model"
    product_id:
      name: "Product ID"
    hardware_model:
      name: "Hardware Model"
    hardware_version:
      name: "Hardware Version"
    keep_away:
      name: "Active Reporting Of Proximity"
    motion_status:
      name: "Motion Information"
    custom_mode_end:
      name: "Custom Mode Status"

binary_sensor:
  - platform: seeed_mr24hpc1
    has_target:
      name: "Presence Information"

sensor:
  - platform: seeed_mr24hpc1
    custom_presence_of_detection:
      name: "Static Distance"
    movement_signs:
      name: "Body Movement Parameter"
    custom_motion_distance:
      name: "Motion Distance"
    custom_spatial_static_value:
      name: "Existence Energy"
      id: eval
      on_value:
        if:
          condition:
            for:
              time: 240s
              condition:
                and:                  
                   - switch.is_on: indicator
                   - sensor.in_range:
                       id: eval
                       below: 175
          then:
            - switch.turn_off: indicator
            - uart.write:
                data: "Indicator off"
                id: indicator_bus
    custom_spatial_motion_value:
      name: "Motion Energy"
      id: mval
      on_value:
        - if:
            condition:
              and:
                 - switch.is_off: indicator
                 - sensor.in_range:
                     id: mval
                     above: 6
            then:
              - switch.turn_on: indicator
              - uart.write:
                  data: "Indicator on"
                  id: indicator_bus

    custom_motion_speed:
      name: "Motion Speed"
    custom_mode_num:
      name: "Current Custom Mode"

switch:
  - platform: seeed_mr24hpc1
    underlying_open_function:
      name: Underlying Open Function Info Output Switch
  - platform: template
    id: indicator
    name: "Occupancy Indicator"
    optimistic: true
    restore_mode: ALWAYS_OFF


button:
  - platform: seeed_mr24hpc1
    restart:
      name: "Module Restart"
    custom_set_end:
      name: "End Of Custom Mode Settings"

select:
  - platform: seeed_mr24hpc1
    scene_mode:
      name: "Scene"
    unman_time:
      name: "Time For Entering No Person State (Standard Function)"
    existence_boundary:
      name: "Existence Boundary"
    motion_boundary:
      name: "Motion Boundary"
  
number:
  - platform: seeed_mr24hpc1
    sensitivity:
      name: "Sensitivity"
    custom_mode:
      name: "Custom Mode"
    existence_threshold:
      name: "Existence Energy Threshold"
    motion_threshold:
      name: "Motion Energy Threshold"
    motion_trigger:
      name: "Motion Trigger Time"
    motion_to_rest:
      name: "Motion To Rest Time"
    custom_unman_time:
      name: "Time For Entering No Person State (Underlying Open Function)"

Use an analog threshold binary sensor (inverted) with delayed on filter, set the switch off in the on_press trigger and use the switch on_turn_off trigger to send the log message.

1 Like

Breaking out thresholds like this into separate (binary) sensors also make things way easier to debug plus you get a nice history of events.

You can always mark them as internal if you don’t want them sent to HA.

I much prefer that approach to the various on_value “range” type alternatives.

Thanks for the suggestions y’all. The analog threshold binary sensor is def. more convenient - got what I was after.

Still can’t wrap my head around why my previous code glitched. I initially thought it was UART bus related but guess not. Anywhos, glad it’s running now.

And I didn’t know we could hide sensors from HA. That’s neat. Thanks @Mahko_Mahko

Any entity without a name won’t be exposed to HA, and for internal use you don’t need a name, just an ID.

2 Likes