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

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

Soo I have my sonoff door sensor was Swtich on Home assistant,
I did try Create a template to Binary without success and now I need help.

My ideia is:

if states.switch.sonoff_1000e0ed27.== “on”
{
binary.Sonoff = on
}
elseif states.switch.sonoff_1000e0ed27.== “unavaliable”
{
binary.Sonoff = binary.Sonoff.LastState
}
else
{
binary.Sonoff = off
}

That would be hard. I don’t think you can access history with templating. You would need to use a web sensor with the API I think…

Soo, I came across with another sample ideia that works. Soo this topic can be close

Configuration.yaml

sensor:

  • platform: sql
    db_url: !secret db_url
    queries:
    • name: Sonoff_GetastState
      query: >
      SELECT state
      FROM “states”
      WHERE entity_id like ‘%sonof%’
      AND state NOT IN ( ‘unavailable’, ‘unknown’)
      ORDER BY Last_Updated desc
      LIMIT 1;
      column: ‘state’

How did you manage to get DW2 WiFi working with HA, was it via HACS or some other way? I did pair with eWeLink, DW2 is now connected to my wifi, I know its IP address. But no idea how I can make it working.
Could you please advise?

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