-
Notifications
You must be signed in to change notification settings - Fork 14
/
weight.yaml
67 lines (66 loc) · 2.1 KB
/
weight.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
- alias: MQTT Discovery and Automation
id: mqtt_store_and_automate_weights
mode: parallel
trigger:
- id: start
platform: homeassistant
event: start
- id: new
platform: state
entity_id: sensor.master_scale_weight
variables:
root: "homeassistant"
range: 20
config:
- person: person.petro
initial: 190
- person: person.lambo
initial: 145
people: >
{%- set ns = namespace(items=[]) %}
{%- for item in config %}
{%- set object_id = item.person.split('.')[-1] ~ '_weight' %}
{%- set ns.items = ns.items + [{
'unique_id': object_id,
'object_id': object_id,
'initial': item.initial,
'current': states('sensor.' ~ object_id) | float(None),
}] %}
{%- endfor %}
{{ ns.items }}
weight: >
{%- if trigger.id == 'new' and trigger.to_state.state | is_number %}
{%- set next_weight = trigger.to_state.state | float %}
{%- set ns = namespace(items=[]) %}
{%- for person in people %}
{%- set current = person.current or person.initial %}
{%- if current - range <= next_weight <= current + range %}
{%- set ns.items = ns.items + [{'next':next_weight, 'unique_id':person.unique_id}] %}
{%- endif %}
{%- endfor %}
{{ ns.items | first | default }}
{%- else -%}
None
{%- endif %}
action:
- choose:
- conditions: "{{ trigger.id == 'start' }}"
sequence:
- repeat:
for_each: "{{ people }}"
sequence:
- service: script.mqtt_automated_config
data:
domain: sensor
unique_id: "{{ repeat.item.unique_id }}"
object_id: "{{ repeat.item.object_id }}"
state_class: measurement
device_class: weight
unit_of_measurement: lb
- conditions: "{{ weight is not none }}"
sequence:
- service: script.mqtt_automated_states
data:
domain: sensor
unique_id: "{{ weight.unique_id }}"
state: "{{ weight.next }}"