Shellies Discovery Script

First, many thanks for your work!

One question:
I use Node-RED for the automations.
Is it possible, to migrate the two yaml-automations to Node-RED?

Do you perhaps have a template for this?

Best regards
Gerd

Hi.

I have added few Shelly Plug S with mqtt. Plugs does not appear in the devices list and I see in the logs:

2022-05-03 18:05:22 ERROR (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] Error executing script: model_id value None is not valid, check script configuration
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)
  File "shellies_discovery.py", line 1639, in <module>
ValueError: model_id value None is not valid, check script configuration

Debug:

2022-05-03 18:39:47 INFO (SyncWorker_0) [homeassistant.components.python_script] Executing shellies_discovery.py: {'id': 'shellyplug-s-F17D38', 'mac': 'E868E7F17D38', 'fw_ver': '20190516-073020/master@ea1b23db', 'model': '', 'mode': '', 'host': '192.168.8.153'}

2022-05-03 18:39:47 ERROR (SyncWorker_0) [homeassistant.components.python_script.shellies_discovery.py] Error executing script: model_id value None is not valid, check script configuration

Traceback (most recent call last):

  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute

    exec(compiled.code, restricted_globals)

  File "shellies_discovery.py", line 1639, in <module>

ValueError: model_id value None is not valid, check script configuration

Any ideas?

model_id value None is not valid, check script configuration probably you have to update shellies_discovery automation.

Updated script, automations config… but no luck =[
Mby problem is that the shelly returns empty model and mode?
{'id': 'shellyplug-s-******', 'mac': '******', 'fw_ver': '20190516-073020/master@ea1b23db', 'model': '', 'mode': '', 'host': '192.168.8.157'}

Your device is using very old firmware, you have to update it.

Oh, yes. Updated via OTA link. Thx for your patience :wink:

Great that you heve solved the problem :+1:

Hey! Im triing to debug my wifi connection issues and while triing to display the rssi s ive noticed that my Shellie Door/Window Sensors also feature

  "wifi_sta": {
    "connected": true,
    "ssid": "",
    "ip": "192.168.2.8",
    "rssi": -57
  }

but those sensors are not integrated by your script under " Diagnostic" as “Wifi Signal” where i can i.e. find them for my Shellie 1s. Could you add that, or do i have to update something in the config of this script to do that?

Also related, i dont quite understand how qos=2 works here. could i define in the config of your script that i want all mqtt communication with all the entities it creates to use qos=2? im suffering from lossy transmission over wifi here and already implemented some higher level software doublechecking and acking, but thought “why is that not working/implemented directly on mqtt protocoll layer, or is it not active?”

Thanks a lot!

I have tried to switch my 3 Shelly Vintage bulbs from MQTT to CoIoT with unicast HA_IP:5683.
Restarted them all and double checked that I did it correctly.
I have also checked with MQTTExplorer that there are no MQTT topics from them, all fine.
But they are not picked up by the Shelly Integration, and no matter what I do they are reappearing as MQTT devices?! Restarted my HA several times but no success.
What step did I forgot?

What topic is this payload from?

If you set QoS to 2 in the script configuration then this value will be used to configure all entities, HA for these entities will use QoS = 2. You should also set up QoS in the device’s web panel.

Disable MQTT or add the device to the ignored list in the script configuration.

Topic is “shiellies/!!!Name of Shelly!!!/info” i get from MQTT Explorer. Here is what i believe is the full payload.:

info =
{"wifi_sta":{"connected":true,"ssid":"","ip":"192.168.2.8","rssi":-57},"cloud":{"enabled":false,"connected":false},"mqtt":{"connected":true},"time":"","unixtime":0,"serial":1,"has_update":false,"mac":"D8BFC01A87CB","cfg_changed_cnt":0,"actions_stats":{"skipped":0},"is_valid":true,"tmp":{"value":21.00,"units":"C","tC":21.00,"tF":69.80, "is_valid":true},"lux":{"value":131, "illumination": "twilight", "is_valid":true},"accel":{"tilt":-1, "vibration":-1},"sensor":{"state":"close", "is_valid":true},"bat":{"value":0,"voltage":0.01},"act_reasons":["sensor"],"sensor_error":0,"update":{"status":"unknown","has_update":false,"new_version":"","old_version":"20220209-093605/v1.11.8-g8c7bb8d"},"ram_total":51352,"ram_free":39872,"fs_size":233681,"fs_free":152859,"uptime":0}

That is apparently consistent with/same structure and topic where rssi is loacted in the shelly 1 hardware.

Hardware is “Shelly Door/Window 2”.: Shelly Door/Window 2 - Shelly Cloud

About qos = 2
Can you specify in more detail how i would define that in the script configuration for all and also how to define it for just some of my shellies? Im using the “Minimal configuration” and am unsure how to edit that. i couldnt find a good description what can be edited there and how. just the " Custom configuration example" from the github page.

I would try to:

# configuration.yaml file
python_script:

# automations.yaml file
- id: shellies_announce
  alias: 'Shellies Announce'
  trigger:
    - platform: homeassistant
      event: start
    - platform: time_pattern
      hours: "/1"  # Modifying this if you are using Shelly Motion can drain your device's battery quickly.
  action:
    service: mqtt.publish
    data:
      topic: shellies/command
      payload: announce

- id: 'shellies_discovery'
  alias: 'Shellies Discovery'
  mode: queued
  max: 999
  trigger:
    platform: mqtt
    topic: shellies/announce
  action:
    service: python_script.shellies_discovery
    data:
      id: '{{ trigger.payload_json.id }}'
      mac: '{{ trigger.payload_json.mac }}'
      fw_ver: '{{ trigger.payload_json.fw_ver }}'
      model: '{{ trigger.payload_json.model | default }}'
      mode: '{{ trigger.payload_json.mode | default }}'
      host: '{{ trigger.payload_json.ip }}'
      qos: 2

just add “qos: 2” at the end of id: ‘shellies_discovery’ action: service: data: in the " Minimal configuration" should make it so ALL shellies use qos = 2, correct?

To set qos=2 for just specific shellies i could nest it further down under the shellie name like this?:

- id: 'shellies_discovery'
  alias: 'Shellies Discovery'
  mode: queued
  max: 999
  trigger:
    platform: mqtt
    topic: shellies/announce
  action:
    service: python_script.shellies_discovery
    data:
      id: '{{ trigger.payload_json.id }}'
      mac: '{{ trigger.payload_json.mac }}'
      fw_ver: '{{ trigger.payload_json.fw_ver }}'
      model: '{{ trigger.payload_json.model | default }}'
      mode: '{{ trigger.payload_json.mode | default }}'
      host: '{{ trigger.payload_json.ip }}'
      discovery_prefix: 'hass'
      qos: 0
      shelly1-AABB9900:
        qos: 2

Will this set qos=0 as the default for all and set it to qos=2 for specific named shelly? or how does that work?

Also, as soon as i update automations.yaml and boot HA several times the existing entities will be updated with the new setting of qos=2 or do i need to delete the old entities to have the changes in automations.yaml take effect?

Thanks a lot for your GREAT script! makes it so much more hassle/error free and more consistent!

Is topic info sent periodically, for example every hour?

Correct

No, qos is a global option.

Topic seems to be sent on state changes. History shows updates in i.e. 1m,5m,25m,30s,5min.

Also the same payload is available for “Shelly H&T” ( Shelly H&T - Shelly Cloud ) under the same topic “info”.

Full H&T payload example.:

{
  "wifi_sta": {
    "connected": true,
    "ssid": "",
    "ip": "192.168.2.5",
    "rssi": -61
  },
  "cloud": {
    "enabled": false,
    "connected": false
  },
  "mqtt": {
    "connected": true
  },
  "time": "",
  "unixtime": 0,
  "serial": 1,
  "has_update": false,
  "mac": "4C7525349039",
  "cfg_changed_cnt": 0,
  "actions_stats": {
    "skipped": 0
  },
  "is_valid": true,
  "tmp": {
    "value": 34.5,
    "units": "C",
    "tC": 34.5,
    "tF": 94.1,
    "is_valid": true
  },
  "hum": {
    "value": 26,
    "is_valid": true
  },
  "bat": {
    "value": 100,
    "voltage": 3.01
  },
  "act_reasons": [
    "sensor"
  ],
  "connect_retries": 0,
  "sensor_error": 0,
  "update": {
    "status": "unknown",
    "has_update": false,
    "new_version": "",
    "old_version": "20220324-134040/v1.11.8-HT-fix-g60b9bd1"
  },
  "ram_total": 51520,
  "ram_free": 38456,
  "fs_size": 233681,
  "fs_free": 155871,
  "uptime": 0
}

So the topic is not send periodically but on restart/event. As far I remember DW/DW2 does not send info topic every hour if there was no event or reboot.

Yes i believe that is what is happening. I hope that still makes it possible to integrate it as a sensor. RSSI information would be usefull to me to debug my physical wifi sattelite placement and i wouldnt want to mess with adding RSSI manually via some config not reliant on your script because that only leeds to messy unorganised implementation in the long run i believe.

Thanks a lot for your time and have a nice weekend. Hope temperatures over at your place are similar to mine :).

If the device does not send info topic periodically, it makes no sense to create such a sensor. Lots of people use DW with doors/windows that don’t open for days or even weeks. Personally, I don’t have this device so I can’t check it.

Hmm. It doesnt send it periodically, because both the DW and the HT are battery powered - to save energy.

I dont understand why you think it doesnt make sense to have that information. On which timescale samplingrate or if that is periodic or not becomes only relevant depending on what you want to do with it. In my case the last value would be usefull. You i.e. also read out battery % charge levels on i.e. the DW devices. This value also doesnt update periodically but only if it changes. Also it would be consistent across shelly devices to also have this info. Its structured in the same way as in the shelly 1 and there you also read it out into a sensor.

or do you mean you wouldnt add it under “Sensors” but under “Diagnostics”?

For the HT the python code suggest to me that it was planned to add the RSSI Sensor as option.:

if model_id == MODEL_SHELLYHT_ID or dev_id_prefix == MODEL_SHELLYHT_PREFIX:
    model = MODEL_SHELLYHT
    sensors = {
        SENSOR_BATTERY: OPTIONS_SENSOR_BATTERY,
        SENSOR_HUMIDITY: OPTIONS_SENSOR_HUMIDITY,
        SENSOR_IP: OPTIONS_SENSOR_IP,
        SENSOR_RSSI: OPTIONS_SENSOR_RSSI,
        SENSOR_SSID: OPTIONS_SENSOR_SSID,
        SENSOR_TEMPERATURE_F: OPTIONS_SENSOR_TEMPERATURE_F,
        SENSOR_TEMPERATURE: OPTIONS_SENSOR_TEMPERATURE,
        SENSOR_UPTIME: OPTIONS_SENSOR_UPTIME,
    }
    binary_sensors = {
        SENSOR_CLOUD: OPTIONS_SENSOR_CLOUD,
        SENSOR_FIRMWARE_UPDATE: OPTIONS_SENSOR_FIRMWARE_UPDATE,
    }
    battery_powered = True

However the device that is created under HA doesnt contain that option then - i havent figured out yet why.