Shelly MQTT determine if switched on by command or switch

I have many shelly1 devices controlling my main lights in each room. Based on lots of suggestions for stability I decided to run them all using MQTT and integrated them with HA as follows:

  - platform: mqtt
    unique_id: Hall_Main_Lights
    name: "Hall Main Lights"
    state_topic: "shellies/shelly1-8CAAB575877C/relay/0"
    command_topic: "shellies/shelly1-8CAAB575877C/relay/0/command"
    payload_off: "off"
    payload_on: "on"
    retain: "false"

These work great but I need to know if the light is on because of PIR sensor or switched at the wall.

There are are separate topics for these command topic above when switched from wifi (PIR) and input topic
for switch changes.

To make it more complex i use edge switching so the switch toggles meaning switching up or down could be how it switches the relay on. But the input topic always shows switch state i.e. 1 for down 0 for up.

I thought of the following approach but it sounded to complex:

  • Create a binary MQTT sensor for both command and input topics
  • Monitor state changes for each with their own automation then set some helper
  • drive my automation off the helper state change so I will know how the light was turned on.

I am still quite new to this so hoping smarter people than I have a better idea like some kind of template sensor or clever way to determine it using standard automation triggers/conditions

Thanks in advance for your help

In case anyone else tries to do the same thing later this is the simplest way that I could think of to break it down for Automating it.
-Ignore all the shelly topics above and simply follow this rule.
If the light turns on and the PIR is NOT detecting (has cool down period so stays on for a while) then it was switched on. Else it was the PIR.

This scenario is helpful when you want short periods of lights on for PIR sensor but want to tell it to stay on longer or always on by using the switch.

1 Like