State of shelly switch not recognised after Homeassistant restart

Hi,

I have two shelly plus 1 switches, that each control a lamp, configured in home assistant via MQTT (see below for my configuration). This works fine, I can turn my lights off and on via Homeassistant and do some automation with it via nodeRED, the only problem I’m facing is that if I restart homeassistant while at least one of the lights are on homeassistant doesn’t pick up the active state and/or simply defaults to an “off” state (physically lights remain “on”). This causes problems, as homeassistant can’t operate the shelly switches anymore until I turned them off either via the wall switch or via the web interface of the shelly switch. Are there any solutions to fix this, so that when I restart homeassistant he actually looks at the current state of the shelly switch instead of like just assuming it is “off”

configuration.yaml

switch:
    - platform: mqtt
      name: "Kitchen - lamp left"
      qos: 2
      state_topic: "shellyplus1-a8032abc5c0c/events/rpc"
      command_topic: "shellyplus1-a8032abc5c0c/rpc"
      payload_on: '{"method":"Switch.Set", "params":{"id":0,"on":true}}'
      payload_off: '{"method":"Switch.Set", "params":{"id":0,"on":false}}'
      state_on: true
      state_off: false
      value_template: "{{ value_json['params']['switch:0']['output'] }}"

log lines after restart homeassitant:

2021-10-03 11:40:47 INFO (MainThread) [homeassistant.components.mqtt] Data in your configuration entry is going to override your configuration.yaml: {'birth_message': {'topic': 'homeassistant/status', 'payload': 'online', 'qos': 0, 'retain': False}, 'port': 1883, 'username': '****', 'password': '********', 'discovery': True, 'will_message': {'topic': 'homeassistant/status', 'payload': 'offline', 'qos': 0, 'retain': False}, 'broker': '<ip>'}

2021-10-03 11:40:48 INFO (Thread-3) [homeassistant.components.mqtt] Connected to MQTT server <ip>:1883 (0)

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to homeassistant/+/+/config, mid: 1

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to homeassistant/+/+/+/config, mid: 2

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to tasmota/discovery/#, mid: 3

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellyplus1-a8032abc5c0c/status/switch:0, mid: 4

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellyplus1-a8032abdfc0c/status/switch:0, mid: 5

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellyplus1-a8032abc5c0c/status/switch:0, mid: 6

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellyplus1-a8032abdfc0c/status/switch:0, mid: 7

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellyplus1-a8032abc5c0c/events/rpc, mid: 8

2021-10-03 11:40:48 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to shellyplus1-a8032abdfc0c/events/rpc, mid: 9

Hu cvm, did you try “optimistic: true” to get an assumed state?

Can you tell the Shelly to post its status to mqtt with the persistent flag?

Then homeassistant will gather the state when it restarts from your MQTT broker

This did indeed seem to do the trick! That then in combination with telling nodeRED to watch the state of the switch itself instead of the binary sensor state (as I cannot pass the optimistic flag to the binary sensor). Quick question: Does using this optimistic flag hold downfalls? Are there cases where this flag could give me disadvantages or whatever…?

Thank you already for this solution!

This would indeed be the best solution I think, but unfortunately this can’t be done. I think it is possible with gen 1 shelly devices, but not (yet) with gen 2 (and I’m using gen2)

You can try this code, as well. I believe it works.

- platform: mqtt
  name: "Kitchen - lamp left"
  state_topic: "shellyplus1-a8032abc5c0c/status/switch:0"
  command_topic: "shellyplus1-a8032abc5c0c/rpc"
  payload_on: "{'method':'Switch.Set', 'params':{'id':0,'on':true}}"
  payload_off: "{'method':'Switch.Set', 'params':{'id':0,'on':false}}"
  state_on: true
  state_off: false
  value_template: '{{ value_json["output"] }}'
  optimistic: false
  retain: false

It does not, I’m afraid. Well, to be perfectly clear the code itself works, I can turn my lights on/off just as good, as with my code above, but the original problem is still there. Seems that I need to set the optimistic flag to true as long as shelly doesn’t change their software to allow the setting of the persistent flag (but that’s just a hypothesis).

Hi
Were you able to also set the absorption sensor on the Shelly 1PM Plus?

I do not have the PM version of the Shelly 1 plus. So no I do not have experience with it

thanks anyway

Can u give more details?

In shelly 1pm (not plus), to get the absorption I used this code:

sensor:
  - platform: mqtt
    name: "Shelly 1PM Assorbimento"
    state_topic: "shellies/NOME_SHELLY/relay/0/power"
    availability_topic: "shellies/NOME_SHELLY/online"
    payload_available: "true"
    payload_not_available: "false"
    unit_of_measurement: "Watt"

I can’t find the equivalent code for the 1pm PLUS shelly

It should be like the code below:

- platform: mqtt
  name: "Kitchen - lamp left"
  state_topic: "shellyplus1pm-a8032abc5c0c/status/switch:0"
  value_template: '{{ value_json["apower"] }}'
  unit_of_measurement: "W"
1 Like

Thank you very much, it’s perfect

Checking the HA register, I realized that there are many warnings regarding the switch:

WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'output' when rendering '{{ value_json['params']['switch:0']['output'] }}'

Do you know how to solve?

I don’t know for you, but for me I had issues with the switch when I had my quotes for the value_template like this: ’ " " '. It only worked when " ’ ’ " was like this

I don’t know if you realised that after enabling MQTT we have to reboot the device and the make sure we activate “Generic status update over MQTT” under MQTT section (refresh the page to show this option, if needed).

Hello. Yes, the option is active and everything seems to work correctly but the warning I posted above appears in the log. It seems that the problem is similar to the one described in this topic:
https://community.home-assistant.io/t/mqtt-dict-object-has-no-attribute/297330
but I don’t understand how to fix it.
Questa è la configurazione dell’interrutore, come quella di cvm

  - platform: mqtt
    name: "S1PM_Lavatrice_interruttore"
    qos: 2
    state_topic: "S1PM_Lavatrice/events/rpc"
    command_topic: "S1PM_Lavatrice/rpc"
    payload_on: '{"method":"Switch.Set", "params":{"id":0,"on":true}}'
    payload_off: '{"method":"Switch.Set", "params":{"id":0,"on":false}}'
    state_on: true
    state_off: false
    value_template: "{{ value_json['params']['switch:0']['output'] }}"
    optimistic: true

Have you checked your MQTT content with an MQTT explorer of some kind? Maybe the PM outputs differently towards MQTT? (would be weird I guess, but who knows…)