Broadlink S1C Kit sensors in HA using python and MQTT

I found a very simple way to keep my sensors state updated after a system restart,
My initial problem wasn’t making the code update the states, the problem was the addon was initiated before the mqtt sensors or the mosquito broker, therefore there were no sensors to update.

So, I just made it wait for 3 minutes for the mqtt sensors and mosquito broker to finish loading, then I update the sensors state once, and then I start the while loop updating the state for each change.

import broadlink
import time, os, datetime

mosquitto_address = "x.x.x.x"
mosquitto_port = "xxxx"
mosquitto_user = "x"
mosquitto_password = "x"
broadlink_s1c_ip = "x.x.x.x"
broadlink_s1c_mac = "xxxxxxxxxxxx"

devices = broadlink.S1C(host=(broadlink_s1c_ip, 80), mac=bytearray.fromhex(broadlink_s1c_mac))
devices.auth()


def checkSensor(v_type, v_name, v_status):
    if v_type == "Door Sensor" and v_status in ("0", "128"):
        sendToMosquito(v_name, "Closed")
    elif v_type == "Door Sensor" and v_status in ("16", "144"):
        sendToMosquito(v_name, "Open")
    elif v_type == "Door Sensor" and v_status == "48":
        sendToMosquito(v_name, "Tampered")
    elif v_type == "Motion Sensor" and v_status in ("0", "128"):
        sendToMosquito(v_name, "No_motion")
    elif v_type == "Motion Sensor" and v_status == "16":
        sendToMosquito(v_name, "Motion_Detected")
    elif v_type == "Motion Sensor" and v_status == "32":
        sendToMosquito(v_name, "Tampered")
    return


def sendToMosquito(v_deviceName, v_payload):
    os.system("mosquitto_pub -h " + mosquitto_address + " -p " + mosquitto_port + " -t 'sensors/s1c/" + v_deviceName + "' -u " + mosquitto_user + " -P " + mosquitto_password + " -m " + v_payload)
    return

time.sleep(180)
sens = devices.get_sensors_status()
old = sens

for k, se in enumerate(sens['sensors']):
    checkSensor(se['type'], ((se['name']).replace(" ", "_")).lower(), str(se['status']))

while 1:
    try:
        sens = devices.get_sensors_status()
        for i, se in enumerate(sens['sensors']):
            if se['status'] != old['sensors'][i]['status']:
                checkSensor(se['type'], ((se['name']).replace(" ", "_")).lower(), str(se['status']))
                old = sens
    except:
        continue
1 Like

Gabriel, what version of HA are you running? Could it be that a recent update made the script stop working? I’m on the latest version 0.56.2.
Seems the script sill works for TomerFi. What HA version are you running?

I’m actually still running version 0.55.
I plan on upgrading to 0.56.2 this weekend.

Things are all good now. Thank you.

Could you tell us what you did, Gabriel? I have the same problem as you and can’t figure it out. Thanks

My motion sensors suddenly stopped working while the door sensors still works. Do your sensors still work correctly?

The motion sensor is working but the door sensor stopped.

In my case, the broadlink app shows the change but the Home assistant doesn’t. I also saw there’s an update for the S1 firmware, but I’m not sure it’s related to my (our) problem

I think that the real problem is with the script running inside the container. Maybe someone using this script on Hassbian will tell us if he had this kind of behavior. Or hopefully the developers of the HomeAssistant will try to add the s1c as a component.

1 Like

@gabriel.colceriu, ok, I changed your the line you use to get the time with:

now = time.strftime("%d %b %Y %H:%M:%S", time.gmtime())

and now, don’t know why, my sensors work again.
Maybe it’s just a matter of timestamps in the mqtt database. I’ve got no idea.

I will try this tonight. Thank you.

1 Like

In the meantime i installed MQTTLens(Chrome Addon) and made a connection to my mqtt to watch the “#” topic and see all the clients that connect to my mqtt and publish on any topics. There i saw that the information is coming from the script but the HA is not showing it. Will debug this later this evening.

2 Likes

After some restarts of the HA the sensors started to work again for some hours. But than again the script published the info on the MQTT Topics but the HA is not showing any change. In the logs since last night i see a bunch of broadlink errors trying to update the state of another sensor(mp1). I don’t know if this is related but i personally think that it may have something to do with it. Anyone else had this kind of behavior? My next goal is to switch to Hassbian and see if the situation still persists.

I just found out that inside the broadlink motion sensors there’s a jumper, and moving it from “normal” to “full” makes the sensor more responsive… probably this is also going to drain the batteries a little more quickly, but… I’ll let you know…

Service does not start automatically.
Show error. Anyone know the reason?

sudo systemctl status broadlink-s1c.service

● broadlink-s1c.service - Broadlink S1C
   Loaded: loaded (/etc/systemd/system/broadlink-s1c.service; enabled)
   Active: failed (Result: exit-code) since Ter 2017-11-21 19:11:54 -02; 1 day 23h ago
  Process: 514 ExecStart=/usr/bin/python /home/pi/s1c/s1c.py (code=exited, status=1/FAILURE)
 Main PID: 514 (code=exited, status=1/FAILURE)

Nov 21 19:11:54 pi01 python[514]: Traceback (most recent call last):
Nov 21 19:11:54 pi01 python[514]: File "/home/pi/s1c/s1c.py", line 12, in <module>
Nov 21 19:11:54 pi01 python[514]: devices.auth()
Nov 21 19:11:54 pi01 python[514]: File "/home/pi/s1c/broadlink.py", line 206, in auth
Nov 21 19:11:54 pi01 python[514]: response = self.send_packet(0x65, payload)
Nov 21 19:11:54 pi01 python[514]: File "/home/pi/s1c/broadlink.py", line 280, in send_packet
Nov 21 19:11:54 pi01 python[514]: self.cs.sendto(packet, self.host)
Nov 21 19:11:54 pi01 python[514]: socket.error: [Errno 101] Network is unreachable
Nov 21 19:11:54 pi01 systemd[1]: broadlink-s1c.service: main process exited, code=exited, status=1/FAILURE
Nov 21 19:11:54 pi01 systemd[1]: Unit broadlink-s1c.service entered failed state.

MY broadlink-s1c.service

[Unit]
Description=Broadlink S1C
Wants=network.target
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/python /home/pi/s1c/s1c.py

[Install]
WantedBy=multi-user.target

Do you face problem with \bin\bash^M?

I tried and keep having problem starting the repository.

I have follow the step everything seem to work fine.
I follow NightRanger step. I use the s1c.py from TomerFi as I want to have more than 1 door sensor.
I use systemd as recommend from NightRanger as well. Check the status working fine with the service.
**** In HA the sensor state is “unknown” on both motion and door sensor.***

Please advice!!!

Do I need to use the binary_sensor instead of sensor?

Furthermore, after trying to run via ssh. I got this. What do I need to do?
Tue Dec 19 20:25:54 2017: Motion Detected: 16
Connection Refused: not authorised.
Error: The connection was refused.

Got it work. Have to allow_anonymous true. Although, already put the user pass for mosquitto.

Is there any chance to add the keyfob inside HA as well. So we can put the alarm / disarm / part alarm.

1 Like