So I’ve got an automation that turns off my hotwater smart plug (Local Tuya device) when there has been no power draw for the past 30 minutes. The reason I have this condition is because for reason unknown to me, sometimes the hotwater unit itself momentarily turns off for a few minutes and starts back up again.
Anyways, my below automation script seems to work fine 90% of the time however there are occasions when the device seems to lose connection and because of this loss of connection, I believe the 30 minute timer condition fails and so the entire automation fails for the day.
alias: Hotwater Off
description: >-
Turns off the hotwater if there has been no power draw for the last 30 minutes.
trigger:
- platform: numeric_state
entity_id:
- sensor.hotwater_smart_switch_current_consumption
below: 10
for:
hours: 0
minutes: 30
seconds: 0
condition:
- condition: state
entity_id: switch.hotwater_smart_plug
state: "on"
for:
hours: 0
minutes: 0
seconds: 0
action:
- type: turn_off
device_id: blanked_out
entity_id: switch.hotwater_smart_plug
domain: switch
mode: single
So based on the history, it looks like the unit finished running at 12pm. But it lost connection 8 minutes later at 12:08pm. Because of this, I think the timer for the trigger resets itself and because of that, it doesn’t turn off at 12:38pm.
Is there anyway I can fix this automation to account for connection losses in the future?
If the device loses connection, the value of the sensor representing it probably changes to unavailable. So if its value was below 10 for a few minutes and then changed to unavailable, that’s sufficient to cancel the Numeric State Trigger’s 30-minute countdown.
You appear to have addressed that potential source of resetting the for option but there’s more. The 30-minute countdown can also be cancelled whenever Home Assistant restarts and whenever you execute Reload Automations.
If you use the Automation Editor to create/modify automations, the moment you click the Save button it automatically executes Reload Automations in the background (thereby terminating any trigger employing for and any automation that’s in-progress).
Keep in mind that the for option is more than just a timer. You are using it together with the below option and the combination makes for act as a timer only when the sensor entity’s value is below 10. It resets its countdown if the value rises above 10.
A standard timer simply counts time and doesn’t take into consideration any other entity’s value. To make it behave like the for option, you would need to create an automation to control the standard timer’s operation. The advantage a standard timer has over the for option is that it can survive a restart (see restore option).