Turn off switch after n time (and then check)

I have a switch (unreliable) that must be switched off after 55mins of being on. This controls a central heating 1hr boost switch.

The automation I have is proving unreliable at turning off the switch (switchbot), as the switch is unreliable, so sometimes the automations fires but doesn’t trigger the switch.

Is there a way for the automation to check if the switch has been on for 55 mins, then turn it off. and then, if the switch hasn’t managed to turn of, to attempt to turn it off again ?

Any help would be appreciated!

My current automation:

- id: 'heating_dumb_off'
  alias: 'Heating Dumb Off'
  initial_state: true
  trigger:
    - platform: state
      entity_id: switch.boiler_bot
      to: 'on'
      for:
        minutes: 55
  action:   
    - service: homeassistant.turn_off
      entity_id: switch.boiler_bot

You can track how long it’s been on inside 55 minutes using

Then just make a template sensor or something if it’s close to 100% on time.

Thanks for that info @petro.

I’ve been having a tinker with this, but it’s taking me a long time as I have to reboot everytime I make a change to the sensor!

I think that it’s working, but in about 15 mins of testing it has already failed to turn of the switch once in about 5 tries:

This is the code that I have - (obviously using short timescale for testing):

- platform: history_stats
  name: heating_monitor
  entity_id: switch.boiler_bot
  state: 'on'
  type: ratio
  duration:
    minutes: 2
  end: '{{ now() }}'


- id: 'heating_dumb_off'
  alias: 'Heating Dumb Off'
  initial_state: true
  trigger:
    - platform: numeric_state
      entity_id: sensor.heating_monitor
      above: '99'
  action:   
    - service: homeassistant.turn_off
      entity_id: switch.boiler_bot

I have also uploaded a screenshot of the sensor graph which I’m finding confusing!

Does the switch disconnect from the network? Why is it unreliable? Can you tell us if there is a status when it is offline?
For example, if the state is “unknown” or “unavailable”, you can add a wait_template and wait for it to connect again and then turn it off:

- id: 'heating_dumb_off'
  alias: 'Heating Dumb Off'
  initial_state: true
  trigger:
    - platform: numeric_state
      entity_id: sensor.heating_monitor
      above: '99'
  action:
    - wait_template: '{{ is_state("switch.boiler_bot", "on") }}'
    - service: homeassistant.turn_off
      entity_id: switch.boiler_bot

Be aware that if the switch remains for a loooong time as unavailable, then the wait template can be unreliable too…

Thanks for that suggestion!

The switch is bluetooth, so I’m guessing that this is the issue. My rPi and switch are about 2m apart though, so distance shouldn’t be a problem!

My logs get littered with these errors when it can’t connect:

Log Details (ERROR)
Tue Feb 05 2019 10:38:32 GMT+0000 (Greenwich Mean Time)

Failed to connect to switchmate
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/switchbot/__init__.py", line 48, in _sendpacket
    hand.write(binascii.a2b_hex(key))
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 163, in write
    return self.peripheral.writeCharacteristic(self.valHandle, val, withResponse)
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 502, in writeCharacteristic
    return self._getResp('wr')
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 369, in _getResp
    resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 326, in _waitResp
    raise BTLEException(BTLEException.DISCONNECTED, "Device disconnected")
bluepy.btle.BTLEException: Device disconnected

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/switchbot/__init__.py", line 36, in _connect
    bluepy.btle.ADDR_TYPE_RANDOM)
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 353, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 402, in _connect
    "Failed to connect to peripheral %s, addr type: %s" % (addr, addrType))
bluepy.btle.BTLEException: Failed to connect to peripheral FE:F9:C3:5F:8F:F6, addr type: random

also:

Log Details (ERROR)
Tue Feb 05 2019 10:43:00 GMT+0000 (Greenwich Mean Time)

Cannot connect to switchbot. Retrying
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/switchbot/__init__.py", line 48, in _sendpacket
    hand.write(binascii.a2b_hex(key))
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 163, in write
    return self.peripheral.writeCharacteristic(self.valHandle, val, withResponse)
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 502, in writeCharacteristic
    return self._getResp('wr')
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 369, in _getResp
    resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
  File "/usr/local/lib/python3.6/site-packages/bluepy/btle.py", line 326, in _waitResp
    raise BTLEException(BTLEException.DISCONNECTED, "Device disconnected")
bluepy.btle.BTLEException: Device disconnected

Also - just to add - the switch never seems to go into an unknown state. If i switch the switch on the interface, when it can’t connect it just slides back to off after a second or so.