From fed54c5470d8f41aea8fdaf4e9c2a4f552dc592f Mon Sep 17 00:00:00 2001 From: Till Date: Mon, 10 Jun 2024 10:44:12 +0200 Subject: [PATCH] Add editable template polygon example --- .pre-commit-config.yaml | 5 + README.md | 5 + examples/editable_template_polygon.yaml | 170 ++++++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 examples/editable_template_polygon.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aa93c90..25c0e3f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,3 +15,8 @@ repos: rev: v17.0.6 hooks: - id: clang-format + + - repo: https://github.com/adrienverge/yamllint.git + rev: v1.29.0 + hooks: + - id: yamllint diff --git a/README.md b/README.md index a214515..c89b519 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,11 @@ on_...: - **id**(**Required**, id): Id of the zone which should be updated - **polygon**(**Required**, `return std::vector;`): List of Points which make up a convex polygon. The new polygon is only used if it's valid. +With the help of this action, a user editable dynamic polygon can be defined. +Note, that this allows for the definition of non-convex polygons. In the referenced example, the number components are only updated if the new polygon is valid. +When using the Home Assistant front end, number sliders may not reflect this change (or rather lack thereof) properly. +A partial example configuration for dynamic template polygons can be found [here](examples/editable_template_polygon.yaml). + ## Troubleshooting When using Dupont connectors make sure they make proper contact. The very short pins on the LD2450 Sensor can easily go loose or break. diff --git a/examples/editable_template_polygon.yaml b/examples/editable_template_polygon.yaml new file mode 100644 index 0000000..eb9d29d --- /dev/null +++ b/examples/editable_template_polygon.yaml @@ -0,0 +1,170 @@ +esphome: + # [...] + on_boot: + # Set polygon zone with values form number components + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,id(point_y1).state*1000), ld2450::Point(id(point_x2).state*1000,id(point_y2).state*1000), ld2450::Point(id(point_x3).state*1000,id(point_y3).state*1000), ld2450::Point(id(point_x4).state*1000,id(point_y4).state*1000)}; + +# [...] + +LD2450: + # [...] + zones: + - zone: + name: "Templated Zone" + id: template_zone + polygon: + lambda: !lambda |- + return {}; + update_interval: 0s + occupancy: + id: template_zone_occupancy + +script: + - id: update_number_components + then: + # Update number components from zone (if the polygon didn't change since the provided values were invalid) + - lambda: !lambda |- + auto polygon = id(template_zone).get_polygon(); + + if (polygon.size() != 4) + return; + + template_::TemplateNumber* numbers[8] = {id(point_x1), id(point_y1), id(point_x2), id(point_y2), id(point_x3), id(point_y3), id(point_x4), id(point_y4)}; + for(int i = 0; i < 8; i++){ + auto new_state = i % 2 == 0 ? (polygon[int(i/2)].x/1000.0f) : (polygon[int(i/2)].y/1000.0f); + if (numbers[i]->state != new_state){ + numbers[i]->publish_state(new_state); + } + } + +number: + - platform: template + id: point_x1 + name: "Point 1 X" + initial_value: -6 + min_value: -6 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(x*1000,id(point_y1).state*1000), ld2450::Point(id(point_x2).state*1000,id(point_y2).state*1000), ld2450::Point(id(point_x3).state*1000,id(point_y3).state*1000), ld2450::Point(id(point_x4).state*1000,id(point_y4).state*1000)}; + - script.execute: update_number_components + + - platform: template + id: point_y1 + name: "Point 1 Y" + initial_value: 0 + min_value: 0 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,x*1000), ld2450::Point(id(point_x2).state*1000,id(point_y2).state*1000), ld2450::Point(id(point_x3).state*1000,id(point_y3).state*1000), ld2450::Point(id(point_x4).state*1000,id(point_y4).state*1000)}; + - script.execute: update_number_components + + - platform: template + id: point_x2 + name: "Point 2 X" + initial_value: -6 + min_value: -6 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,id(point_y1).state*1000), ld2450::Point(x*1000,id(point_y2).state*1000), ld2450::Point(id(point_x3).state*1000,id(point_y3).state*1000), ld2450::Point(id(point_x4).state*1000,id(point_y4).state*1000)}; + - script.execute: update_number_components + + - platform: template + id: point_y2 + name: "Point 2 Y" + initial_value: 6 + min_value: 0 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,id(point_y1).state*1000), ld2450::Point(id(point_x2).state*1000,x*1000), ld2450::Point(id(point_x3).state*1000,id(point_y3).state*1000), ld2450::Point(id(point_x4).state*1000,id(point_y4).state*1000)}; + - script.execute: update_number_components + + - platform: template + id: point_x3 + name: "Point 3 X" + initial_value: 6 + min_value: -6 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,id(point_y1).state*1000), ld2450::Point(id(point_x2).state*1000,id(point_y2).state*1000), ld2450::Point(x*1000,id(point_y3).state*1000), ld2450::Point(id(point_x4).state*1000,id(point_y4).state*1000)}; + - script.execute: update_number_components + + - platform: template + id: point_y3 + name: "Point 3 Y" + initial_value: 6 + min_value: 0 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,id(point_y1).state*1000), ld2450::Point(id(point_x2).state*1000,id(point_y2).state*1000), ld2450::Point(id(point_x3).state*1000,x*1000), ld2450::Point(id(point_x4).state*1000,id(point_y4).state*1000)}; + - script.execute: update_number_components + + - platform: template + id: point_x4 + name: "Point 4 X" + initial_value: 6 + min_value: -6 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,id(point_y1).state*1000), ld2450::Point(id(point_x2).state*1000,id(point_y2).state*1000), ld2450::Point(id(point_x3).state*1000,id(point_y3).state*1000), ld2450::Point(x*1000,id(point_y4).state*1000)}; + - script.execute: update_number_components + + - platform: template + id: point_y4 + name: "Point 4 Y" + initial_value: 0 + min_value: 0 + max_value: 6 + step: 0.1 + restore_value: true + set_action: + # Update polygon zone with new value form number component + - LD2450.zone.update_polygon: + id: template_zone + polygon: !lambda |- + return {ld2450::Point(id(point_x1).state*1000,id(point_y1).state*1000), ld2450::Point(id(point_x2).state*1000,id(point_y2).state*1000), ld2450::Point(id(point_x3).state*1000,id(point_y3).state*1000), ld2450::Point(id(point_x4).state*1000,x*1000)}; + - script.execute: update_number_components