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

Just Add sonof addon Via HACS and tem add this in configuration.yaml

sonoff:
username: ‘XXXXX’
password: ‘XXXX’
scan_interval: 60 #(optional, lower values than 60 won’t work anymore!)
grace_period: 600 #(optional)
api_region: ‘eu’ #(optional)
entity_prefix: True #(optional)
debug: False #(optional)

2 Likes

It seems that DW2 as many other devices only works if you use and pay the cloud of home assistant.
I use the alexxIT addon, have 2 DW2 sensors but didn’t work.
Read the documentation of alexxIT.

You don’t need Nabu Casa to do that. I think you’re getting very confused, which part exactly in the docs says that?

Hi. Any further progress on this. I can’t see the DW2 in “Entities” in HA.

  • It’s connected to ewelink
  • Other Sonoff device import automatically in HA via HACS
  • I can also see it in the config file “.sonoff.json”

I still can’t see it in HA.

Any logs? You might want to create a new topic, because this topic is about a different one.

@KTibow. Will do. Thx.
New Topic now: Sonoff DW2 WiFi not showing in Home Assistant

Ok I thought about doing a new post but I guess it will help some people here so ill post here:

Im using DW2 WIFI and im pulling last state from the database. Got the states fo the doors and created a template.

For the battery is a little more complicated. It only outputs the voltage (3volt max) so here we go:

Im using this for the voltage percent:

* platform: template
sensors:
iron_door_battery:
friendly_name: "iron_door_battery"
unit_of_measurement: '%'
value_template: "{{ (state_attr('binary_sensor.sonoff_1000e0e51a', 'battery')|float/0.03)|round(1) }}"
icon_template: mdi:battery

Problem is, that it also goes away when the sensors go offline, so I went to sqlite to pull the last state also for the battery with this:

* name: Sonofflock_GetbatteryState
query: "SELECT substr(attributes, instr(attributes, 'battery')+10,5) FROM states WHERE entity_id like '%binary_sensor.sonoff_1000e0e51a%' AND state NOT IN ('unavailable') ORDER BY Last_Updated desc LIMIT 1"
column: 'attributes'

Now I’ve tried different string organization and I have tested it in the sqlite add-on and it pulls the voltage value, but when I go to the entity it says state unknown

Does anybody see what im doing wrong?

You could just create a self-referential template sensor that updates as long as the sensor’s aren’t offline

Could i get an example of that? That way I can pull the voltage info while the sensor is online, and eventually it will update when it comes up again.

Just use a is_state with an if and elif. There’s lots of tutorials on HA Jinja2.

Hi guys,
Many thanks for your ideas!
I could make the battery % work perfect. I have modify the formula to make the 0% at 2volts as I don’t think is going to work at all at that low voltage. Just added a substract and changed the numbers to:
value_template: "{{ ((state_attr('binary_sensor.sonoff_xxx', 'battery')-2)|float/0.01)|round(1) }}"

I could not make the other issue work. getting some errors not able to solve yet.
Mainly I am receiving an error at “column: ‘state’”. I will keep trying and listening you…

I am using them with that alexxIT addon. Only issue is the described here about the status lost and battery in volts, but they work seamless like the rest of sonoff devices.

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.