MQTT Switch config puzzle

I’m running into an issue.
I got some of those cheap 433 switches that have the remote. A set of 3 switches intended to go on and off with a remote. The remote has 6 switches and the switches take 1 code for on and one code for off for each of the 3 switches. Smart Electrician 3-pack special from Menards. Sonoff RF Bridge sees the codes and can send them, so I have 12 codes for 6 switches stored in my bridge for Christmas Lights.

I know now that I can fire these using the MQTT equiv of:
Backlog RfSync 9600; RfLow 290; RfHigh 890; RfCode #472086
but these 6 switches are the packed away in the attic Christmas ones so I don’t now the codes to write the backlog stuff. I set them up like this and they work now using stored commands:

 SCRIPT:
    #####################################################
    # MQTT Plugs Set B                                  #
    #####################################################
    #######
    ## 1 ##
    #######
    switchb1_on:
      alias: Switch B1 On
      sequence:
        - service: mqtt.publish
          data:
            topic: RF_Bridge/cmnd/RfKey3
            payload: 0

    switchb1_off:
      alias: Switch B1 Off
      sequence:
        - service: mqtt.publish
          data:
            topic: RF_Bridge/cmnd/RfKey4
            payload: 0

Then I would just call up the script via an automation to turn them on/off. The problem is there is no switch entity, so turning it off from Google home speaker you have to say something like ‘hey google, turn on Switch B1 Off’. This I have found has high number of negative WAF points, so trying to fix. WAF is not good because she had to turn something on to turn the lights off and that is apparently just wrong, so I am told…

Since the command topic(s) are for on:
RF_Bridge/cmnd/RfKey4 (with a blank payload)
and off the topic is:
RF_Bridge/cmnd/RfKey5 (with a blank payload)

I can’t set an MQTT switch, It needs 1 command string and can be 2 payloads.
Has anyone found a way to make an MQTT switch out of this situation?

If I am going to re-do a lot of coding here I will next Fall (when we get them back out of the attic) just make an MQTT Switch send the straight up backlog thing and skip the stored keys thing altogether. That would be 1 command and 2 payloads. If someone has a way to do a MQTT switch now I’l just change to that now when I can remember to do it.

You’ve already got the scripts to do what you need to control the switches so what I would do is create a few input_booleans (1 for each on/off set of switches) then use the input boolean to trigger an automation to run the scripts for on or off to follow the state of the input boolean.

It sounds more complicated than what it is.

for example:

input_boolean:
  b_1:
    name: B1

automation:
  - alias: Control Plug B1
    trigger:
      platform: state
      entity_id: input_boolean.b_1
    action:
      service: script.turn_on
      data_template: 
        entity_id: >
          {% if trigger.to_state == 'on' %}
            script.switchb1_on
          {% else %}
            script.switchb1_off
          {% endif %}

Obviously you can play with the boolean name to up the WAF but with this you can just say “turn on B1” or “turn off B1” and it should work. And if you want to control it from the front end there will be a “switch” called B1 to turn or off with the same result.

I think…

1 Like

If you need a switch rather than an input_boolean, you can use a template switch to call your scripts.

1 Like

I’m going to tackle a template switch today. Thanks for the ideas!

I ended up going with finity’s automation idea with a slight tweak to get the automation to work.

#####################################################
# Christmas Plugs set B                             #
#####################################################

- alias: Control Plug B1
  trigger:
    platform: state
    entity_id: input_boolean.b_1
  action:
    service: script.turn_on
    data_template: 
      entity_id: >
        {% if is_state('input_boolean.b_1', 'on') %}
          script.switchb1_on
        {% else %}
          script.switchb1_off
        {% endif %}

The template switch started getting weird again when it wanted to know the state, so I stuck with the Input_Boolean and the Automation.

I am building another same format switch called Christmas that is grouping the switches, so She will be able to turn Christmas on and off and get my WAF in the positive zone again…
Thanks for your help!