Temlate alternative switch

Hello,

I have a Dingtian relay. It has a LAN and also a WIFI connection. Works with MQTT, but the device ID is different on wifi and on LAN (let’s call them DTWifi and DTLAN). Mainly I’d like to use it with LAN, but if there is any problem (e.g. cutting LAN cable), I’d like to use with wifi. So there are two switches under HA: one for LAN, one for Wifi. Both of them switch the same relay. Can I merge them into one entity? I mean if the LAN switch is available, it will be the LAN switch, if the LAN switch is “unavailable” it will be the wifi based switch.

The MQTT part for the LAN switch currently is:

  - platform: mqtt
    unique_id: kert-kiskapu-nyitas
    name: "Kerti kiskapu nyitás"
    command_topic: "/dingtian/relay4966/in/control"
    state_topic: "/dingtian/relay4966/out/r1"
    availability:
      - topic: "/dingtian/relay4966/out/lwt_availability"
        payload_available: "online"
        payload_not_available: "offline"
    payload_on: '{"type":"JOGGING","idx":"1","status":"ON","time":"5","pass":"0"}'
    state_on: "ON"
    state_off: "OFF"
1 Like

Thinking about this another way, you could create an automation that re-sends states from the two relay ID state_topics to a third topic that you set as the state_topic for your switch. The same automation could also re-send commands to both relay ID topics.

Sory, I don’t understand this. My aim to see one switch entity, but behind tis there is a default switch, or if this defaoult switch is not available, it should be another switch.

Set up a switch

switch:
  - platform: mqtt
    name: 'Your Template Switch'
    state_topic: 'template_switch/state'
    command_topic: 'template_switch/command'
    payload_on: '{"type":"JOGGING","idx":"1","status":"ON","time":"5","pass":"0"}'
    state_on: 'ON'
    state_off: 'OFF'

The below automation will resend commands from your template switch to BOTH the wifi and LAN versions of your Dingtain switch

- id: '1'
  alias: Template to Dingtian MQTT Switch Forwarding
  trigger:
  - platform: mqtt
    topic: template_switch/command
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: /dingtian/relay4966/in/control
      payload: '{{trigger.payload}}'
  - service: mqtt.publish
    data:
      topic: /dingtian/relayXXXX/in/control
      payload: '{{trigger.payload}}'
  mode: single

You then need another automation ( or you could add to the above ) to get states from either version of your Dingtain switch back to the template switch

- id: '2'
  alias: Digitian to Template MQTT Switch Forwarding
  trigger:
  - platform: mqtt
    topic: /dingtian/relay4966/out/r1
  - platform: mqtt
    topic: /dingtian/relayXXXX/out/r1
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: template_switch/state
      payload: '{{trigger.payload}}'
  mode: single

Forwarding the availability would be the same as above