Skip to content

Script that adds MQTT discovery support for Shellies Gen2 devices

License

Notifications You must be signed in to change notification settings

maciejmajewski/ha-shellies-discovery-gen2

 
 

Repository files navigation

Shellies Discovery Gen2

GitHub Release GitHub All Releases Community Forum Buy me a coffee PayPal_Me

This script adds MQTT discovery support for Shelly Gen2 devices in the Home Assistant.

image

Prerequisites

This script needs Home Assistant python_script component so, if you never used it, I strongly suggest you to follow the official instruction and check that python_script is properly configured and it's working.

For the device to work with the script, it must have MQTT configured and options RPC status notifications over MQTT and Generic status update over MQTT enabled.

Installation

You can download shellies_discovery_gen2.py file and save it in <config>/python_scripts folder or install the script via HACS. You won't find Shellies Discovery Gen2 in the HACS Integrations section, nor add it as a custom repository. You must have a properly configured python_script component to be able to install the script from the HACS Automations section.

Supported devices

  • Shelly Plus 1
  • Shelly Plus 1PM
  • Shelly Plus 2PM
  • Shelly Plus I4
  • Shelly Pro 1
  • Shelly Pro 1PM
  • Shelly Pro 2
  • Shelly Pro 2PM
  • Shelly Pro 4PM

How to debug

To debug the script add this to your logger configuration:

# configuration.yaml file
logger:
  default: warning
  logs:
    homeassistant.components.python_script: debug
    homeassistant.components.automation: info

Supported platforms

  • binary sensor
  • button
  • light
  • sensor
  • switch

Supported features

  • the device name is taken from the device configuration
  • the relay name is taken from the device configuration
  • the relay consumption type is taken from the device configuration
  • if the input type is set to button, device automation triggers are available
  • custom MQTT prefixes are supported

Script arguments

key optional type default description
discovery_prefix True string homeassistant MQTT discovery prefix
qos True integer 0 MQTT QoS, you can use 0, 1 or 2

Minimal configuration

# configuration.yaml file
python_script:

# automations.yaml file
- id: shellies_announce_gen2
  alias: "Shellies Announce Gen2"
  trigger:
    - platform: homeassistant
      event: start
  variables:
    device_info_payload:  "{{ {'id': 1, 'src':'shellies_discovery', 'method':'Shelly.GetConfig'} | to_json }}"
  action:
    - service: mqtt.publish
      data:
        topic: "shellypro4pm-aabbccddeeff/rpc"  # shellypro4pm-aabbccddeeff is a device ID
        payload: "{{ device_info_payload }}"
    - service: mqtt.publish
      data:
        topic: "shellyplus1pm-112233445566/rpc"  # shellyplus1pm-112233445566 is a device ID
        payload: "{{ device_info_payload }}"

- id: shellies_discovery_gen2
  alias: "Shellies Discovery Gen2"
  mode: queued
  max: 999
  trigger:
  - platform: mqtt
    topic: shellies_discovery/rpc
  action:
  - service: python_script.shellies_discovery_gen2
    data:
      id: "{{ trigger.payload_json.src }}"
      device_config: "{{ trigger.payload_json.result }}"

- id: shellies_status_gen2
  alias: "Shellies Status Gen2"
  trigger:
  - platform: time_pattern
    minutes: "/15"
  - platform: homeassistant
    event: start
  action:
  - service: mqtt.publish
    data:
      topic: shellypro4pm-aabbccddeeff/rpc
      payload: "{{ {'id': 1, 'src':'shellypro4pm-aabbccddeeff/status', 'method':'Shelly.GetStatus'} | to_json }}"  # shellypro4pm-aabbccddeeff is a device ID
  - service: mqtt.publish
    data:
      topic: shellyplus1pm-112233445566/rpc
      payload: "{{ {'id': 1, 'src':'shellyplus1pm-112233445566/status', 'method':'Shelly.GetStatus'} | to_json }}"  # shellyplus1pm-112233445566 is a device ID

About

Script that adds MQTT discovery support for Shellies Gen2 devices

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%