ESPHOME SONOF S26 Timed Light
by theblujuice in Circuits > Electronics
4111 Views, 11 Favorites, 0 Comments
ESPHOME SONOF S26 Timed Light
Good Day. So I have a fish tank light that I want to switch on and off a certain time of the day. I just had to make it complicated for myself. I want to be able to change the time it switches on and off from my Home Assistant dashboard. Maybe even more.
So lets just see how much more I can take myself into a rabbit hole.
Step 1: I Need a Way to Send the Times From Home Assistant.
Some googling helps. Some pulling out your hair helps, if you actually have any :P
Open my Visual Studio Code Editor add-on. Under Config folder find the configuration.yaml file and copied and pasted some stuff I found hidden in some forum. Sorry if I remembered I would give acknowledgement. Added the code and restarted home assistant.
Step 2: Added the Following Code to Configuration.yaml
input_number:
ft_start_hr:
name: FT Light Start Hours
icon: mdi:clock-start
min: 0
max: 23
step: 1
# initial: 13
The first line will be the input number entity id.
Next we need some details for this little dude:
A name will be awesome: FT Light Start Hours for this one P.S. FT is for Fish Tank... Not what you were thinking right?
Next is an optional icon. Plenty out there to chose from so go crazy... or not.
Then we can put in a min, max and step value. As this is hours I chose 0 - 23 with single steps.
Initially I had an initial value and when home assistant restarted it would change it to this initial value. I decided to comment it out as I wanted Home Assistant to try and remember the last value I had set for it.
You can put in mode to chose a box. But as default it chooses a nice slider for you. I think a slider will do for now.
Be aware of the indentations as they are very important.See the image
So these will give you entities in home assistant to be able to adjust.
Here is a link to more information about input numbers:
Step 3: Now We Need to Get the Information to ESPHOME API
sensor:
- platform: template
sensors:
ft_start_hr:
value_template: '{{ states.input_number.ft_start_hr.state | int }}'
This will give ESPHome API the connection required, as well as it is now an integer instead of fancy text slider :-P
Here it is taking the input number state and converting it to a integer and storing it in a sensor.
Here is some more information on the templates:
https://www.home-assistant.io/integrations/templat...
Oh I changed the File extension of my file to text so I can Upload it... Instructables may be destructible with a yaml...
Downloads
Step 4: So the ESPHome Configuration of the Plug Now
I started with a base config for the s-on-off plug and then started tinkering.
I will not go into detail on how to flash the S26 plug with ESPHome as google has all the answers.
My full configuration file will be available to download at the end. Da Da Dah...
So lets start with the linked part to Home Assistant:
We have to create sensors that match the sensors in home assistant so they can talk to each other.
Step 5: the ESPHome Home Assistant Connection... Beam Me Up Scotty
sensor:
- platform: homeassistant
entity_id: sensor.ft_start_hr
id: id_ft_start_hr
So now we create a sensor in ESPHome of the type homeassistant. Who would have guessed?
The entity id will have to match what you set up in your configuration.yaml file.
I set up an id so that ESPHome could reference this sensor in its abundance of thinking.
Step 6: Lets Add Some Time
So with this being a timed plug... Lets actually add some time... Or is it subtract? It is now in the past?
Here are some basics of the time component. We can chose some different ones but I chose homeassistant. Too easy?
Step 7: Interval
No its not a break, or is it?
interval:
- interval: 1sec
then:
- lambda: |-
id(gl_ft_start_hr) = id(id_ft_start_hr).state;
So I set an interval of 1 second to write the sensor value from home assistant to the global variable.
I just want to do this once a second as time is ticking...tick tock
Step 8: So We Have Time and Money... What Next?
So I decided to use a binary sensor template to determine whether its time to switch on the switch switch switch... relay
I created three switches with the id's: start_time_valid, stop_time_valid and run_time_valid
start_time_valid is true when it is now past the time set up to switch on the relay.
I used nested if statements to check if we were greater or equal to the seconds required, then minutes, and finally hours.
stop_time_valid is true when it is now before it is time set up to switch off the switch. This is reversed for checking that now has not past the required time to switch off the relay
run_time_valid is true when both start_time_valid and stop_time_valid are true. I suppose I could have just used a normal and condition for this one. I just got into the swing of things with lambdas.
- platform: template
name: "Start Time Valid"
id: start_time_valid
lambda: |-
if (id(homeassistant_time).now().hour > id(gl_ft_start_hr)) {
return true;
}
else if (id(homeassistant_time).now().hour == id(gl_ft_start_hr) &&
id(homeassistant_time).now().minute > id(gl_ft_start_mn)) {
return true;
}
else if (id(homeassistant_time).now().hour == id(gl_ft_start_hr) &&
id(homeassistant_time).now().minute == id(gl_ft_start_mn) &&
id(homeassistant_time).now().second >= id(gl_ft_start_ss)){
return true;
}
else {
return false;
}
Step 9: Now for Some Scripts: 'To Be, or Not to Be: That Is the Question'
Hmm wrong script dude!
So if I plugged in the switch... or the house power just came back on, I wanted the switch to know whether it is meant to be on or off and act accordingly. (yes we all going for acting lessons)
The first script id: checkingstatuson, is checking whether we are meant to be on and acting accordingly. I also included a global flag with the id: trackingon, to ensure that this script is just run once and does not impede my other scripts from doing their part in the play.
This has 3 and conditions: the relay is off, its meant to be on and I have not already done my part. This will then switch off any other scripts running, turn on the relay and set the tracking flags.
The second script id: checkingstatusoff, is just the opposite of the first. We just checking whether we need to close the curtains now. The tracking flag for this one is trackingoff
I run these two scripts in the 1 second interval to check every second.
Step 10: Some Bonus Scripts
Two more features I wanted to add was if I switched the light off at the tank that it will switch back on after 5 seconds if the light is meant to be on at that time. Also, if the light was off and I wanted to switch it on to see my fishies and it was out of its scheduled on time, that it would switch on for 5 minutes. Yes 5 minutes is long enough... Get back to work.
The first script id: relayisoff will switch on the light for 5 minutes if it's out off scheduled on time and then turn off.
The second script id: relayison is practically reversed of the first and will switch the light back on 5 seconds later if its meant to be on. Who said you can switch off my light mister!
Here is some more information about scripts:
https://esphome.io/guides/automations.html
Some bonus information about scripts is they are non blocking. Unless you put a delay in a lambda.Uh No No No
Step 11: Some Final Thoughts
From home assistant I wanted the switch to act the same way as the button.
I did not put a name for the binary sensor local switch as did not need to see it being pushed in home assistant.
I did not put a name for the relay switch as I wanted to use the scripts for doing the switching from home assistant.
I created a template switch with a name so that I can use that to switch from home assistant. It will run my scripts the same as the local switch at the plug.
I have not tested whether switching my home assistant off if the plug will still work correctly. I'm hoping so. I have seen some posts about some issues when losing connection to the Home Assistant API.
I really hope this little tutorial will help you and please feel free to help me make it better. I'm no expert and learning very slowly. I just thought some things I learnt how to do may help someone out there struggling to figure it out.
Now to change the times in home assistant based on sunrise and sunset...+ - a few