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
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?
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.
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.
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…
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.