Sonoff DW2 wifi - Home Assistant Switch to Binary, and get last state when unavaliable

Thanks a lot David!
I could finally make mine work with just a little of work with the marks, quotes, etc.

Note the extra ifā€™s to receive the Closed and Opened instead of on or off.
At sensors.yaml:

  - platform: template
      door_open_garaje_laststate:
        friendly_name: Puerta Garaje
        value_template: >-
          {% if states('binary_sensor.sonoff_1001039bba') == 'on' %}
            {{'Open'}}
          {% elif states('binary_sensor.sonoff_1001039bba') == 'off' %}
            {{'Closed'}}
          {% else %}
            {% if states('sensor.sonoff_dw2_get_last_state') == 'on' %}
                {{'Open'}}
            {% elif states('sensor.sonoff_dw2_get_last_state') == 'off' %}
                {{'Closed'}}
            {% else %}
                {{ states('sensor.sonoff_dw2_get_last_state') }}
            {% endif %}
          {% endif %}

and just after that:

# 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.

image

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.

2 Likes

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.

You can publicly expose your HA instance without Nabu Casa.

HEllo.

Guys, just update to latest HA version and the Sonoff DW2 will work perfectly without any additional configuration.

Iā€™m using HACS integration and now my DW2 is working perfeclty.Selection_641

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.
bateria

What do you mean?

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%


Probably will need to adjust to 2.2 or little more. Letā€™s see when stops working in a few months.

Here you can see, after a reboot, all sensors related to DW2 become unavailable except the one coming from the sql, saved as last state.

I canā€™t for the life of me register the DW2 RF as closed with the Ewelink app.

Add - Alarm and open the door. This is fine.

But when I click ADD when open then shut the door it doesnā€™t registerā€¦

Any ideas?

Thanks

Hello Good afternoon, I would like to know how I can integrate the DW2 wifi device in home assistant, thank you, I am trying but there is no way.

You will need to install GitHub - hacs/integration: HACS , follow the instructions. and then it finds the sensor automatically

Regards

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:

# Bateria del sensor PuertaEntrada
template:
  - sensor:
    - name: "PuertaEntrada_battery_perc"
      unit_of_measurement: "%"
      value_template: "{{ ((states('sensor.sonoff_1000e0e148_battery_voltage')-2)|float/0.01)|round(1) }}"

:sweat_smile: :sweat_smile: I solved this way:

  - platform: template
    sensors:
      puertaentrada_battery_perc:
        friendly_name: "PuertaEntrada_battery_perc"
        unit_of_measurement: "%"
        value_template: "{{ ((states('sensor.sonoff_1000e0e148_battery_voltage')|float-2)/(0.01)|float)|round(0) }}"
1 Like

you got the voltage, and then voltage -2 ā€¦ and then / 0.01

but thereĀ“s no relation between voltage and battery levelā€¦

for example, my voltage is 2,581 ā€¦ using your math, my battery level is 58%ā€¦ no itĀ“s notā€¦ i just replaced the batteriesā€¦they are 100%

how did you solve it?
I know my solution wasnā€™t exact but an aprox. at least

I didnā€™tā€¦ still no solutionā€¦ DW2 used to have a battery-level entityā€¦but not anymoreā€¦ I donā€™t know why it was deprecated

but your math is wrongā€¦like I saidā€¦thereĀ“s no relationship between voltage and battery level.

maybe with tasmotaā€¦

Just in case someone is still interested:

    door_garaje_pct_battery:
      friendly_name: BaterĆ­a puerta garaje PCT
      unit_of_measurement: "%"
      value_template: >-
        {% set minvdc = 2.25 %} # Minumum voltage 0%
        {% set maxvdc = 2.80 %} # Max voltage 100%
        {% if states('sensor.puerta_garage_battery_voltage')|float < minvdc %}
          0.0
        {% elif states('sensor.puerta_garage_battery_voltage')|float > maxvdc %}
          100.0
        {% else %}
          {{ ( 100/(maxvdc-minvdc) * ((states('sensor.puerta_garage_battery_voltage')|float - minvdc)))|round(0) }}
        {% endif %}
      device_class: battery

@Guilherme_Gimenes It can be linear, logaritmic or whatever, but there is always a relation.