Can I control non MQTT entities with MQTT?

Hi there,

Would love some advice on the best way to achieve control/state for devices that aren’t natively MQTT like Arduino. I’ve deployed an Arduino board in my test environment which was too easy but I want to be able to now control the switch using MQTT. What is the best way to do that?

arduino:
  port: /dev/ttyACM0
  
switch:
  platform: arduino
  pins:
    11:
      name: Fan Office
      type: digital
    12:
      name: Light Desk
      type: digital
      default: on
      negate: true
    13:
      name: Internal LED
      type: digital

I know the answer will be obvious but brain is running in slo-mo at the moment. :unamused: I’m assuming I could use a simple script to publish to a topic when a device changes state and vice versa?

1 Like

Automation -> MQTT trigger

Thanks, of course. Can the ‘action’ be to publish a topic as well?

Yes. Call the MQTT publish service in your action: https://home-assistant.io/components/mqtt/#publish-service

Thank you. Is there any chance you could show me what that would look like in the example below?

- alias: 'Update Available Notifications'
  trigger:
    platform: state
    entity_id: updater.updater
  action:
    service: mqtt
action:
  service: mqtt.publish
  data:
    topic: 'your/topic'
    payload: 'your payload'
2 Likes

That is super helpful! Thank you so much. Think I can integrate MQTT Dash nicely with every component now, regardless of its protocol.

For those that want to do the same, here is my automation code hiding inside a folder called ‘auto’ and included using the automation: !include_dir_merge_list auto method.

### Publish Wemo state via MQTT ###
- alias: Wemo MQTT on
  trigger:  
    platform: state
    entity_id: switch.wemo_switch
    state: 'on'
  action:
    service: mqtt.publish
    data:
      topic: 'mqtt/switch/wemo'
      payload: 'on'
      
- alias: Wemo MQTT off
  trigger:  
    platform: state
    entity_id: switch.wemo_switch
    state: 'off'
  action:
    service: mqtt.publish
    data:
      topic: 'mqtt/switch/wemo'
      payload: 'off'

I now have a light icon on MQTT Dash that changes state when the Wemo does. Now to get the icon to turn it on and off.

OK, trying to get control but not working. Anyone see a glitch?

    ## Wemo control via MQTT ##
    - alias: Control Wemo via MQTT
      trigger:  
        platform: mqtt
        topic: mqtt/switch/wemo/control
        # Optional
        payload: 'on'
      action:
        service: switch.turn_on
        data:
        entity_id: switch.wemo_switch

I’m publishing the payload ‘on’ to mqtt/switch/wemo/control via MQTT.fx and the switch is not turning on. Also, when I add this yaml file to the ‘auto’ folder, the previous automation to track the status of the wemo also stops working. Can see anything in the log.

1 Like

Peter, still new to HASS, but have you tried to remove the data: field so its like this?

action:
service: switch.turn_on
entity_id: switch.wemo_switch

Yep, surely have.

OK. All working now.

- alias: Arduino on via MQTT
  trigger:  
    platform: mqtt
    topic: switch/arduino/control
    # Optional
    payload: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.internal_led
      
- alias: Arduino off via MQTT
  trigger:  
    platform: mqtt
    topic: switch/arduino/control
    # Optional
    payload: 'off'
  action:
    service: switch.turn_off
    entity_id: switch.internal_led
    
- alias: Publish Arduino on
  trigger:  
    platform: state
    entity_id: switch.internal_led
    state: 'on'
  action:
    service: mqtt.publish
    data:
      topic: 'switch/arduino/state'
      payload: 'on'
      
- alias: Publish Arduino off
  trigger:  
    platform: state
    entity_id: switch.internal_led
    state: 'off'
  action:
    service: mqtt.publish
    data:
      topic: 'switch/arduino/state'
      payload: 'off'
1 Like

OK, one last bit of advice. How do I add QOS and retain?

- alias: Publish Orvibo off
  trigger:  
    platform: state
    entity_id: switch.kitchen_socket
    state: 'off'
  action:
    service: mqtt.publish
    data:
      topic: 'switch/orvibo/state'
      payload: 'off'

Disregard. Pulled some info from MQTT Switch and this now works a treat with MQTT Dash.

## Publish Orvibo Status to MQTT ## 
- alias: Publish Orvibo on
  trigger:  
    platform: state
    entity_id: switch.kitchen_socket
    state: 'on'
  action:
    service: mqtt.publish
    data:
      topic: 'switch/orvibo/state'
      payload: 'on'
      qos: 1
      retain: true
      
- alias: Publish Orvibo off
  trigger:  
    platform: state
    entity_id: switch.kitchen_socket
    state: 'off'
  action:
    service: mqtt.publish
    data:
      topic: 'switch/orvibo/state'
      payload: 'off'
      qos: 1
      retain: true
1 Like

I’m trying to achieve something similar by using a physical switch I made connected to the gpio pin on my pi. When the switch is pressed, it goes login HIGH (5V). I can’t for the life of me get it to work with the physical switch.
I’ve tried rpi_gpio with both binary_sensor and switch methods to no avail. This is the code I’ve tried for each:

- platform: rpi_gpio
  ports:
    21: Hallway Light Switch
  pull_mode: "DOWN"
  invert_logic: true

and this is my automation yaml config:

### Publish Hallway Light state via MQTT ###
- alias: Hallway Light MQTT on
  trigger:  
    platform: state
    entity_id: switch.hallway_light_switch
    state: 'on'
  action:
    service: mqtt.publish
  data:
    topic: 'left/hallwaylight'
    payload: '1'
  
- alias: Hallway Light MQTT off
  trigger:  
    platform: state
    entity_id: switch.hallway_light_switch
    state: 'off'
  action:
    service: mqtt.publish
    data:
    topic: 'left/hallwaylight'
    payload: '0'

Any ideas?

Have you got the GPIO working on its own? According to the Wiki, the GPIO pin 21 is a digital out, 20 is digital in. Have not played with Pi GPIO at all but I’d get that working first and then concentrate on MQTT second.

I’m pretty much just trying to get the GPIO working on its own at the moment. I’m pretty confident that the MQTT stuff is working properly so not even focusing on that. I’ll check out the DIGI in/out of the pins though. Cheers.

You also say the input is ‘active’ when you switch 5v to that GPIO pin so it is normally pulled low. The active state would therefore be high?

The Raspberry PI GPIO Binary Sensor page states:

invert_logic (Optional): If true, inverts the output logic to ACTIVE LOW. Default is false (ACTIVE HIGH).

Wouldn’t you therefore need to remove your invert_logic line?

Thanks for documenting your work on this- It saved me a bunch of hassle trying to do the same thing!

Thanks, this is exactly what i was looking for !