Skip to content

Commit

Permalink
Add editable template polygon example
Browse files Browse the repository at this point in the history
  • Loading branch information
TillFleisch committed Jun 10, 2024
1 parent 73f46a1 commit 9a8eaf0
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ on_...:
- **id**(**Required**, id): Id of the zone which should be updated
- **polygon**(**Required**, `return std::vector<ld2450::Point>;`): 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.
Expand Down
167 changes: 167 additions & 0 deletions examples/editable_template_polygon.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
esphome:
...
on_boot:

Check failure on line 3 in examples/editable_template_polygon.yaml

View workflow job for this annotation

GitHub Actions / Run yamllint

syntax error: mapping values are not allowed here
# 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++){
numbers[i]->publish_state(i % 2 == 0 ? (polygon[int(i/2)].x/1000.0f) : (polygon[int(i/2)].y/1000.0f));
}
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

0 comments on commit 9a8eaf0

Please sign in to comment.