For one of my first automations in Home Assistant (starting to replace my Node-Red home control code) I picked a new Aqara no-neutral switch I’ve just fitted to our just-redesigned bathroom wall – to turn the bathroom light on and off – manually or remotely. I wanted a 20 minute timeout. FAIL. The bathroom light was on from my wife getting washed. I went into the bathroom, turned off the light and back on to ensure I got my full 20 minutes of light in the shower – erm, NO – after 10 minutes it went off.
Here is the code for the automation – put together initially by CHATGPT:
alias: Turn off bathroom light after 20 minutes description: Turn off the bathroom light 20 minutes after it is turned on. trigger: - platform: state entity_id: switch.aqara_bathroom_light to: "on" condition: [] action: - delay: hours: 0 minutes: 20 seconds: 0 milliseconds: 0 - service: switch.turn_off target: entity_id: switch.aqara_bathroom_light mode: single
Being an absolute amateur at Home Assistant after only 2 weeks of using it – I cheated and asked CHATGPT to write the code for me. I could of course done this in the UI but the code FELT right when I looked at it as above. I’d no real idea what the MODE meant and sure enough – it all worked but times out after the FIRST light-on operation. not the last.
I showed CHATGPT the above code – not telling it that it gave me the code in the first place. I said “in Home Assistant, modify the yaml code below so that if the light is turned off then on during the timeout – the timeout starts afresh. Here is the code.” – I gave it the code above.
This is so surreal – ChatGPT came back with this “To modify the automation so that if the light is turned off and then back on during the timeout period, the timeout starts afresh, you can change the mode
of the automation to restart
. This way, every time the light is turned on, the timer will reset. Here is the updated YAML code:” – ansd gave me the code with as it said ONE CHANGE – the mode set to restart instead of single.
All looks pretty obvious in hindsight – but then that’s how things normally look after the event.
If like me you’ve been avoiding using AI for programming because of hype and “delusions” as complete mistakes are often called – maybe time to have a rethink? My NEXT job will be much more complex – and so on until ChatGPT lets me down as it used to months ago.. but maybe now my questions are just better phrased though last night if did create a couple of howlers as ChatGPT data is maybe a couple of years old and HA is changing at a fast rate so there’s still a need for care.
from a useability point of view, hopefully just pressing ON rather than OFF then ON could work?
Since then I realised that mode: single is not ideal – mode: restart solves the problem. The light will always come on for 20 minutes after the last button press. Lovely.
I’m quite happy using Node Red for all my automations. If you use the Node Red Contribution Websocket (https://flows.nodered.org/node/node-red-contrib-home-assistant-websocket) you will have direct access to all the HA devices and entities, you can even create them from Node Red. Also, you can create scenes such that you can invoke one from Alexa which will kick off a Node Red flow for you.