Hello everyone,
First post here so go easy on me. So as the title may suggest I have come here from using OH for few years. I was mainly happy with it but I think I may be better off with HA, as long as I can get my head around the way it is configured. At this stage I have installed HASSIO on a pi3+ with SDimage and its up and running with one sonoff 4 channel discovered and using Tasmota. Apart from that it is bare with Samba, SH, Configurator, Mosquito running.
What I am trying to do first is get my head around how to configure the 4 channel pro first as this is used to control 2 way lighting (Called 3 way in the USA I believe) BUT also with Alexa control.
This is where I am stuck because in Openhab I had special items and rules defined because Alexa could not determine the physical state of the light. I also have a custom made arduino sensor with CT coils sensing when the lights are actually on and sending the state via MQTT to Openhab.
I suppose my questions are what is HA’s equivelant of Rules and Items in Openhab?
This is my wiring diagram of the lights (without the current sensing)-:
rule "Sonoff4ch_1Channel1 Toggle"
when
Item Sonoff4ch_1Channel1 received command
then
if (receivedCommand != Sonoff4ch_1Channel1.state) {
// only if new request does not match actual current state
// then we need to toggle the relay state
if (Sonoff_relay1.state == OFF) {
Sonoff_relay1.sendCommand(ON)
} else {
Sonoff_relay1.sendCommand(OFF)
}
} // else already in requested state
end
rule "Sonoff4ch_1Channel2 Toggle"
when
Item Sonoff4ch_1Channel2 received command
then
if (receivedCommand != Sonoff4ch_1Channel2.state) {
// only if new request does not match actual current state
// then we need to toggle the relay state
if (Sonoff_relay2.state == OFF) {
Sonoff_relay2.sendCommand(ON)
} else {
Sonoff_relay2.sendCommand(OFF)
}
} // else already in requested state
end
rule "Sonoff4ch_1Channel3 Toggle"
when
Item Sonoff4ch_1Channel3 received command
then
if (receivedCommand != Sonoff4ch_1Channel3.state) {
// only if new request does not match actual current state
// then we need to toggle the relay state
if (Sonoff_relay3.state == OFF) {
Sonoff_relay3.sendCommand(ON)
} else {
Sonoff_relay3.sendCommand(OFF)
}
} // else already in
end
rule "Sonoff4ch_1Channel4 Toggle"
when
Item Sonoff4ch_1Channel4 received command
then
if (receivedCommand != Sonoff4ch_1Channel4.state) {
// only if new request does not match actual current state
// then we need to toggle the relay state
if (Sonoff_relay4.state == OFF) {
Sonoff_relay4.sendCommand(ON)
} else {
Sonoff_relay4.sendCommand(OFF)
}
} // else already in
end
I think, as hard as it is, you need to stop thinking about equivalency. There’s been several threads from people in your position and the way that the two systems operate are just too different to say ‘entities are things’ or ‘automations are rules’ or whatever.
So, clear your mind of the OH concepts completely if you can,then…
It looks like you’ll need to define 4 mqtt lights, one for each channel of the device, something like this for the kitchen lights…
This then creates a virtual ‘light switch’ in homeassistant (we would just call it a ‘light’) that knows whether it is on or off from the stat topic, that when you toggle it on the interface will toggle the light via the cmnd topic, and you can automate the turning on/off of that switch and expose it to Alexa.
Hi Marc,
Thanks for the pointers and your time to layout the code for me. Yes I have been trying to forget about the openhab way of doing things but its hard as I got my head around quite few complex problems to get it all working.
Anyway, I dont think I explained what I am trying to do with these lighting circuits. The problem I face is that there are 3 ways I can physically turn the lights ON and OFF. Alexa, Home Assistant and a physical light switch.
The biggest problem I face is the fact that Alexa has no way of knowing what state the light is in (ON or OFF)
So what I did (with some help from some very clever people!) is had CT sensors send an MQTT message stating if the lights were physically on or off. I then used this data to allow the system to determine if the Sonoff device relays should be turned ON or OFF when Alexa is asked to turn the lights ON or OFF. So in some switch conditions the lights could be physically OFF but the Sonoff relay could be ON as they are single pole double throw on a 4 channel pro.
I have successfully got the state of the switch to change when the MQTT message comes in. So I know all the MQTT server etc works fine, its just my understanding of what I need to do in HA thats very sketchy…
What I am going to look at next is making the CT coil sensor I have built a sensor type and not a switch in HA. Then attempt to get my head around the equivalent of rules in HA.
What I cant get my head around is when I use Alexa I need to be able to intercept the command to the Sonoff and determine if I need to turn the relay on or off to change the light condition. In openhab I listened for the command and then triggered a rule to check (as above).
Could anyone please suggest how I go about this in HA? I have gone round and round in circles looking at input_boolean, switch templates, and scripts. But I just cant make the connection with all these things at the moment…
I don’t understand the need for the binary sensor? It is using the same topic as the light, so is just duplicating the state of the light.
I also don’t understand why you need to intercept anything about anything when using Alexa. Expose the light to Alexa, shout at her to turn on the light, light turns on. Opposite command turns the light off.
Hi Marc,
Yes thats exactly what I thought when doing this a couple of years ago on Openhab. The reason that does not work is because of 2 way switching and the physical state of the light (on or off). I dont think the topics are duplicated, one is from the sonoff relays and the other is from my custom CT coil sensor that gives the physical states of the lights.
If say the relay in the sonoff is off and the lights are physically off, yes indeed, shout at Alexa and she will turn the lights on, shout at her again and she will turn them off. Great so far.
Now, imagine the sonoff again the relay is off, someone comes in from work in the dark and turns on the physical light switch on the wall. The sonoff relay is still off and the lights are physically on. Still great so far. That person now grabs a cup of tea and crashes on the sofa for a bit to relax. But forgets to turn off the kitchen light on the way back, doh… Ahh Alexa, turn off the kitchen light, she replies with the usual bing but the light stays on…hmmm
The problem here is that the sonoff relay is already off and cannot be turned off again. If you ask alexa to turn the kitchen lights on (when they are already on) then indeed the light goes out. BUT this does not help the wife factor one little bit.
Hope you see what I mean? I am assuming there is no smarter (easier way) of doing it in HA?
I have already progressed to the point where I have a script and automations that make this work from asking questions in another thread. IF and ELSE syntax for 2 way lighting script
I’m still a bit confused as to how all this works, but on the basis of that sentence I’m thinking that what you actually want is to change that mqtt light you defined to an mqtt switch (literally change the line light: to switch:) so it becomes switch.kitchen_light or whatever, then use a template light…
… That accurately reflects whether you’re light is on based on the state of the coil, and the switch on/off actions will be to toggle the relay. Then you expose that to Alexa and it will know whether it is on or off.
Yeah what you are saying makes sense, I think its just my understanding of HA and template switches and lights. Its on my to do list to take a look at these.