Successfully installed hass.io on rpi2, would it be possible to

You can trigger the automation from the pop up when you click the automation card.

Ok so I’ve got the trigger working and publishing the message to mqtt. mqtt is listening and receives it. The code for mqttReceive.py is:

import paho.mqtt.client as mqtt
from failedPythonBLE import turn1ON,turn1OFF
MQTT_SERVER = "192.168.1.176"
MQTT_PATH = "test_channel"
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe(MQTT_PATH)
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
    if str(msg.payload)=="holacaracola":
        print "letsGoBaby!"
        turn1ON.letsgobaby()
    else:
        print "naranjas"
        turn1OFF.letsgobaby()
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(MQTT_SERVER, 1883, 60)
    client.loop_forever()

and the python file is indeed called because I have this as its code in turn1ON.py:

#!/usr/bin/env python
import bluepy.btle as btle

print ("outside")
def letsgobaby():
  print ("turning1on")
  #production tsrb430
  p = btle.Peripheral("00:0E:0B:00:65:19", "random")

  # GET SERVICES
  services=p.getServices()
  for service in services:
     print service

  # NORMAL get to write to ...
  s = p.getServiceByUUID("0000ffe0-0000-1000-8000-00808f9b34cb")
  c = s.getCharacteristics()[0]
  c.write("e")
  p.disconnect()
  
 if __name__ == "__main__":
       letsgobaby()

And the console after running mqttReceive.py logs:

outside
Connected with result code 0
turning1on
test_channel holacaracola
letsGoBaby!

But I need to configure the switch/automation to send the right command when toggled back off. Im confused as to whether I should do that by modifying the Automation itself, or the switch itself or both? Because currently when I toggle the switch back to OFF, the code if-else’s else block doesnt get called as can be seen from the console result. Im guessing that I need to add the bit where toggling from ON to OFF must also do someting.

So do I need to create another automation for trigger from OFF to ON with the respective payload? I dunno, it just seems repetitive considering I could have many more components to control.