Shelly Gen2 Mqtt Detatched Binary Sensor

Hi everyone! This is my first post in a homeassistant forum because I’ve generally been able to the solution to my problem as someone has usually posted to this forum before with the same problem.

My problem is this: I have a mix of Shelly Gen1 and Gen2s, the Shellies are integrated using the MQTT autodiscovery script for Gen 1 and Gen 2. GitHub - bieniu/ha-shellies-discovery-gen2: Script that adds MQTT discovery support for Shellies Gen2 devices

I want them to work in detached mode with a binary sensor to tell me when someone has switched the physical light switch so that a homeassistant automation can turn on/off my zigbee lights per bulb.

This code works for Gen1 and works very well.

  - platform: mqtt
    # Dining Room Shelly
    unique_id: "dining_room_downlight_switch"
    name: "dining_room_downlight_switch"
    payload_on: "1"
    payload_off: "0"
    device_class: "power"
    state_topic: "shellies/shellyswitch25-C45BBE60140C/input/1"

The problem is Gen2 Shelly mqtt is full of JSON with MQTT completely redone on the Gen2 side. I have a few Shelly Plus 1PMs in numerous places and I would really like to get this working with the rest of the smart home in this manner.

I really don’t like the Shellyforhass integration and only want to do it via mqtt.

Solution:

Ok I found some similar code and adapted it to my use case but here it is for everyone who is scared of JSON like me.

Note this is for a binary sensor:

  - platform: mqtt
    # Kitchen Downlights Shelly
    unique_id: "kitchen_downlights_switch"
    name: "kitchen_downlights_switch"
    payload_on: "True"
    payload_off: "False"
    device_class: "power"
    state_topic: "shelly-kitchen-downlights-switch/status/input:0"
    value_template: "{{ value_json.state }}"