Rocka84 / esphome_components

10 stars 4 forks source link

Feature Request: Add Motion Detection Sensor #2

Open OkhammahkO opened 5 months ago

OkhammahkO commented 5 months ago

Hello again;)

The Desky custom component on the RJ45 port tracks the state of the desk in a similar way to some cover components.
https://github.com/ssieb/esphome_components/tree/master/components/desky

binary_sensor:
  - platform: template
    name: Desky moving
    lambda: return id(my_desky).current_operation != desky::DESKY_OPERATION_IDLE;

It would be nice to have a similar feature for your component. I think this also sets up for a “cover” type set-up which I’ll have a play with and raise a separate feature request for soonish.

I came up with this yaml based approach. It works by watching for changes in height (which seems simple to do via on_value since height seems to only update on changes) . Since we aren't measuring something directly I guess this relies on the contoller being honest about what it is sending, and an assumption about a "motion timeout".

I think this kind of sensor then sets up a for a nicer "cover" implementation.

I don't know whether my approach is any good, I guess doing it at a lower level (maybe just by tracking byte 5?) might be better. Also I haven't thought through whether there may be some safety related aspect to this if the motion is "assumed" to some extent.

Let me know your thoughts.

  numbers:
    height:
      name: "Height"
      id: height_id
      on_value: # When height changes, publish the desk state as "moving" with a small timeout for "not moving"
        then:
          - binary_sensor.template.publish:
              id: desk_is_moving
              state: True
          - delay: 100ms
          - binary_sensor.template.publish:
              id: desk_is_moving
              state: False

            # Calculate the change in height and publish it to a new sensor
            # You could use something like this to track the direction of movement of the desk?
            # - lambda: |-
                # static float last_value = 0;
                # float change = 0;
                # change = x - last_value;
                # last_value = x;
              # return id(height_delta).publish_state(change);

# sensor:
  # - platform: template
    # id: height_delta
    # name: "height delta"
    # internal: false

binary_sensor:
  - platform: template
    id: desk_is_moving
    name: "Desk is Moving"
    filters:
      - delayed_off: 300ms # Probably this should relate to the height message update frequency. Seems to work ok for me.