Still learning python and whatnot need help wrapping my head

So I have this piston from SmartThings. But as I’m looking to start moving devices off of smartthings and over to HASS.io I decided that the first thing I would do is offload all of my current automations. There are some things that I’m just going to have to leave SmartThings around to handle for me such as having my DLNA speakers to TTS. But for other things I don’t see why I can’t move these automations in house.

I’ve done some scripting with appdaemon for super simple things but I think that for something like this I have seen combinations where automation triggers a python script. For this automation and some of my others such as controlling my whole house fan and thermostat settings since I have lots of conditions for those things. Besides learning python here at home will help me with my career as well.

So this is what I started writing last night

import datetime
import urllib2
import time

#camera stuff

#Motion Sensors
WestSideMotion = ["sensor.west_side_motion"]
EastSideMotion = ["sensor.east_side_motion"]
LivingRoomMotion = ["sensor.living_room_motion"]
ChickenCoopMotion = ["sensor.chicken_coop_motion"]
#Door Sensors
BackDoorSensor = ["sensor.back_door_contact"]


if BackDoorSensor == 'open'
  if 

So for this one I have a condition that if my back door opens and its night time then I want to make a URL call to have ZoneMinder activate a couple of cameras for 1 min and then got back to normal mode.

I think once I have a general idea of how to do automation like this then I can move forward with some of my my more complex ones.

Thank you all for your help with this.

BTW SmartThings people this is not a knock at this wonderful product. My WAF (Wife Approval Factor) needs to stay in the positive range and when the internet goes down I still need to be able to turn on/off some lights. Yes ST is working on moving more things to be controlled by the hub but the webcore I don’t think will ever run locally.

EDIT: I am hoping that I can just throw all of this into one script with multiple IF’s and then have the automation side monitor the sensors I need it to monitor to call this script. I’m sure I’m over complicating this which is about normal for me I think about things way too deep when the answer is just staring right at me.

Well, first off, the python component runs in a sandbox and cannot import modules like urllib2 to make requests outside, you just get to work with the internal home assistant data.
So if you wanted to interact with ZoneMinder the better way would be to setup the ZoneMinder component in HA and interact with that in the python script (and then the component makes the calls on your behalf), or if the built-in zoneminder component doesn’t have all the features you want, add some RESTful Switch’s to interact with ZoneMinder and then call those in your python script (I don’t use ZoneMinder, so I can’t really speak to the feature set of the built-in component).

You should also look at breaking this script up a bit, rather than having having one big monolithic one to do everything, that’s a bit of an anti-pattern to asynchronous systems. Having things broken apart lets HA check on other things while it’s waiting for components to return data, otherwise your script could miss a change in state at the bottom of your code while it’s still working on the top part. I would look at handling at least some of the base level triggering and conditional logic with a normal automation and then using the python script to handle the logic of which cameras get called for that event.

1 Like

Yeah I got it all written up last night and when I went to test it got errors on the import lines. Thats when I found out what you stated that I can’t use import here. I’m currently working on moving it over to appdaemon since I can do imports there. I just need to get some better understanding of the def lines of appdaemon because this straight python is just really simple to me where with the python and the functions requires a little more for me to wrap my head around but I’m getting there.

EDIT: I do like your point though on breaking this apart into multiple scripts. I suppose that would also make it easier to troubleshoot it as well.

So moved it over to appdaemon

import appdaemon.appapi as appapi
import urllib
import time


class cameras(appapi.AppDaemon):

# camera stuff

# Motion Sensors
  WestSideMotion = ["sensor.west_side_motion"]
  EastSideMotion = ["sensor.east_side_motion"]
  LivingRoomMotion = ["sensor.living_room_motion"]
  ChickenCoopMotion = ["sensor.chicken_coop_motion"]
# Door Sensors
  BackDoorSensor = ["sensor.back_door_contact"]
  DiningRoomWindow = ["sensor.dining_room_window"]
  FrontDoor = ["sensor.front_door_contact"]
# URL
  BaseURL = ["http://192.168.7.1:80/zm/api/monitors/alarm/"]


def initialize(self):
# Back Door and Diningroom Window Contact Sensors
  self.listen_state(back_door_cameras)
  if self.get_state("BackDoorSensor") == 'open' or self.get_state("DiningRoomWindow") == 'open' :
    if self.now_is_between("sunset - 00:30:00","sunrise - 00:30:00") or self.nonone_home() :
      content = urllib.urlopen('http://192.168.7.1:80/zm/api/monitors/alarm/id:1/command:on.json').read()
      content = urllib.urlopen('http://192.168.7.1:80/zm/api/monitors/alarm/id:10/command:on.json').read()
      time.sleep(60)
      content = urllib.urlopen('http://192.168.7.1:80/zm/api/monitors/alarm/id:1/command:off.json').read()
      content = urllib.urlopen('http://192.168.7.1:80/zm/api/monitors/alarm/id:10/command:off.json').read()

# Chicken Coop Motion Sensor
  self.listen_state(chicken_coop_motion)
  if self.get_state("ChickenCoopMotion") == 'active' :
    if self.now_is_between("sunset - 00:30:00","sunrise - 00:30:00") :
      content = urllib.urlopen('http://192.168.7.1:80/zm/api/monitors/alarm/id:10/command:on.json').read() 
    if self.get_state("ChickenCoopMotion") == 'inactive' :
      content = urllib.urlopen('http://192.168.7.1:80/zm/api/monitors/alarm/id:10/command:off.json').read()

I expect it to fail fantastically when I test it out but its passing the sanity check of appdaemon. I’m not passing in any arguments from appdaemon to this so we will see what it does and if it doesn’t fire off then back to the drawing board to make more adjustments to it.

And now that its passed sanity checks with appdaemon here is the end result

import appdaemon.appapi as appapi
import urllib
import time


class cameras(appapi.AppDaemon):

  def initialize(self):

# camera stuff

# Motion Sensors
    WestSideMotion = ["sensor.west_side_motion"]
    EastSideMotion = ["sensor.east_side_motion"]
    LivingRoomMotion = ["sensor.living_room_motion"]
    ChickenCoopMotion = ["sensor.chicken_coop_motion"]
# Door Sensors
    BackDoorSensor = ["sensor.back_door_contact"]
    DiningRoomWindow = ["sensor.dining_room_window"]
    FrontDoor = ["sensor.front_door_contact"]
# URL
    BaseURL = ["http://192.168.7.1:80/zm/api/monitors/alarm"]



# Back Door and Diningroom Window Contact Sensors
    self.listen_state(back_door_cameras)
    if self.get_state("BackDoorSensor") == 'open' or self.get_state("DiningRoomWindow") == 'open' :
      if self.now_is_between("sunset - 00:30:00","sunrise - 00:30:00") or self.nonone_home() :
        content = urllib.urlopen('"BaseURL"/id:1/command:on.json').read()
        content = urllib.urlopen('"BaseURL"id:10/command:on.json').read()
        time.sleep(60)
        content = urllib.urlopen('"BaseURL"/id:1/command:off.json').read()
        content = urllib.urlopen('"BaseURL"/id:10/command:off.json').read()

# Chicken Coop Motion Sensor
    self.listen_state(chicken_coop_motion)
    if self.get_state("ChickenCoopMotion") == 'active' :
      if self.now_is_between("sunset + 00:30:00","sunrise - 00:30:00") :
        content = urllib.urlopen('"BaseURL"/id:10/command:on.json').read() 
      if self.get_state("ChickenCoopMotion") == 'inactive' :
        content = urllib.urlopen('"BaseURL"/id:10/command:off.json').read()

I’m torn about using kargs and splitting this up into several apps and just doing one big script. I like the idea of one big script for a learning base because as I continue with my automation migration from ST over to HA I do have some scripts that are rather large due to the various conditions that I have. Especially my climate control.