A few Sonoff switches controlled by MQTT - How to get them in HA?

I’ve got a setup that’s been built piece by piece as I learn and may not be optimal but…

I’ve got a VM that runs HA and Node-Red (plugin) – At 6pm, Node red triggers MQTT messages to 4 outside Sonoff switches to turn the lights on.
It turns them off at 0600.

It normally works unless there is a power outtage, some start on, some start off?

My questions are

  1. Is there a way in HA I can poll a Sonoff switch via API and return the value, say every 5 minutes? In case during the night, I see one of the switches off, I can turn it on. Would like to figure out a way to know in somewhat real time if the light is on or off.
    1b) There may be other things I’d like to poll too, maybe a ping to see if Internet is online, etc? This can be a custom script?

perhaps have a look at retained messages for the state and status of your sonoff devices in MQTT - It is unclear what you use to control the sonoffs. I have programmed my own before tasmota etc were prevalent.

Then you can subscribe to the state of the device when home assistant begins (in case it restarts)

Once home assistant core reliably knows the state of the tasmota’s, you are ready for control.

I am not familiar with node red … this is a home assistant core answer

Thanks Phil.

I’ll take a look at the retained messages. That seems likely.

To control the Sonofs, I use a timer in Node-Red that at on time will send an ON to the speciic MQTT topics that I have set (I didn’t use the discovery, I use seperate topics for each) – then Off at certain times…

Do you have a link about HA subscribing to the topic to get the latest state? Maybe I can use that to display a switch in Lovelace so I can see.

There’s a pretty good video on the hookup about retain settings in Tasmota.

For those landing on this thread later. this is what DavidW1960 mentioned

I had the retain flag in HA set… Hmmmm

Anyone with a good example on how to get my switches to automatically update? It seems they are not listening to the state topic.

1 Like

Maybe that is the problem. If you show how you configured them, maybe we can spot the error why you don’t see the state.

There is an automation in the Tasmota Wiki to make sure HA gets the state on a restart.
If you had retain: true in your switches you need to delete the retained messages from the broker. MQTT Explorer is ideal for fixing that. This will cause other issues if you don’t fix it.

I think I’ve fixed my issues about the retain part. I had retain in my config. I also did the steps in the video above, setting the last message retained to the MQTT. I can simulate a power loss by restarting the MQTT server. The lights are no longer in a opposite state.

Now the issue is that my lights around the house are on (it’s 7pm here) but in the HA they are show that they are off. If I click the switch (to On) Then click off, they indeed turn off and the switch is in sync.

Here is how I added the switches in my configuration file

  - platform: mqtt
    name: "Rear Patio"
    command_topic: "/Home/Outside/IoT-Sonoff-1/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-1/cmnd/Power"
    payload_on: "ON"
    payload_off: "OFF"    
    retain: false
    
  - platform: mqtt
    name: "Side Patio"
    command_topic: "/Home/Outside/IoT-Sonoff-3/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-3/cmnd/Power"
    payload_on: "ON"
    payload_off: "OFF"    
    retain: false
    
  - platform: mqtt
    name: "Living Room Window"
    command_topic: "/Home/Outside/IoT-Sonoff-4/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-4/cmnd/Power"
    payload_on: "ON"
    payload_off: "OFF"    
    retain: false
    
  - platform: mqtt
    name: " Room Window"
    command_topic: "/Home/Outside/IoT-Sonoff-5/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-5/cmnd/Power"
    payload_on: "ON"
    payload_off: "OFF"    
    retain: false

I’ve tried toying around with tele as the state topic but I still cannot get it to show.

here is teh info from MQTT Explorer.

After toying around for hours with different topics, I still cannot get the switch in HA to get the current state. It seems that HA doesn’t receive or parse the messages in the topics.

Is there a way every 5 minutes I can poll the state of my tasmota sonoff switch and report it? Or poll it any receive it in real time if I use something other than HA (MQTT message from my phone) to turn on the switch

The state topic is wrong.

"/Home/Outside/IoT-Sonoff-4/cmnd/state"

I have tried all 3 and still seem to get no state at all.

Tried /tele/state and /stat

  - platform: mqtt
    name: "Rear Patio"
    command_topic: "/Home/Outside/IoT-Sonoff-1/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-1/tele/State"
    payload_on: "ON"
    payload_off: "OFF"    
    retain: false
    
  - platform: mqtt
    name: "Side Patio"
    command_topic: "/Home/Outside/IoT-Sonoff-3/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-3/cmnd/state"
    payload_on: "ON"
    payload_off: "OFF"    
    retain: false
    
  - platform: mqtt
    name: "Living Room Window"
    command_topic: "/Home/Outside/IoT-Sonoff-4/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-4/stat"
    payload_on: "ON"
    payload_off: "OFF"    
    retain: false

image

Well. I’ve attempted to follow so many links and into templates is where I got … It seems stat is immediately after a change and STATE is broadcast on interval… Still nothing works

  - platform: mqtt
    name: "Side Patio"
    command_topic: "/Home/Outside/IoT-Sonoff-3/cmnd/Power"
    state_topic:  "/Home/Outside/IoT-Sonoff-3/cmnd/stat"
    state_value_template: "{{value_json.POWER}}"
    value_template: "{{value_json.POWER}}"
    payload_on: "ON"
    payload_off: "OFF"  
    state_off: "OFF"
    state_on: "ON"
    retain: false

Not sure if I should be grabing the stat topic or STATE

I’ve looked at the following links.
https://tasmota.github.io/docs/#/integrations/Home-Assistant

When I try what is listed here with the developer tools/listning, i don’t see any messages at all
https://tasmota.github.io/docs/#/integrations/Home-Assistant

But right now the switch is On the sonoff, but in the HA UI it is off. If I switch it in HA off and On the command topic does work so I know my mqtt connectivity is there.

Here is teh MQTT config

mqtt:
  broker: 127.0.0.1
  username: (myusername)
  password: (mypass)

but this just shows no messages. I’m really stuck

image

well… I’m not sure where to go. Thoroughly upset…

I’ve uninstalled the old broker. Re-set up Mosquitto plug following rob post here

Tried auto-discovery and not.

I can use the switch in the UI to turn off and on the light, but I just can’t get any state to show if I turn it off or on via mqtt or via the UI. Basically, the switch doesn’t update with the state. If I turn it On, refresh the page, it’s gone…

I’ve run out of ideas.

One thing to note, when I try under Developer tools to listen to the STAT or other topics. I don’t see any messages. Not sure if that’s relevant?

This shows nothing

Very little chance listening to /# will return something. Very few mqtt implementations start their topics with /. For sure tasmota does not. Listen to # , not /#

Edit : probably your issue all along, you define your topics starting with /

Thanks Francis.

I have changed one of my sonof’s to not have the leading /… Tried to listen again under Developer Tools > Events and #… Nothin

Also, still no change in the state…

  - platform: mqtt
    name: "Living Room Window"
    command_topic: "Home/Outside/IoT-Sonoff-4/cmnd/Power"
    state_topic:  "Home/Outside/IoT-Sonoff-4/stat/Power"
    state_value_template: "{{ value_json.POWER }}"
    value_template: "{{ value_json.POWER }}"
    payload_on: "ON"
    payload_off: "OFF"  
    state_off: "OFF"
    state_on: "ON"
    retain: false

When I listen to “call_service” under Dev> Events
and turn off and on this light from the Lovelace UI

Event 5 fired 11:06 AM:
{
    "event_type": "call_service",
    "data": {
        "domain": "mqtt",
        "service": "publish",
        "service_data": {
            "topic": "Home/Outside/IoT-Sonoff-4/cmnd/Power",
            "qos": 0,
            "retain": false,
            "payload": "OFF"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-03-25T04:06:53.695287+00:00",
    "context": {
        "id": "0f99cd66dfa14684a9a6f6f462783942",
        "parent_id": null,
        "user_id": null
    }
}
Event 4 fired 11:06 AM:
{
    "event_type": "call_service",
    "data": {
        "domain": "switch",
        "service": "turn_off",
        "service_data": {
            "entity_id": "switch.living_room_window"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-03-25T04:06:53.694227+00:00",
    "context": {
        "id": "135bea19e3614a6b9d08c2494534f397",
        "parent_id": null,
        "user_id": "3acc9e2704cb46cd86576e86eedb7333"
    }
}
Event 3 fired 11:06 AM:
{
    "event_type": "call_service",
    "data": {
        "domain": "mqtt",
        "service": "publish",
        "service_data": {
            "topic": "Home/Outside/IoT-Sonoff-4/cmnd/Power",
            "qos": 0,
            "retain": false,
            "payload": "ON"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-03-25T04:06:53.041330+00:00",
    "context": {
        "id": "e79d80962ebd42cea60871719bd9cabc",
        "parent_id": null,
        "user_id": null
    }
}
Event 2 fired 11:06 AM:
{
    "event_type": "call_service",
    "data": {
        "domain": "switch",
        "service": "turn_on",
        "service_data": {
            "entity_id": "switch.living_room_window"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-03-25T04:06:53.040250+00:00",
    "context": {
        "id": "54479e877f804b62999fa340d8bc57c5",
        "parent_id": null,
        "user_id": "3acc9e2704cb46cd86576e86eedb7333"
    }
}
Event 1 fired 11:06 AM:
{
    "event_type": "call_service",
    "data": {
        "domain": "mqtt",
        "service": "publish",
        "service_data": {
            "topic": "Home/Outside/IoT-Sonoff-4/cmnd/Power",
            "qos": 0,
            "retain": false,
            "payload": "ON"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-03-25T04:06:48.139784+00:00",
    "context": {
        "id": "c4c76e77ca3e45aa81c085d7746153a8",
        "parent_id": null,
        "user_id": null
    }
}
Event 0 fired 11:06 AM:
{
    "event_type": "call_service",
    "data": {
        "domain": "switch",
        "service": "turn_on",
        "service_data": {
            "entity_id": "switch.living_room_window"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-03-25T04:06:48.136890+00:00",
    "context": {
        "id": "b6b85cf230854283afaad36d3b8124c0",
        "parent_id": null,
        "user_id": "3acc9e2704cb46cd86576e86eedb7333"
    }
}

maybe I cannot debug MQTT messages from the Developer Tools > Events

When I look in the logs I see

Log Details (WARNING)
Wed Mar 25 2020 10:58:05 GMT+0700 (Indochina Time)
Your configuration contains extra keys that the platform does not support.
Please remove [state_value_template].  (See /config/switch.yaml, line 23). 
Offending data: {"platform": "mqtt", "name": "Living Room Window", "command_topic": "Home/Outside/IoT-Sonoff-4/cmnd/Power", "state_topic": "Home/Outside/IoT-Sonoff-4/stat/Power", "state_value_template": "{{ value_json.POWER }}", "value_template": "<redacted>", "payload_on": "<redacted>", "payload_off": "<redacted>", "state_off": "<redacted>", "state_on": "<redacted>", "retain": "<redacted>"}

Honestly at a loss here and not find the right documentation to piece my solution toggether…

Finally got it.
I removed the value template and it works
Seems, the error doesn’t agree with this

The error is here

020-03-25 04:21:52 ERROR (MainThread) [homeassistant.components.persistent_notification] Error rendering message Template("Your configuration contains extra keys that the platform does not support (but were silently accepted before 0.88). Please find and remove the following.This will become a breaking change.

- [state_value_template].  (See /config/switch.yaml, line 0). 
Offending data: {"platform": "mqtt", "name": "Rear Patio", "command_topic": "/Home/Outside/IoT-Sonoff-1/cmnd/Power", "state_topic": "/Home/Outside/IoT-Sonoff-1/tele/state", "state_value_template": "{{value_json.POWER}}", "payload_on": "<redacted>", "payload_off": "<redacted>", "state_off": "<redacted>", "state_on": "<redacted>", "retain": "<redacted>"}

But the mqtt switch document says value templates

Er… Edit… Looks like state_value_template is deprecated but value_template is still there…

Ughh… Hours… Gone…

Last question on this… As i’m eagerly trying to understand the value template

Definition
value_template. — (string)(Optional)
Defines a [template] to extract device’s state from the state_topic . To determine the switch’s state result of this template will be compared to state_on and state_off .

I tested my template in the template editor and it returns On so the template is ok

From my switch.yaml file

  - platform: mqtt
    name: " Room Window"
    command_topic: "Home/Outside/IoT-Sonoff-5/cmnd/Power"
    state_topic:  "Home/Outside/IoT-Sonoff-5/tele/STATE"
    value_template: "{{ value_json.POWER }}"
    payload_on: "ON"
    payload_off: "OFF"   
    state_off: "OFF"
    state_on: "ON"
    retain: false

That should say read the tele/STATE topic for any message. When the message comes in in JSON format, parse it for Power and read that value. If it matches On, then the switch goes on…

Tested… It works

Fianlly. I can read from the Power state that happens within seconds. Or I can read from the STAT update that comes in every 5 minutes…

Thanks all for the help. Hopefully the details above can help someone else