Correct way to handle countdown timers and restarts

Hi all,
I’m having an issue with a plug not turning off or staying on too long while using the countdown timer. I’m unsure if it’s an automation (user) issue or a container restart issue.

The plug runs a heater overnight when cold. From 12:00am until 7:00am, if the temperature falls below a threshold, the switch is turned on and the countdown timer set for 1 hour.

Additionally, when the plug is manually toggled, I have an automation that sets the countdown for an hour (this keeps the heater from running too long if someone switches it on and forgets or leaves).

The last part of this is that at 1:30am, the Home Assistant and Zigbee2MQTT containers are stopped so backups can be synced offsite, and then restarted. This can happen while the plug is on and timer is counting down.

I noticed the heater on way to long one night so set the “power outage memory” to off, to see if that would help, but it doesn’t. I guess that is irrelevant of HA being restarted.

Questions:

  • Is it bad to have the overnight automation set a countdown AND the manual switch set one?
  • Is there a correct way to deal with HA/MQTT resets in these situations?

Here are the relevant automations:

alias: Overnight bedroom heat
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.bedroom_thermastat_temperature
    for:
      hours: 0
      minutes: 10
      seconds: 0
    below: 12
    id: temp
  - trigger: time
    at: "00:00:00"
    id: time
conditions:
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: trigger
            id:
              - temp
          - condition: time
            after: "00:00:00"
            before: "04:30:00"
      - condition: and
        conditions:
          - condition: trigger
            id:
              - time
          - type: is_temperature
            condition: device
            device_id: 1a9a1a0c5e507cb2f9f26f8f61a2e489
            entity_id: 1a6d06ec66f8d36cb0e8c8fc563fc019
            domain: sensor
            below: 12
actions:
  - type: turn_on
    device_id: a91e70912520dc28809bbf1e81954a54
    entity_id: bdf2cd712a60c2f2001b11af3ad30d2f
    domain: switch
  - action: number.set_value
    metadata: {}
    target:
      entity_id: number.bedroom_plug_countdown
    data:
      value: "3600"
mode: single
alias: Bedroom plug manual timer
description: ""
triggers:
  - type: turned_on
    device_id: a91e70912520dc28809bbf1e81954a54
    entity_id: bdf2cd712a60c2f2001b11af3ad30d2f
    domain: switch
    trigger: device
conditions: []
actions:
  - device_id: a91e70912520dc28809bbf1e81954a54
    domain: number
    entity_id: afb0347c86ac3239a9f16c6dfebacea1
    type: set_value
    value: 3600
mode: single

And here’s the activity. (I have a similar automation that does the same as the Overnight, but just at 9pm) So at 9pm it was turned on and that automation set the countdown to 1hr. Then at 9:30 I restarted HA & MQTT. You can see the switch be marked as on again at 9:31 which triggers the “manual automation” timer.

Is my best option to remove the countdowns from the night time automations and just leave it on the “manual automation”? What else am I misunderstanding?

To have a timer survive through a restart of HA, a Timer Helper can be utilized.

Without digging in too much, a few general comments as I set up a space heater in a similar way.

First, for safety I use a power switch that has a parameter for an auto-off timer. Even if HA vanishes the heater will get turned off after that time.

Second, although my heater has one of those dial thermostats, I instead used the generic thermostat to control on/off cycles and to maintain temp. That integration has many parameters for tuning how it runs. And what I do is just change the setpoint temp at various times of day to control when it is on/off. I assume you could look at how it is turned on (e.g. by a manual button) and set a temporary setpoint and a timer.

I’m not using a timer for that heater. But as @bob.t pointed out, when you use a timer you need to consider that HA might be off when the timer finishes. I do that for irrigation and on HA restart I simply look if a valve is currently on and if there’s no timer running I turn the valve off (or move to next valve in a scheduled program).

If you don’t care that it is exactly on the amount of time on the timer you can also use a template using a delay_on, which will start counting again from when HA starts. B

@bob.t @busman Thanks both for that information. I don’t think my countdown itself is being lost by the restart of HA. As far as I can tell, that countdown is part of the device itself, so persists. However, when HA restarts, the plug state goes from an Unknown state to On (since the plug was on prior to the restart). This then triggers the automation to set the Countdown to 1hr.

There doesn’t seem to be a way to tell the difference between “the physical button was pressed” and “HA switched the plug on”. Does that sound right for this sort of device?

I think both a Timer helper or the Generic Thermostat could solve this problem though, but not relying on haphazardly applied coundowns - it just seemed like utilising the features of the device all in one place made sense and was tidier.

You can look at the context field on a trigger (look at one of your traces). You can deduce from that what happened. There are posts on the forum about this. It might be in the cookbook — I cannot remember exactly now.

I’m confused. How are you setting the “timer” if it’s not a timer? How is number.bedroom_plug_countdown being used as a timer? And is it the same thing as this?

  - device_id: a91e70912520dc28809bbf1e81954a54
    domain: number
    entity_id: afb0347c86ac3239a9f16c6dfebacea1
    type: set_value
    value: 3600

But the automation you posted has a midnight trigger, too, if below 12 degrees, right?

I still think things might be simpler using the generic thermostat. I never use device IDs, and I try to use simple automations.

Maybe I’m way off, and not tested (or even validated the YAML) but something like this?

There’s a few cases where the heat can be turned on:

alias: Overnight bedroom heat on when temp drops < 12 for 10 minutes
triggers:
  - trigger: numeric_state
    entity_id: sensor.bedroom_thermastat_temperature
    below: 12
    for: '00:10:00'
conditions:
  - condition: time
    after: "00:00:00"
    before: "04:30:00"
actions:
  - action: switch.turn_on
    target:
        entity_id: switch.bed_heater

And at midnight:

alias: Turn heat on at midnight if temp is < 12
triggers:
  - trigger: time
    at: "00:00:00"
conditions:
  - condition: numeric_state
    entity_id: sensor.bedroom_thermastat_temperature
    below: 12
actions:
  - action: switch.turn_on
    target:
      entity_id: switch.bed_heater

Whenever the heater turns on set the off timer:

alias: Set heater timer if timer isn't running.
triggers:
  - trigger: state
    entity_id: switch.bed_heater
    from: 'off'
    to: 'on'
conditions:
  - condition: timer.is_idle
    target:
      entity_id: timer.bed_heater
actions:
 - action: timer.start
   target:
     entity_id: timer.bed_heater
alias: Timer Finished Automation
trigger:
  - trigger: timer.finished
    target:
      entity_id: timer.bed_heater
action:
  - action: switch.turn_off
    target:
      entity_id: switch.bed_heater

I would add an automation for home assistant start turn off the heater if the heater is on and timer is idle as well as something to send out a warning if the heater is on longer the allowed time.

Unclear how that fits in with number.bedroom_plug_countdown.

Again, untested.