forked from leofabri/hassio_appliance-status-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappliance-state-monitor.yaml
1029 lines (939 loc) · 47.4 KB
/
appliance-state-monitor.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
blueprint:
name: Appliance State Monitor
description: >-
`- Version: 3.1.0 - millercentral`
ASM ('Appliance State Monitor') is an automation that can detect and monitor the state of your appliances through smart plugs.
The blueprint is intended to be universal and versatile. You can use it with anything that consumes energy.
You be able to pair this project with other automations and services.
For instance, if you want to send alerts when the washing machine is not resuming a job, you want to send TTS notifications, or if
your fridge is somehow not behaving correctly and de-icing you can see that happening. Do you want to turn your TV off when nobody is watching
it? No problem. All you need is just a little bit of creativity.
The [state machine](https://github.com/millercentral/hassio_appliance-status-monitor) is extensible, and you can run custom actions for each state change.
In the setup process I will refer to the appliance's operations with the generic word 'job'. A 'job' could be anything (washing, rinsing, etc.),
you can later specify what that word means in your case.
This blueprint is based on the excellent work by [leofabri](https://github.com/leofabri/hassio_appliance-status-monitor) and has been modified
by millercentral to support door sensors which will impact the appliance state (set paused or transition from complete -> idle). This version also
removes overload status tracking.
<strong>First setup?</strong> <i>[Follow the instructions](https://github.com/millercentral/hassio_appliance-status-monitor)</i>
domain: automation
input:
appliance_socket:
name: Appliance Smart Socket
description: >-
(*REQUIRED | switch)
The socket that is used to control this appliance.
default: []
selector:
entity:
domain: switch
appliance_power_sensor:
name: Appliance Power Consumption
description: >-
(*REQUIRED | sensor | [ Unit: Watt ])
The power entity reporting the current power usage in Watts.
default: []
selector:
entity:
domain: sensor
device_class: power
multiple: false
# SENSOR for appliance door
appliance_door_sensor:
name: Appliance Door Sensor
description: >-
(*REQUIRED | binary_sensor)
Sensor to indicate when appliance door is opened. Used to change state from job_running -> paused or
from job_completed -> idle when the appliance door opens.
default: []
selector:
entity:
domain: binary_sensor
appliance_starting_power_threshold:
name: Starting power threshold
description: >-
(*REQUIRED)
Power threshold above which we assume the appliance has started
a new job or is resuming the current one (job_ongoing state).
The **Starting Power threshold** must always be **>** than the **Finishing Power threshold**.
default: 5
selector:
number:
min: 1.0
max: 1000.0
unit_of_measurement: W
mode: slider
step: 1.0
# TIMER delayed_job_ongoing_timer
delayed_job_ongoing_timer:
name: Delayed Job Ongoing timer
description: >-
(*REQUIRED | Helper | Name: <i><strong><your_appliance_name>_delayed_job_ongoing_timer</i></strong> | [?](https://github.com/leofabri/hassio_appliance-status-monitor/blob/b71e28faca532a8a0192e43433e91ce958025d58/home%20assistant/packages/universal/asm_your_appliance_name.yaml#L21))
The timer that will allow to 'wait' & 'see' before assuming that a new job has started.
default: []
selector:
entity:
domain: timer
delayed_job_ongoing_duration:
name: Delayed Job Ongoing duration
description: >-
(OPTIONAL | <i><strong>Suggested: 0, Default: 0 | DISABLED</strong></i>)
During a job cycle, some appliances may intermittently use more power than the starting power threshold,
thus entering the job_ongoing state (even when the job is not really started).
With this value set, the automation will wait for the indicated time in seconds, and see if in that timespan the power consumption stays higher than the starting power threshold.
...
<i><strong>WARNING:</strong> Setting a duration introduces a delay on the transition to the 'job_ongoing' state.
Please make sure that you really need this, or leave it 0 if unsure.</i>
default: 0.0
selector:
number:
min: 0.0
max: 900.0
step: 1.0
unit_of_measurement: seconds
mode: slider
appliance_finishing_power_threshold:
name: Finishing power threshold
description: >-
(*REQUIRED)
Power threshold below which we assume the appliance has finished
a job (job_completed state).
The **Finishing Power threshold** must always be **<** than the **Starting Power threshold**.
default: 3.0
selector:
number:
min: 0.1
max: 1000.0
unit_of_measurement: W
mode: slider
step: 0.1
delayed_job_completion_duration:
name: Delayed Job Completion duration
description: >-
(OPTIONAL | <i><strong>Suggested: 0, Default: 0 | DISABLED</strong></i>)
During a job cycle, some appliances may intermittently use less power than the finishing power threshold,
thus entering the job_completed state (even when the job is not finished).
With this value set, the automation will wait for the indicated time in seconds, and monitor during that period the power consumption rises again.
...
<i><strong>WARNING:</strong> Setting a duration introduces a delay on the transition to the 'job_completed' state.
Please make sure that you really need this, or leave it 0 if unsure.</i>
default: 0.0
selector:
number:
min: 0.0
max: 900.0
step: 1.0
unit_of_measurement: seconds
mode: slider
appliance_job_completed_when_socket_is_off:
name: Consider the 'job_completed' when the socket is off
description: >-
(*REQUIRED)
Activate this if you want the state machine to transition to the 'job_completed' if the socket is is detected as unplugged.
Note: A perfect use case for this option is with TVs and other appliances that may be normally unpowered.
This is based on the assumption that: if the appliance is not powered, its job is done.
...
<i><strong>WARNING:</strong> There is [a bug in Home Assistant](https://community.home-assistant.io/t/blueprint-booleans-not-saving-after-deployment/440382/2) that causes the UI to ignore the update
the value of inputs like this. Sometimes, you'll have to go to Edit in YAML and define
'appliance_job_completed_when_socket_is_off: true/false' to solve the issue.
default: false
selector:
boolean:
appliance_state_machine:
name: Appliance State Machine
description: >-
(*REQUIRED | Helper | Name: <i><strong><your_appliance_name>_state_machine</strong></i> | [?](https://github.com/leofabri/hassio_appliance-status-monitor/blob/main/home%20assistant/packages/your_appliance_name.yaml#L18))
The State Machine entity of this appliance.
default: []
selector:
entity:
domain: input_select
appliance_job_cycle:
name: Appliance Job Cycle
description: >-
(*REQUIRED | Helper | Name: <i><strong><your_appliance_name>_job_cycle</strong></i> | [?](https://github.com/leofabri/hassio_appliance-status-monitor/blob/b71e28faca532a8a0192e43433e91ce958025d58/home%20assistant/packages/universal/asm_your_appliance_name.yaml#L33))
A variable that stores whether the appliance is still in a job cycle
or not.<br>
This is a boolean (so: 0 or 1).<br> <strong>off</strong> -> the appliance is
not doing any job<br> <strong>on</strong> -> the job is incomplete.
<br>
<strong>Note that this bare entity does not provide any information about the detailed status
of the machine (like an overload stuation).</strong>
<br>
default: []
selector:
entity:
domain: input_boolean
# TIMER delayed_job_completion_timer
delayed_job_completion_timer:
name: Job Completion | Delayed Job Completion timer
description: >-
(*REQUIRED | Helper | Name: <i><strong><your_appliance_name>_delayed_job_completion_timer</i></strong> | [?](https://github.com/leofabri/hassio_appliance-status-monitor/blob/b71e28faca532a8a0192e43433e91ce958025d58/home%20assistant/packages/universal/asm_your_appliance_name.yaml#L15))
The timer that will allow to 'wait' & 'see' before assuming that a job has been completed.
default: []
selector:
entity:
domain: timer
automation_self_trigger:
name: Automation Self-triggering entity
description: >-
(*REQUIRED | Helper | Name: <i><strong><your_appliance_name>_automation_self_trigger</i></strong> | [?](https://github.com/leofabri/hassio_appliance-status-monitor/blob/b71e28faca532a8a0192e43433e91ce958025d58/home%20assistant/packages/universal/asm_your_appliance_name.yaml#L38))
The in charge of triggering the execution of the automation when it changes from off -> on.
Sometimes, if the power consumption of the appliance is perfectly steady (or 0), no other trigger will work, and the automation is stuck. This entity solves the problem.
default: []
selector:
entity:
domain: input_boolean
#####################
# FLAG: ACTIONS #
#####################
actions_new_job_cycle_begins:
name: CUSTOM ACTION(S) | When a new job cycle begins
description: >-
Executed when the appliance starts a new job cycle (<strong>idle -> job_ongoing</strong>
state).
...
<i>**WARNING:** Just use non-blocking actions in this space! No delays, actionable notifications, TTS, waits, or anything that takes time to execute.
Please consider that the permanence in this state could last for a limited amount of time (seconds, potentially!).
This section is meant to be used to trigger other things like scripts.</i>
If you need to use it for time-consuming operations, you have two options: dispatch the work by calling other scripts, or use the State Machine entity to wake up other external automations.
default: []
selector:
action:
actions_job_cycle_resumes:
name: CUSTOM ACTION(S) | When a job cycle resumes
description: >-
Executed when a pending job cycle is resumed (<strong>paused | unplugged -> job_ongoing</strong>
state).
Note that in this situation, the job cycle indicator is still on. That's how I know that the appliance is resuming and not startig a job.
...
<i>**WARNING:** Just use non-blocking actions in this space! No delays, actionable notifications, TTS, waits, or anything that takes time to execute.
Please consider that the permanence in this state could last for a limited amount of time (seconds, potentially!).
This section is meant to be used to trigger other things like scripts.</i>
If you need to use it for time-consuming operations, you have two options: dispatch the work by calling other scripts, or use the State Machine entity to wake up other external automations.
default: []
selector:
action:
actions_job_cycle_ends:
name: CUSTOM ACTION(S) | When a job cycle is finished
description: >-
Executed when the appliance finishes a job cycle (<strong>job_ongoing -> job_completed</strong>
state).
...
<i>**WARNING:** Just use non-blocking actions in this space! No delays, actionable notifications, TTS, waits, or anything that takes time to execute.
Please consider that the permanence in this state could last for a limited amount of time (seconds, potentially!).
This section is meant to be used to trigger other things like scripts.</i>
If you need to use it for time-consuming operations, you have two options: dispatch the work by calling other scripts, or use the State Machine entity to wake up other external automations.
default: []
selector:
action:
actions_paused_after_unplugged:
name: CUSTOM ACTION(S) | When the appliance is plugged back in, now paused
description: >-
Executed when the state changes from <strong>unplugged -> paused</strong> (NOT resuming the job).
...
<i>**WARNING:** Just use non-blocking actions in this space! No delays, actionable notifications, TTS, waits, or anything that takes time to execute.
Please consider that the permanence in this state could last for a limited amount of time (seconds, potentially!).
This section is meant to be used to trigger other things like scripts.</i>
If you need to use it for time-consuming operations, you have two options: dispatch the work by calling other scripts, or use the State Machine entity to wake up other external automations.
default: []
selector:
action:
actions_paused_after_door_opened:
name: CUSTOM ACTION(S) | When the door opens while job is running, now paused
description: >-
Executed when the state changes from <strong>job_ongoing -> paused</strong> (NOT resuming the job).
...
<i>**WARNING:** Just use non-blocking actions in this space! No delays, actionable notifications, TTS, waits, or anything that takes time to execute.
Please consider that the permanence in this state could last for a limited amount of time (seconds, potentially!).
This section is meant to be used to trigger other things like scripts.</i>
If you need to use it for time-consuming operations, you have two options: dispatch the work by calling other scripts, or use the State Machine entity to wake up other external automations.
default: []
selector:
action:
source_url: https://github.com/millercentral/hassio_appliance-status-monitor/blob/main/appliance-status-monitor.yaml
# FLAG: Variables
variables:
# Socket info
appliance_socket: !input "appliance_socket"
# Door info
appliance_door_sensor: !input "appliance_door_sensor"
# Delays
delayed_job_ongoing_duration: !input "delayed_job_ongoing_duration" # precedes job_start
delayed_job_completion_duration: !input "delayed_job_completion_duration" # precedes job_completed
# Timer entities
delayed_job_ongoing_timer: !input "delayed_job_ongoing_timer" # precedes job_start
delayed_job_completion_timer: !input "delayed_job_completion_timer" # precedes job_completed
# Blueprint settings
appliance_job_completed_when_socket_is_off: !input "appliance_job_completed_when_socket_is_off"
LOG_WAIT: "WAIT"
LOG_STATE_CHANGE: "STATE CHANGE"
LOG_TIMER: "TIMER"
LOG_CYCLE_START: "CYCLE START"
LOG_CYCLE_END: "CYCLE END"
LOG_CUSTOM_ACTIONS: "CUSTOM ACTIONS"
LOG_POWER_THRESHOLD: "POWER_THRESHOLD"
LOG_LEVEL: debug
LOG_LOGGER: homeassistant.components.asm_blueprint
# FLAG: Triggers
trigger:
- platform: state
entity_id: !input "appliance_power_sensor"
id: power_event
- platform: state
entity_id: !input "appliance_socket"
id: socket_state_change_event
- platform: state
entity_id: !input "appliance_state_machine"
from: job_ongoing
to: paused
id: paused_after_door_event
- platform: state
entity_id: !input "appliance_state_machine"
from: unplugged
to: paused
id: paused_after_unplugged_event
- platform: state
entity_id: !input "automation_self_trigger"
from: "off"
to: "on"
id: automation_self_triggered
- platform: state
entity_id: !input "appliance_door_sensor"
from: "off"
to: "on"
id: appliance_door_opened_event
# EVENTS from timers
- platform: event
event_type: timer.finished
event_data:
entity_id: !input "delayed_job_ongoing_timer"
id: job_started_timer_finished
- platform: event
event_type: timer.finished
event_data:
entity_id: !input "delayed_job_completion_timer"
id: job_completed_timer_finished
# ----
# EVENTS from Home Assistant
- platform: homeassistant
event: start
id: home_assistant_started_event
- platform: event
event_type:
- automation_reloaded
id: automation_reloaded_event
# ----
condition:
# Accepted triggers:
- condition: or
conditions:
- condition: trigger
id: power_event
- condition: trigger
id: socket_state_change_event
- condition: trigger
id: appliance_door_opened
- condition: trigger
id: paused_after_door_event
- condition: trigger
id: paused_after_unplugged_event
- condition: trigger
id: automation_self_triggered
- condition: trigger
id: job_started_timer_finished
- condition: trigger
id: job_completed_timer_finished
- condition: trigger
id: home_assistant_started_event
- condition: trigger
id: automation_reloaded_event
# FLAG: Logic
action:
- service: input_boolean.turn_off
data: {}
target:
entity_id: !input "automation_self_trigger"
##################################################
# NOTE: "paused" state handling logic #
##################################################
- choose:
- conditions:
- condition: state
entity_id: !input "appliance_job_cycle"
state: "on"
- condition: state
entity_id: !input "appliance_door_sensor"
state: "on"
- condition: template
value_template: "{{ states(appliance_socket) == 'on' }}"
- condition: numeric_state
entity_id: !input "appliance_power_sensor"
below: !input "appliance_finishing_power_threshold"
- condition: or
conditions:
- condition: state
entity_id: !input "appliance_state_machine"
state: job_ongoing
- condition: state
entity_id: !input "appliance_state_machine"
state: unplugged
- condition: not
conditions:
- condition: state
entity_id: !input "appliance_state_machine"
state: paused
sequence:
- service: input_select.select_option
data:
option: paused # FLAG: paused STATE TRANSITION
target:
entity_id: !input "appliance_state_machine"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_STATE_CHANGE }} - Entered the paused state."
# ***********************
##################################################
# NOTE: "unplugged" state handling logic #
##################################################
- conditions:
- condition: template
value_template: "{{ states(appliance_socket) == 'off' }}"
# Check whether the socket option is enabled or disabled
- condition: not
conditions:
- condition: template
value_template: >
{{ appliance_job_completed_when_socket_is_off }}
sequence:
- condition: not
conditions:
- condition: state
entity_id: !input "appliance_state_machine"
state: unplugged
- service: input_select.select_option
data:
option: unplugged # FLAG: unplugged STATE TRANSITION
target:
entity_id: !input "appliance_state_machine"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_STATE_CHANGE }} - Entered the unplugged state."
# ***********************
- choose:
- conditions:
- condition: template
value_template: "{{ states(delayed_job_completion_timer) == 'active' }}"
sequence:
- service: timer.cancel
data: {}
target:
entity_id: !input "delayed_job_completion_timer"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_TIMER }} - delayed_job_completion_timer canceled and stopped."
# ***********************
- conditions:
- condition: trigger
id: paused_after_door_event
sequence:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_CUSTOM_ACTIONS }} - executing actions_paused_after_door_opened."
# ***********************
- choose: []
default: !input "actions_paused_after_door_opened"
- conditions:
- condition: trigger
id: paused_after_unplugged_event
sequence:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_CUSTOM_ACTIONS }} - executing actions_paused_after_unplugged."
# ***********************
- choose: []
default: !input "actions_paused_after_unplugged"
default:
##################################################
# NOTE: "job_ongoing" state handling logic #
##################################################
- choose:
# IF (and)
- conditions:
# the socket is on
- condition: template
value_template: "{{ states(appliance_socket) == 'on' }}"
# the power consumption > appliance_starting_power_threshold
- condition: numeric_state
entity_id: !input "appliance_power_sensor"
above: !input "appliance_starting_power_threshold"
sequence:
# Delete the delayed_job_completion_timer if it's running
# If the job completed timer is running, then the power consumption was high enough.
# The delayed_job_completion_timer timer has to be discarded
- choose:
- conditions:
- condition: template
value_template: "{{ states(delayed_job_completion_timer) == 'active' }}"
sequence:
- service: timer.cancel
data: {}
target:
entity_id: !input "delayed_job_completion_timer"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_TIMER }} - The starting power threshold was surpassed again => delayed_job_completion_timer canceled and stopped"
# ***********************
- choose:
# IF
- conditions:
# Start the timer only if the state is not already job_ongoing
- condition: not
conditions:
- condition: state
entity_id: !input "appliance_state_machine"
state: job_ongoing
# the delayed_job_ongoing_timer isn't active
- condition: template
value_template: "{{ states(delayed_job_ongoing_timer) != 'active' }}"
# the job_completed_timer wasn't the one that triggered this automation
- condition: not
conditions:
- condition: trigger
id: job_started_timer_finished
sequence:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_POWER_THRESHOLD }} - The starting power threshold was reached"
# ***********************
# Start the timer, start counting backwards
- service: timer.start
data: {}
target:
entity_id: !input "delayed_job_ongoing_timer"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_TIMER }} - delayed_job_ongoing_timer start invoked. Now it's {{ states(delayed_job_ongoing_timer) }}"
# ***********************
- choose:
# if the timer should last for a time that is not 0
- conditions:
- condition: template
value_template: "{{ delayed_job_ongoing_duration > 0 }}"
sequence:
- choose:
# if the elapsed time is less than the wanted delayed_job_ongoing_duration
- conditions:
- condition: template
value_template: |-
{% if states(delayed_job_ongoing_timer) == 'active' %}
{% set t_expiring_date = state_attr(delayed_job_ongoing_timer, 'finishes_at') %}
{% set t_remaining_sec = 0 if t_expiring_date == None else (as_datetime(t_expiring_date) - now()).total_seconds() | int %}
{% set t_total_duration = state_attr(delayed_job_ongoing_timer, 'duration') %}
{% set duration_split = t_total_duration.split(':') %}
{% set t_total_duration_sec = (duration_split[0] | int * 3600) + (duration_split[1] | int * 60) + (duration_split[0] | int) %}
{% set t_elapsed_sec = (t_total_duration_sec - t_remaining_sec) | int %}
{{ t_elapsed_sec < (delayed_job_ongoing_duration) | int }}
{% else %}
{{0}}
{% endif %}
sequence:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_WAIT }} - Waiting {{ delayed_job_ongoing_duration }} seconds, then I will decide if job_ongoing state should be entered."
# ***********************
# try to wait here until the remaining time is elapsed
- delay:
seconds: |-
{% if states(delayed_job_ongoing_timer) == 'active' %}
{% set t_expiring_date = state_attr(delayed_job_ongoing_timer, 'finishes_at') %}
{% set t_remaining_sec = 0 if t_expiring_date == None else (as_datetime(t_expiring_date) - now()).total_seconds() | int %}
{% set t_total_duration = state_attr(delayed_job_ongoing_timer, 'duration') %}
{% set duration_split = t_total_duration.split(':') %}
{% set t_total_duration_sec = (duration_split[0] | int * 3600) + (duration_split[1] | int * 60) + (duration_split[0] | int) %}
{% set t_elapsed_sec = (t_total_duration_sec - t_remaining_sec) | int %}
{% set t_remaining = ((delayed_job_ongoing_duration) | int) - t_elapsed_sec %}
{{ 1 + t_remaining }}
{% else %}
{{ 1 + (delayed_job_ongoing_duration) | int }}
{% endif %}
# if the delay is elapsed, self trig the atomation
- service: input_boolean.turn_on
data: {}
target:
entity_id: !input "automation_self_trigger"
# make sure that the execution doesn't continue. So that the job_completed state is not reached.
- stop: ""
default: [] # Do nothing. End
- condition: not
conditions:
- condition: state
entity_id: !input "appliance_state_machine"
state: job_ongoing
- service: input_select.select_option
data:
option: job_ongoing # FLAG: job_ongoing STATE TRANSITION
target:
entity_id: !input "appliance_state_machine"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_STATE_CHANGE }} - Entered the job_ongoing state."
# ***********************
- choose:
- conditions:
- condition: template
value_template: "{{ states(delayed_job_ongoing_timer) == 'active' }}"
sequence:
- service: timer.cancel
data: {}
target:
entity_id: !input "delayed_job_ongoing_timer"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_TIMER }} - delayed_job_ongoing_timer canceled and stopped."
# ***********************
- choose:
- conditions:
- condition: state
entity_id: !input "appliance_job_cycle"
state: "off"
sequence:
- service: input_boolean.turn_on
data: {}
target:
entity_id: !input "appliance_job_cycle"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_CYCLE_START }} - A new Job Cycle started"
# ***********************
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_CUSTOM_ACTIONS }} - executing actions_new_job_cycle_begins."
# ***********************
- choose: []
default: !input "actions_new_job_cycle_begins"
default:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_CUSTOM_ACTIONS }} - executing actions_job_cycle_resumes."
# ***********************
- choose: []
default: !input "actions_job_cycle_resumes"
##################################################
# NOTE: "job_completed" state handling logic #
##################################################
# IF
- conditions:
# The job is currently ongoing
- condition: state
entity_id: !input "appliance_state_machine"
state: job_ongoing
# The job cycle is still in progress
- condition: state
entity_id: !input "appliance_job_cycle"
state: "on"
# A value below the finishing power threshold is received
- condition: numeric_state
entity_id: !input "appliance_power_sensor"
below: !input "appliance_finishing_power_threshold"
# If the socket is off and the
- condition: or
conditions:
# The socket is on
- condition: template
value_template: "{{ states(appliance_socket) == 'on' }}"
- condition: and
conditions:
# The option that forces the state machine to enter idle is on
- condition: template
value_template: "{{ appliance_job_completed_when_socket_is_off }}"
# The socket is off
- condition: template
value_template: "{{ states(appliance_socket) == 'off' }}"
sequence:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_POWER_THRESHOLD }} - The finishing power threshold was reached"
# ***********************
- choose:
# IF
- conditions:
- condition: or
conditions:
- condition: not
conditions:
- condition: template
value_template: >
{{ appliance_job_completed_when_socket_is_off }}
- condition: not
conditions:
- condition: and
conditions:
- condition: template
value_template: >
{{ appliance_job_completed_when_socket_is_off }}
# The socket is on
- condition: template
value_template: "{{ states(appliance_socket) == 'off' }}"
# If the state machine isn't already in the job_completed state
- condition: not
conditions:
- condition: state
entity_id: !input "appliance_state_machine"
state: job_completed
# the timer isn't active
- condition: template
value_template: "{{ states(delayed_job_completion_timer) != 'active' }}"
# the job_completed_timer didn't trigger this automation this time
- condition: not
conditions:
- condition: trigger
id: job_completed_timer_finished
sequence:
# Start the timer, start counting backwards
- service: timer.start
data: {}
target:
entity_id: !input "delayed_job_completion_timer"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_TIMER }} - delayed_job_completion_timer start invoked. Now it's {{ states(delayed_job_completion_timer) }}"
# ***********************
- choose:
# if the timer should last for a time that is not 0
- conditions:
- condition: template
value_template: "{{ states(delayed_job_completion_timer) == 'active' }}"
- condition: template
value_template: "{{ delayed_job_completion_duration > 0 }}"
sequence:
- choose:
# if the elapsed time is less than the wanted delayed_job_completion_duration
- conditions:
# - condition: not
# conditions:
# - condition: template
# value_template: >
# {{ appliance_job_completed_when_socket_is_off }}
- condition: template
value_template: |-
{% if states(delayed_job_completion_timer) == 'active' %}
{% set t_expiring_date = state_attr(delayed_job_completion_timer, 'finishes_at') %}
{% set t_remaining_sec = 0 if t_expiring_date == None else (as_datetime(t_expiring_date) - now()).total_seconds() | int %}
{% set t_total_duration = state_attr(delayed_job_completion_timer, 'duration') %}
{% set duration_split = t_total_duration.split(':') %}
{% set t_total_duration_sec = (duration_split[0] | int * 3600) + (duration_split[1] | int * 60) + (duration_split[0] | int) %}
{% set t_elapsed_sec = (t_total_duration_sec - t_remaining_sec) | int %}
{{ t_elapsed_sec < (delayed_job_completion_duration) | int }}
{% else %}
{{0}}
{% endif %}
sequence:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_WAIT }} - Waiting {{ delayed_job_completion_duration }} seconds, then I will decide if job_completed state should be entered."
# ***********************
- delay:
seconds: |-
{% if states(delayed_job_completion_timer) == 'active' %}
{% set t_expiring_date = state_attr(delayed_job_completion_timer, 'finishes_at') %}
{% set t_remaining_sec = 0 if t_expiring_date == None else (as_datetime(t_expiring_date) - now()).total_seconds() | int %}
{% set t_total_duration = state_attr(delayed_job_completion_timer, 'duration') %}
{% set duration_split = t_total_duration.split(':') %}
{% set t_total_duration_sec = (duration_split[0] | int * 3600) + (duration_split[1] | int * 60) + (duration_split[0] | int) %}
{% set t_elapsed_sec = (t_total_duration_sec - t_remaining_sec) | int %}
{% set t_remaining = ((delayed_job_completion_duration) | int) - t_elapsed_sec %}
{{ 1 + t_remaining }}
{% else %}
{{ 1 + (delayed_job_completion_duration) | int }}
{% endif %}
# if the delay is elapsed, self trig the atomation
- service: input_boolean.turn_on
data: {}
target:
entity_id: !input "automation_self_trigger"
# make sure that the execution doesn't continue. So that the job_completed state is not reached.
- stop: ""
default: [] # Do nothing. End
- service: input_boolean.turn_off
data: {}
target:
entity_id: !input "appliance_job_cycle"
- service: input_select.select_option
data:
option: job_completed # FLAG: job_completed STATE TRANSITION
target:
entity_id: !input "appliance_state_machine"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_CYCLE_END }} - The Job Cycle is over"
# ***********************
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_STATE_CHANGE }} - Entered the job_completed state."
# ***********************
- choose:
- conditions:
- condition: template
value_template: "{{ states(delayed_job_completion_timer) == 'active' }}"
sequence:
- service: timer.cancel
data: {}
target:
entity_id: !input "delayed_job_completion_timer"
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_TIMER }} - delayed_job_completion_timer canceled and stopped."
# ***********************
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_CUSTOM_ACTIONS }} - executing actions_job_cycle_ends."
# ***********************
- choose: []
default: !input "actions_job_cycle_ends"
- choose:
- conditions:
- condition: or
conditions:
- condition: trigger
id: automation_self_triggered
- condition: template
value_template: "{{ delayed_job_completion_duration <= 0 }}"
- condition: template
value_template: >
{{ appliance_job_completed_when_socket_is_off }}
sequence:
# - delay:
# minutes: 1
# Self trigger once more to move to the idle state
- service: input_boolean.turn_on
data: {}
target:
entity_id: !input "automation_self_trigger"
default:
##################################################
# NOTE: "idle" state handling logic #
##################################################
- choose:
- conditions:
- condition: state
entity_id: !input "appliance_job_cycle"
state: "off"
- condition: not
conditions:
- condition: state
entity_id: !input "appliance_state_machine"
state: idle
sequence:
- choose:
# IF
- conditions:
# the washer door is closed so dont progress to idle
- condition: state
entity_id: !input "appliance_door_sensor"
state: "off"
sequence:
# ***** LOG SECTION *****
- service: system_log.write
data_template:
level: "{{ LOG_LEVEL }}"
logger: "{{ LOG_LOGGER }}"
message: "{{ LOG_WAIT }} - Door not open, so remaining in job_completed state."
# ***********************
- stop: ""
default: [] # Do nothing. End
# Go to idle
- service: input_select.select_option
data:
option: idle # FLAG: idle STATE TRANSITION