Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No issue] working setup for Home Assistant + ESPHome + esp32dev + CB28M1B-1 + HS13A-1 #30

Open
guevara777 opened this issue Mar 22, 2022 · 3 comments

Comments

@guevara777
Copy link

guevara777 commented Mar 22, 2022

Thank you to @iMicknl for helping me out with this! I´d like to share my working setup with others :)

Hardware

Disclaimer: I power my ESP32 with USB. I did not test to power it via V5-Pin. Please check the Documentation of your Board!

Wiring
image

ESPHome

Don`t forget to put this file -> https://github.com/iMicknl/LoctekMotion_IoT/blob/main/packages/esphome/desk_height_sensor.h
in the same folder as your .yaml-file for esphome!

substitutions:
  device_name: Schreibtisch # <- Friendlyname of your Device in Home Assistant
  name: esp-schreibtisch # <- name of your device in Home Assistant
  min_height: "75.1" # Min height + 0.1
  max_height: "109.1" # Max height - 0.1

esphome:
  name: ${name}
  comment: ${device_name}
  platform: esp32 # TODO Change to your platform
  board: esp32dev # TODO Change to your board
  includes:
    - desk_height_sensor.h

  project:
    name: "imicknl.loctekmotion_iot"
    version: "1.0.0"

  # Wake Desk by sending the "M" command
  # This will pull the current height after boot
  on_boot:
    priority: -10
    then:
      - button.press: button_m

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp-Schreibtisch"
    password: "<supersecretpassword>"

captive_portal:

# Enable logging
logger:
  level: DEBUG
  # level: VERBOSE # Makes uart stream available in esphome logstream
  baud_rate: 0 # Disable logging over uart


# Enable Home Assistant API
api:

ota:
  password: "<supersecretpassword>"

uart:
  id: desk_uart
  baud_rate: 9600
  tx_pin: GPIO17
  rx_pin: GPIO16

sensor:
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 60s

  - platform: uptime
    name: Uptime

  - platform: custom
    lambda: |-
      auto desk_height_sensor = new DeskHeightSensor(id(desk_uart));
      App.register_component(desk_height_sensor);
      return {desk_height_sensor};
    sensors:
      id: "desk_height"
      name: Desk Height
      unit_of_measurement: cm
      accuracy_decimals: 1
      icon: "mdi:counter"
      state_class: "measurement"
      on_value:
        then:
          - cover.template.publish:
              id: desk_cover
              position: !lambda |-
                  // The sensor outputs values from min_height (cm) to max_height (cm)
                  // We need to translate this to 0 - 1 scale.
                  float position = (float(x) - float(${min_height})) / (float(${max_height}) - float(${min_height}));
                  ESP_LOGD("main", "X is %f", x);
                  ESP_LOGD("main", "Current position is %f", position);
                  return position;
switch:
  # PIN20
  - platform: gpio
    name: "Virtual Screen"
    pin:
      number: GPIO23
      mode: OUTPUT
    restore_mode: ALWAYS_ON
    entity_category: "config" 
    internal: true

  - platform: uart
    name: "Up"
    id: switch_up2
    icon: mdi:arrow-up-bold
    data: [0x9b, 0x06, 0x02, 0x01, 0x00, 0xfc, 0xa0, 0x9d]
    uart_id: desk_uart
    send_every: 108ms

  - platform: uart
    name: "Down"
    id: switch_down2
    icon: mdi:arrow-down-bold
    data: [0x9b, 0x06, 0x02, 0x02, 0x00, 0x0c, 0xa0, 0x9d]
    uart_id: desk_uart
    send_every: 108ms

button:
  - platform: template
    name: "Preset 1"
    icon: mdi:numeric-1-box
    on_press:
      - uart.write:
          id: desk_uart
          data: [0x9b, 0x06, 0x02, 0x04, 0x00, 0xac, 0xa3, 0x9d] 

  - platform: template
    name: "Preset 2"
    icon: mdi:numeric-2-box
    on_press:
      - uart.write:
          id: desk_uart
          data: [0x9b, 0x06, 0x02, 0x08, 0x00, 0xac, 0xa6, 0x9d]

  - platform: template
    name: "Sit" # Preset 3 on some control panels
    icon: mdi:chair-rolling
    on_press:
      - uart.write:
          id: desk_uart
          data: [0x9b, 0x06, 0x02, 0x00, 0x01, 0xac, 0x60, 0x9d]

  - platform: template
    name: "Stand"
    icon: mdi:human-handsup
    on_press:
      - uart.write:
          id: desk_uart
          data: [0x9b, 0x06, 0x02, 0x10, 0x00, 0xac, 0xac, 0x9d]

  - platform: template
    name: "Memory"
    id: button_m
    icon: mdi:alpha-m-box
    entity_category: "config"
    on_press:
      - uart.write:
          id: desk_uart
          data: [0x9b, 0x06, 0x02, 0x20, 0x00, 0xac, 0xb8, 0x9d]

  - platform: template
    name: "Wake Screen"
    id: button_wake_screen
    icon: mdi:gesture-tap-button
    entity_category: "config"
    on_press:
      - uart.write:
          id: desk_uart
          data: [0x9b, 0x06, 0x02, 0x00, 0x00, 0x6c, 0xa1, 0x9d]

  - platform: template
    name: "Alarm"
    id: button_alarm
    icon: mdi:alarm
    on_press:
      - uart.write:
          id: desk_uart
          data: [0x9b, 0x06, 0x02, 0x40, 0x00, 0xAC, 0x90, 0x9d]

  - platform: restart
    name: "Restart"
    entity_category: "config"

cover:
  - platform: template
    id: "desk_cover" 
    icon: mdi:desk # or mdi:human-male-height-variant
    name: "Desk"
    device_class: blind # makes it easier to integrate with Google/Alexa
    has_position: true
    position_action:
      - logger.log: "Requesting action change"

    # Move desk up
    open_action:
      - while:
          condition:
            sensor.in_range:
              id: desk_height
              below: ${max_height}
          then:
            - logger.log: "Executing up command"
            - cover.template.publish:
                id: desk_cover
                current_operation: OPENING
            - uart.write:
                id: desk_uart
                data: [0x9b, 0x06, 0x02, 0x01, 0x00, 0xfc, 0xa0, 0x9d]
            - delay: 108ms
    
    # Move desk down
    close_action:
      - while:
          condition:
            sensor.in_range:
              id: desk_height
              above: ${min_height} 
          then:
            - logger.log: "Executing down command"
            - cover.template.publish:
                id: desk_cover
                current_operation: CLOSING
            - uart.write:
                id: desk_uart
                data: [0x9b, 0x06, 0x02, 0x02, 0x00, 0x0c, 0xa0, 0x9d]
            - delay: 108ms
    optimistic: true

Please add the following code at the botton of the yaml-file if you want to have a position control in Home Assistant. The Position-range is from 0% to 100% and you can use it with alexa like "set desk to 70%" if you populate the cover-entity to alexa (i did it with Nabu-Casa).

    # Move desk to desired position
    position_action:
    #Check if we need to move desk up or down from current position
      if:
        condition:
        #Current height is more than target height, then move desk down
          lambda: |-
            float position = (float(id(desk_height).state) - float(${min_height})) / (float(${max_height}) - float(${min_height}));
            return pos < position;
        then:
          - logger.log: "Schreibtisch muss runter gefahren werden"
          - while:
              condition:
                # Move down till the current height is smaller then the target height
                lambda: |-
                  float position = (float(id(desk_height).state) - float(${min_height})) / (float(${max_height}) - float(${min_height}));
                  return pos < position;
              then:
                - logger.log: "Executing down command"
                - cover.template.publish:
                    id: desk_cover
                    current_operation: CLOSING
                - uart.write:
                    id: desk_uart
                    data: [0x9b, 0x06, 0x02, 0x02, 0x00, 0x0c, 0xa0, 0x9d]
                - delay: 108ms
        else:
          - logger.log: "Schreibtisch muss hoch gefahren werden"
          - while:
              condition:
                # Move up till the current height is bigger then the target height
                lambda: |-
                  float position = (float(id(desk_height).state) - float(${min_height})) / (float(${max_height}) - float(${min_height}));
                  return pos > position;
              then:
                - logger.log: "Executing up command"
                - cover.template.publish:
                    id: desk_cover
                    current_operation: OPENING
                - uart.write:
                    id: desk_uart
                    data: [0x9b, 0x06, 0x02, 0x01, 0x00, 0xfc, 0xa0, 0x9d]
                - delay: 108ms

Picture of my setup
image

Voice-Control with Alexa
You can control your desk via Alexa now. To do so, expose the button.sit and button.stand to alexa (i use Naba Casa for this). In the Alexa App i createt 2 routines that trigger the "Sit" and "Stand"-Scenes with a Voice command like "Alexa, im stehen arbeiten" (Alexa, work standing).

@marylein
Copy link

marylein commented Aug 20, 2022

Hi @guevara777 , in the image you connect GPIO 18 and 19, but in the code you define the tx and rx for 16 and 17. Is that just a typo?
I'm using the exact same devices but I can't get this setup to work...

@guevara777
Copy link
Author

The Image is wrong. In the code it´s right. I use the GPIO-Pins 16 and 17 like this:

uart:
  id: desk_uart
  baud_rate: 9600
  tx_pin: GPIO17
  rx_pin: GPIO16

Here is the correct wirering:
image

I also added a position-action since i posted it here. You can populate the cover to Alexa and use voice commands like "set the desk to 70%".

for this you have to add this to the yaml-file:

    # Move desk to desired position
    position_action:
    #Check if we need to move desk up or down from current position
      if:
        condition:
        #Current height is more than target height, then move desk down
          lambda: |-
            float position = (float(id(desk_height).state) - float(${min_height})) / (float(${max_height}) - float(${min_height}));
            return pos < position;
        then:
          - logger.log: "Schreibtisch muss runter gefahren werden"
          - while:
              condition:
                # Move down till the current height is smaller then the target height
                lambda: |-
                  float position = (float(id(desk_height).state) - float(${min_height})) / (float(${max_height}) - float(${min_height}));
                  return pos < position;
              then:
                - logger.log: "Executing down command"
                - cover.template.publish:
                    id: desk_cover
                    current_operation: CLOSING
                - uart.write:
                    id: desk_uart
                    data: [0x9b, 0x06, 0x02, 0x02, 0x00, 0x0c, 0xa0, 0x9d]
                - delay: 108ms
        else:
          - logger.log: "Schreibtisch muss hoch gefahren werden"
          - while:
              condition:
                # Move up till the current height is bigger then the target height
                lambda: |-
                  float position = (float(id(desk_height).state) - float(${min_height})) / (float(${max_height}) - float(${min_height}));
                  return pos > position;
              then:
                - logger.log: "Executing up command"
                - cover.template.publish:
                    id: desk_cover
                    current_operation: OPENING
                - uart.write:
                    id: desk_uart
                    data: [0x9b, 0x06, 0x02, 0x01, 0x00, 0xfc, 0xa0, 0x9d]
                - delay: 108ms

@xXkr13g3rXx
Copy link

Hi guys. If I add the second part (Move desk to desired position) i get a double definition error from position_action.

I‘m a noob in coding and do need help there. Can anyone help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants