ESPHome Dfrobot SEN0521 Config help

Hi there team, I am wanting to use these sensors with esphome and esp32…

I have been playing around with the other Dfrobot sensor SEN0395 which is supported by esphome and with a few hilink sensors, all have their pros, and cons.
the SEN0521 sensor is a 5ghz sensor and is included in the tuya zigbee presence sensors that I own, these sensors are not so good as zigbee sensors as if you have 10 or more the network can become unstabe due to to many mesages. I would really like to convert these sensors to esp32/wifi so they can join my other esp32 sensors and remove them from the zigbee.
the benifits I have found with the 5ghz sensor is its a little more stable and has very few false triggers whilst still picking up stationary objects very well. I would be very happy to be able to have these work in esphome.

So with all that said, I dont know how to go about converting the .cpp and .h files provided by dfrobot for Arduino:

into a working esphome yaml.
I tried for quite a bit of time to search google and work with chatgpt to acheive this but its above my head and chatgpt seemed to have the right idea but kept making incorrect and inconsistant yaml config.

I wondering if anyone here can help or point me in the right direction.

Thanks!

for anyone that wants this to I did manage to get this working, here is the esphome config.

1 Like
substitutions:
  name: computer-presence-sensor
  friendly_name: Computer Presence Sensor
  hidden_ssid: "false"
  factory_reset_disabled: "false"


esphome:
  name: ${name}
  friendly_name: ${friendly_name}

esp32:
  board: esp32dev
  
logger:
  level: VERBOSE 
  baud_rate: 0 

api:
  encryption:
    key: 

ota:
  password: 

wifi:
  ssid: 
  password: 
  manual_ip:
    static_ip: 192.168.40.94
    gateway: 192.168.40.1
    subnet: 255.255.255.0
captive_portal:

mqtt:
  broker: 192.168.1.5
  username: esphome
  password: esphome
  client_id: ${name}
  topic_prefix: esphome/${name}
  discovery: false

light:
  - platform: status_led
    name: ESP32 LED
    pin: GPIO14
    internal: False
    restore_mode: ALWAYS_OFF
  - platform: binary
    name: mmWave LED
    restore_mode: RESTORE_DEFAULT_OFF
    output: mmwave_led_output
    entity_category: config
    disabled_by_default: True

output:
  - platform: template
    id: mmwave_led_output
    type: binary
    write_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - if:
          condition:
            lambda: !lambda return state;
          then:
            - uart.write: "setLedMode 1 0"
          else:
            - uart.write: "setLedMode 1 1"
      - delay: 1s
      - uart.write: "saveConfig"
      - delay: 3s
      - switch.turn_on: mmwave_sensor

external_components:
  - source:
      type: git
      url: https://github.com/ssieb/custom_components
    components: [ serial ]

uart:
  id: uart_bus
  rx_pin: GPIO16  
  tx_pin: GPIO17 
  baud_rate: 115200
  debug:
    direction: BOTH
    dummy_receiver: true
    after:
      delimiter: "\n"
    sequence:
      - lambda: UARTDebug::log_string(direction, bytes);

binary_sensor:
  - platform: gpio
    name: mmWave
    id: mmwave
    device_class: occupancy
    pin:
      number: GPIO32
      mode: INPUT_PULLDOWN
  - platform: gpio
    pin:
      number: GPIO25
      mode: INPUT_PULLDOWN
    name: PIR
    id: pir_motion_sensor
    device_class: motion
    filters:
      - delayed_off:  !lambda 'return id(pir_off_latency).state * 1000.0;'
  - platform: template
    name: Occupancy
    id: occupancy
    device_class: occupancy
    filters:
      - delayed_off: !lambda 'return id(occupancy_off_latency).state * 1000.0;'
    lambda: |-
      if ( id(mmwave).state or id(pir_motion_sensor).state) {
        return true;
      } 
      else if (id(mmwave).state == 0 and id(pir_motion_sensor).state == 0) {
        return false;
      } 
      else {
        return id(occupancy).state;
      }

switch:
  - platform: template
    name: mmWave sensor
    id: mmwave_sensor
    disabled_by_default: True
    entity_category: config
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON
    turn_on_action:
      - uart.write: "sensorStart"
      - delay: 1s
    turn_off_action:
      - uart.write: "sensorStop"
      - delay: 1s

  - platform: template
    name: UART presence output
    id: uart_presence_output
    entity_category: config
    internal: false
    optimistic: true
    turn_on_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: "setUartOutput 1 1"
      - delay: 1s
      - uart.write: "saveConfig"
      - delay: 3s
      - switch.turn_on: mmwave_sensor
    turn_off_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: "setUartOutput 1 0"
      - delay: 1s
      - uart.write: "saveConfig"
      - delay: 3s
      - switch.turn_on: mmwave_sensor

  - platform: template
    name: UART target output
    id: uart_target_output
    entity_category: config
    internal: false
    optimistic: true
    assumed_state: false
    turn_on_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: "setUartOutput 2 1 1 1"
      - delay: 1s
      - uart.write: "saveConfig"
      - delay: 3s
      - switch.turn_on: mmwave_sensor
    turn_off_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: "setUartOutput 2 0"
      - delay: 1s
      - uart.write: "saveConfig"
      - delay: 3s
      - switch.turn_on: mmwave_sensor


number:
  - platform: template
    name: mmWave Start Distance
    icon: mdi:arrow-collapse-left
    entity_category: config
    id: mmwave_start_distance
    min_value: 0
    max_value: 1
    initial_value: 0
    optimistic: true
    step: .1
    restore_value: true
    unit_of_measurement: m
    mode: slider
    set_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: !lambda |-
          std::string mss = "setRange " + to_string(id(mmwave_start_distance).state) + " " + to_string(id(mmwave_stop_distance).state);
          return std::vector<unsigned char>(mss.begin(), mss.end());
      - delay: 3s
      - uart.write: "saveConfig"
      - delay: 2s
      - switch.turn_on: mmwave_sensor

  - platform: template
    name: mmWave Stop Distance
    icon: mdi:arrow-collapse-right
    entity_category: config
    id: mmwave_stop_distance
    min_value: 1
    max_value: 11
    initial_value: 4
    optimistic: true
    step: .5
    restore_value: true
    unit_of_measurement: m
    mode: slider
    set_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: !lambda |-
          std::string mss = "setRange " + to_string(id(mmwave_start_distance).state) + " " + to_string(id(mmwave_stop_distance).state);
          return std::vector<unsigned char>(mss.begin(), mss.end());
      - delay: 3s
      - uart.write: "saveConfig"
      - delay: 2s
      - switch.turn_on: mmwave_sensor

  - platform: template
    name: mmWave off latency
    icon: mdi:clock-end
    entity_category: config
    id: mmwave_off_latency
    min_value: 1
    max_value: 60
    initial_value: 15
    optimistic: true
    step: 1
    restore_value: true
    unit_of_measurement: seconds
    mode: slider
    set_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: !lambda |-
          std::string mss = "setLatency " + to_string(id(mmwave_on_latency).state) + " " + to_string(id(mmwave_off_latency).state);
          return std::vector<unsigned char>(mss.begin(), mss.end());
      - delay: 3s
      - uart.write: "saveConfig"
      - delay: 2s
      - switch.turn_on: mmwave_sensor

  - platform: template
    name: mmWave on latency
    icon: mdi:clock-start
    id: mmwave_on_latency
    entity_category: config
    min_value: 0
    max_value: 60
    initial_value: 0
    optimistic: true
    step: 0.5
    restore_value: true
    unit_of_measurement: seconds
    mode: slider
    set_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: !lambda |-
          std::string mss = "setLatency " + to_string(id(mmwave_on_latency).state) + " " + to_string(id(mmwave_off_latency).state);
          return std::vector<unsigned char>(mss.begin(), mss.end());
      - delay: 3s
      - uart.write: "saveConfig"
      - delay: 2s
      - switch.turn_on: mmwave_sensor

  - platform: template
    name: mmWave sensitivity
    icon: mdi:target-variant
    id: mmwave_sensitivity
    entity_category: config
    min_value: 0
    max_value: 9
    initial_value: 7
    optimistic: true
    step: 1
    restore_value: true
    set_action:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write:
          !lambda std::string mss = "setSensitivity " + to_string((int)x);
          return std::vector<unsigned char>(mss.begin(), mss.end());
      - delay: 1s
      - uart.write: "saveConfig"
      - delay: 1s
      - switch.turn_on: mmwave_sensor

  - platform: template
    name: Occupancy off latency
    icon: mdi:clock-end
    entity_category: config
    id: occupancy_off_latency
    min_value: 1
    max_value: 60
    initial_value: 15
    optimistic: true
    step: 1
    restore_value: true
    unit_of_measurement: seconds
    mode: slider

  - platform: template
    name: PIR off latency
    icon: mdi:clock-end
    entity_category: config
    id: pir_off_latency
    min_value: 1
    max_value: 60
    initial_value: 10
    optimistic: true
    step: 1
    restore_value: true
    unit_of_measurement: seconds
    mode: slider


button:
  - platform: restart
    id: restart_internal
    entity_category: config
    internal: false

  - platform: template
    name: Restart mmWave sensor
    id: restart_mmwave
    entity_category: config
    internal: true
    on_press:
      - uart.write: "resetSystem"

  - platform: template
    name: Restart
    icon: mdi:restart
    entity_category: config
    disabled_by_default: True
    on_press:
      - button.press: restart_mmwave
      - button.press: restart_internal

  - platform: safe_mode
    internal: false
    name: Safe mode
    entity_category: config
    disabled_by_default: false

  - platform: template
    name: Factory reset mmWave
    icon: mdi:cog-counterclockwise
    id: factory_reset_mmwave
    entity_category: config
    on_press:
      - switch.turn_off: mmwave_sensor
      - delay: 1s
      - uart.write: "resetCfg"
      - delay: 3s
      - switch.turn_on: mmwave_sensor

Nice one. Thanks for sharing it for the next person.

BTW you need to format your code a special way to make it easy for others to read. The trick is here.

1 Like