# To find the last 'healthy' state from the SonOff DW2 Wifi
- platform: sql
# db_url: !secret db_url. Not needed if the standard one is used
queries:
- name: sonoff_dw2_get_last_state
query:
SELECT state
FROM states
WHERE entity_id like '%binary_sensor.sonoff_1001039bba%'
AND state NOT IN ('unavailable', 'unknown')
ORDER BY Last_Updated desc
LIMIT 1;
column: state
Then, calling the āsensor.door_open_garaje_laststateā inside the lovelace entity card, it made the magic.
I must add I have discover just right now, my DW2 Wifi is refreshing its status by itself. It takes >30ā but it refresh.
I will keep it in observation, but If this is true, not sure if it worth all this extra code.
For fun of course, I have enjoy a lot, but I love simple codes when possibleā¦
Anyway, I have only one DW2 wifi which I will keep. Now with battery in % and this retain state, it is great. I have got another DW2 but RF which is going direct to garbage as it has no state, just send state when opening (now it is just in my desk to know when the kids āborrowā my pens).
I have try the SonOff Zigbee SNZB-04 together the bridge and these are perfect. No issues to install. Never loose the state but they refresh their status enough quick enough in some seconds/minutes after a reset. Battery last 5 times the DW2 one and they are 3/4 of the size.
Sorry for the late answer, the documentation of alexxIT says that it is only in the cloud available.
In the main time I have connected to the cloud and directly after connect the DW2 is available.
Hola, where did you write this code, to convert the battery level to %?
Iām using HACS integration: Battery State Card / Entity Row
As you can see in the following picture, just the first one fails. The one with a Sonoff DW2 entity.
Battery percentage is a ānew sensorā you have to add.
That code will go at configuration.yaml or in sensors.yaml if you have that file āincludedā in the configuration.yaml
So you can choose to use any of them.
Just note I donāt know the Min voltage to be 0% so I just took 2v as 0% and 3v as 100%
I have the hacs installed but I already have an ewelink switch in homeasistant, these if it detects them but the DW2 wifi does not, I have created a sensors.yaml and I have written the FelisucoVFR code
Felix, whatās up, but there is no way, I understand that in cloud mode, two devices at the same time does not work? Thanks
It seems to me that this device the DW2 wifi fails more than a fairground shotgun, in the app it works perfectly but in the home assistant it is worth it, at least to me, it will be a matter of looking for another brand that does not have problems, thanks to all for the answers, a hug.
Sorry about the very very late respond. Sure this is useless nowā¦
Two sensors work perfect but each of them need itās own code with different names. Thatās all.
I still have it working and I have to say it is reliable. Since last AlexxIT versions as integration it is even easier as you already have the voltage as a separate sensor, so no need that template.
After near two years working I have to say their batteries last near a year.
My only complain is about the size. Rest is good for me.
Excuse me to come back with this thread, but after the last upgrades, the % for the battery level doesnāt work anymore, and Iām going nuts.
What Iām trying now is as follow, but doesnāt work, any ideas, please: