I have my bathroom fans connected with sonoffs and DHTs and they turn on/off automatically based on the humidity. Everything works great. However, some times the fans turn on in the middle of the night and that gets annoying. I am trying to find an automation that restricts the fan turning on by time. How can I add a time condition where the fan only turns on between 7 am and 10 pm?
# Example configuration entry
time:
- platform: homeassistant
id: homeassistant_time
Then, you’ll need to add a reference to this in the automation. Here is a link to the explanation of the time component in ESPHome. It contains ways to use the time component in automation.
Thanks for your reply but I already checked that documentation. I can easily use the Time component to implement the automation using only the time parameter. BUT what I want to do it combine both parameters (time and humidity) to turn on/off the fan. I hope this makes sense.
So what you have to do is use the ID of the time sensor to reference it in your on_value_range automation. I believe you’ll want to use an if statment:
I don’t have experience doing this, so this is just my guess - but should be close (check indentation… did some copy pasting):
You’d probably need to add a second if statement for the time being before 8am for example… or however you want to do the timing… this should be close and I think you can get it working from there…
I have checked the documentation and have tried different ways but it does not compile because of the ‘if’ ‘condition’ under the on_value_rage. Any ideas?
The error is:
Cannot have two actions in one item. Key ‘if’ overrides ‘then’
Ah!
I didn’t see that the then for the switch.turn_on was not indented into the if statement.
Good Catch - apologies I didn’t take the time to get my indentations correct
… shoddy help… but at least I got you on the right road .
Compiling /data/fan_1/.pioenvs/fan_1/src/main.cpp.o
src/main.cpp: In lambda function:
src/main.cpp:326:23: error: 'class esphome::homeassistant::HomeassistantTime' has no member named 'hour'
return ha_time->hour < 22;
^
src/main.cpp:327:3: warning: control reaches end of non-void function [-Wreturn-type]
});
^
*** [/data/fan_1/.pioenvs/fan_1/src/main.cpp.o] Error 1
========================== [ERROR] Took 14.00 seconds ==========================
Okay, that error is telling us that the time returned from Home Assistant does not define the hour by itself… I will have to look and see if I can tell what is returned from Home Assistant… I thought that would work…
Let me dig through GitHub and see if I can figure out what is returned…
DeadEnd
… Okay, the documentation isn’t great - but I think you need to add .now() after the ID and before the .hour… but this is just my educated guess…
- lambda: 'return id(ha_time).now().hour < 22;'
This tells it to pull the hour from the current time… previously we had not referenced what part of the time component we wanted I guess…
You’re the Man!!! That did it! Thank you again! I truly appreciate your help on this. Now my wife won’t get mad at me for waking up in the middle of the night when the fan goes off at 3:00 am.
Thanks again
@laca75tn this may (or considering the age of this thread might have already) turn on the fan after midnight when hour cycles back to 0. if so, you’d need a second condition to check if hour is also greater than say 6 in the morning
Hi all, i understand this an old post but better late than never… anyway I’ll jump into it. I have an esp-01s paired with a motion sensor on GPIO2 and an led on GPIO0 all set up and working in esphome. In HA I have an automation which triggers when motion is detected, it’s conditioned that it runs only before sunrise and after sunset, then it turns the led on, delays for 30 seconds after last motion detected, then turns the led off. My question is how can I hard program this automation into the esp device without pulling the time or sun state from home assistant (so it will work if wifi is down). I understand that having the condition based on the sun state working locally on the esp device is unlikely but is there a way to perhaps change the condition to a fixed time, say it only triggers after 7pm and before 7am? Is there a way to pull the time from home assistant periodically when wifi is stable and store this value so that if wifi goes down the esp can utilize the most recent saved time and calculate the current time by subtracting how many seconds have passed since the last saved time? Basically how can I store the time locally on the esp and make a local automation that conditionally utilizes the time?
here’s my current esphome yaml: