Occusim Error (Local variable 'i' referenced before assignment

I am getting this error when I try to use occusim. I have probably avoided it for awhile but I am trying to clean some things up here lately. I am on AppDaemon Version 3.0.1

Here is my error:

2018-09-13 14:53:44.505279 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1575, in init_object
    init()
  File "/conf/apps/occusim.py", line 32, in initialize
    self.create_events({})
  File "/conf/apps/occusim.py", line 77, in create_events
    self.log("step_{} end < start - ignoring end".format(i))
UnboundLocalError: local variable 'i' referenced before assignment

do you also have occusim adapted for AD 3.01?
and what do you have setup in your yaml?

Well I thought I had it adapted for AD 3.01

Occupancy Simulator:
  class: OccuSim
  module: occusim
  log: '1'
  notify: '1'
  enable: input_boolean.home,off
  dump_times: '1'
  reset_time: "02:00:00"
  
  step_morning_name: Morning
  step_morning_start: "06:45:00"
  step_morning_days: mon,tue,wed,thu,fri
  step_morning_end: "06:55:00"
  step_morning_on_1: switch.kitchen_switch
  step_morning_start_offset: "00:01:05"
  step_morning_end_offset: "00:05:00"
  step_morning_on_2: switch.dining_table_switch
  
  step_morning_off_name: Morning Off
  step_morning_off_start: "07:00:00"
  step_morning_off_days: mon,tue,wed,thu,fri
  step_morning_off_end: "07:10:00"
  step_morning_off_off_1: switch.kitchen_switch
  step_morning_off_start_offset: "00:00:01"
  step_morning_off_end_offset: "00:00:05"
  step_morning_off_off_2: switch.dining_table_switch
  
  step_dinner_start_name: Dinner Start
  step_dinner_start_start: "17:45:00"
  #step_dinner_start_end: "21:45:00"
  #step_evening_off_off_1: switch_living_room_outlet_switch
  
  step_dinner_end_name: Dinner End
  step_dinner_end_start: "20:05:00"
  
  step_evening_name: Evening
  step_evening_start: sunset - 00:10:00
  #step_evening_end: sunset - 00:20:00
  #step_evening_on_1: switch.living_room_outlet_switch
  
  step_room_off_name: Room Off
  step_room_off_start: "22:15:00"
  #step_room_off_end: '23:15:00'
  
  step_living_room_name: Living Room
  step_living_room_start: sunset - 00:10:00
  step_living_room_end: sunset + 00:10:00
  step_living_room__on_1: switch.living_room_outlet_switch
  
  step_living_room_off_name: Living Room Off
  step_living_room_off_start: "22:45:00"
  step_living_room_off_end: "23:15:00"
  step_living_room_off_off_1: switch.living_room_outlet_switch
  
  random_dinner_name: Dinner
  random_dinner_start: Dinner Start
  random_dinner_end: Dinner End
  random_dinner_minduration: "00:50:00"
  random_dinner_maxduration: "01:35:00"
  random_dinner_number: '1'
  random_dinner_on_1: switch.dining_table_switch
  random_dinner_on_2: switch.kitchen_switch
  random_dinner_off_1: switch.dining_table_switch
  random_dinner_off_2: switch.kitchen_switch
  
  random_kitchen_name: Kitchen
  random_kitchen_start: Evening
  random_front_room_end: Room Off
  random_kitchen_minduration: "00:05:00"
  random_kitchen_maxduration: "00:23:00"
  random_kitchen_number: '3'
  random_kitchen_on_1: switch.kitchen_switch
  random_kitchen_off_1: switch.kitchen_switch
  
  random_front_room_name: FrontRoom
  random_front_room_start: Evening
  random_kitchen_end: Room Off
  random_front_room_minduration: "00:10:00"
  random_front_room_maxduration: "00:33:00"
  random_front_room_number: '3'
  random_front_room_on_1: switch.front_room_switch
  random_front_room_off_1: switch.front_room_switch
  
  step_tv_name: TV
  step_tv_start: "19:45:00"
  step_tv_end: "20:30:00"
  step_tv_on_1: input_boolean.plex_away
  
  step_tv_off_name: TV Off
  step_tv_off_start: "22:15:00"
  step_tv_off_end: "23:45:00"
  step_tv_off_off_1: input_boolean.plex_away

Switch Reset:
  module: switch_reset
  class: SwitchReset
  file: /conf/switches.db
  delay: '5'
  log: '1'

i dont know enough from occusim to see what might be wrong with this setup.
the best thing i think you can do is remove everything and test part by part.

1 thing i think can cause trouble.

using morning_off as name is not a good idea.

step_morning_off is part from the morning step
so i would at least change step_morning_off_off to step_morningoff_off

same with room_off diner_end etc.

Ok I did as you suggested and started commenting out things until they worked. I am pretty sure it has to do with the sunrise and sunset items because when I change those to actual times it all works. I am guessing there is something wrong with how my longitude and latitude are set up within AD because I had to input this data into the appdaemon.yaml file manually for AD to work but also it has my sunset at about 23:55 tonight which is completely wrong for my location so I am guessing occusim is saying that some of my start times are after my end times so it errors out is my guess.

that should normally not be neccecary.
but if you have that set and the device settings are wrong then also the AD timesettings are wrong.

what didnt work when you didnt set that in the yaml?
it should only be set if you got a race problem with HA.

please show me your appdaemon.yaml and the log from AD right after startup (and if you dont mind also without the longtitude and latitude settings)

I was going to post my log files but my computer just crashed out on my so i will have to wait until i get home to reset it so that I can post the log files. Here is a link to an issue I filed awhile ago and if I didn’t have the longitude and latitude info my AD wouldn’t connect. I am not sure it will work with a local ip address but I can try it when i get home.

Ok if I take the longitude and latitude out of the appdaemon.yaml file it seems to work as long as I don’t have sunrise or sunset in the occusim file. It looks like my time is off by 4 hours in the log file though as I ran this a 17:13 not 21:13.

2018-09-14 21:13:31.319086 INFO AppDaemon Version 3.0.1 starting
2018-09-14 21:13:31.319276 INFO Configuration read from: /conf/appdaemon.yaml
2018-09-14 21:13:31.319996 INFO AppDaemon: Starting Apps
2018-09-14 21:13:31.321416 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2018-09-14 21:13:31.326319 INFO AppDaemon: HASS: HASS Plugin Initializing
2018-09-14 21:13:31.326633 INFO AppDaemon: HASS: HASS Plugin initialization complete
2018-09-14 21:13:31.326805 INFO Dashboards are disabled
2018-09-14 21:13:31.326955 INFO API is disabled
2018-09-14 21:13:31.330716 INFO AppDaemon: HASS: Connected to Home Assistant 0.77.3
2018-09-14 21:13:31.360352 INFO AppDaemon: Got initial state from namespace default
2018-09-14 21:13:33.355403 INFO AppDaemon: Reading config
2018-09-14 21:13:33.365959 INFO AppDaemon: /conf/apps/apps.yaml added or modified
2018-09-14 21:13:33.366213 INFO AppDaemon: /conf/apps/apps.yaml added or modified
2018-09-14 21:13:33.366349 INFO AppDaemon: App 'Occupancy Simulator' added
2018-09-14 21:13:33.366468 INFO AppDaemon: App 'Switch Reset' added
2018-09-14 21:13:33.366739 INFO AppDaemon: Adding /conf/apps to module import path
2018-09-14 21:13:33.367318 WARNING AppDaemon: No app description found for: /conf/apps/globals.py - ignoring
2018-09-14 21:13:33.367453 INFO AppDaemon: Loading App Module: /conf/apps/occusim.py
2018-09-14 21:13:33.372999 INFO AppDaemon: Loading App Module: /conf/apps/switch_reset.py
2018-09-14 21:13:33.374041 INFO AppDaemon: Initializing app Occupancy Simulator using class OccuSim from module occusim
2018-09-14 21:13:33.405404 INFO Occupancy Simulator: Morning in the past - ignoring
2018-09-14 21:13:33.405946 INFO Occupancy Simulator: Morning Off in the past - ignoring
2018-09-14 21:13:33.406414 INFO Occupancy Simulator: Dinner Start in the past - ignoring
2018-09-14 21:13:33.406852 INFO Occupancy Simulator: Evening in the past - ignoring
2018-09-14 21:13:33.407261 INFO Occupancy Simulator: Kitchen_on_1 in the past - ignoring
2018-09-14 21:13:33.407660 INFO Occupancy Simulator: Kitchen_on_2 in the past - ignoring
2018-09-14 21:13:33.408074 INFO Occupancy Simulator: Kitchen_off_1 in the past - ignoring
2018-09-14 21:13:33.408480 INFO Occupancy Simulator: FrontRoom_on_2 in the past - ignoring
2018-09-14 21:13:33.408939 INFO Occupancy Simulator: Kitchen_off_2 in the past - ignoring
2018-09-14 21:13:33.409378 INFO Occupancy Simulator: FrontRoom_off_2 in the past - ignoring
2018-09-14 21:13:33.409792 INFO Occupancy Simulator: Living Room in the past - ignoring
2018-09-14 21:13:33.410245 INFO Occupancy Simulator: Kitchen_on_0 in the past - ignoring
2018-09-14 21:13:33.410708 INFO Occupancy Simulator: Kitchen_off_0 in the past - ignoring
2018-09-14 21:13:33.411177 INFO Occupancy Simulator: Dinner_on_0 in the past - ignoring
2018-09-14 21:13:33.411664 INFO Occupancy Simulator: Dinner_off_0 in the past - ignoring
2018-09-14 21:13:33.412170 INFO Occupancy Simulator: FrontRoom_on_1 in the past - ignoring
2018-09-14 21:13:33.412641 INFO Occupancy Simulator: TV in the past - ignoring
2018-09-14 21:13:33.413040 INFO Occupancy Simulator: Dinner End in the past - ignoring
2018-09-14 21:13:33.413490 INFO Occupancy Simulator: FrontRoom_off_1 in the past - ignoring
2018-09-14 21:13:33.413907 INFO Occupancy Simulator: FrontRoom_on_0 in the past - ignoring
2018-09-14 21:13:33.414322 INFO Occupancy Simulator: FrontRoom_off_0 in the past - ignoring
2018-09-14 21:13:33.414765 INFO Occupancy Simulator: Room Off: @ 2018-09-14 22:15:00
2018-09-14 21:13:33.415237 INFO Occupancy Simulator: TV Off: @ 2018-09-14 22:33:51
2018-09-14 21:13:33.427644 INFO Occupancy Simulator: Living Room Off: @ 2018-09-14 22:39:10
2018-09-14 21:13:33.427838 INFO AppDaemon: Initializing app Switch Reset using class SwitchReset from module switch_reset
2018-09-14 21:13:33.429943 INFO AppDaemon: App initialization complete

Here is my appdaemon.yaml file:

log:
  logfile: /conf/log/logfile.log
  errorfile: /conf/log/errorfile.log
  diagfile: /conf/log/diagnostic.log
appdaemon:
  threads: 10
  app_dir: /conf/apps
  plugins:
    HASS:
      type: hass
      ha_url: http://XXX.XXX.X.XXX:XXXX
      ha_key: Password

without that set in AD it should take the time from HA.
do you have time configured in HA, so that you can see time and date as a sensor, so that you can confirm that HA is running with the right time?

edit and did you configure longtitude, latitude and timezone in HA and how?

yes I confirmed my HA does output time and it is correct. Also my HA log shows the correct time as well.

Here is what I have for HA

homeassistant:
  # Name of the location where Home Assistant is running
  name: Granger New
  # Location required to calculate the time the sun rises and sets
  latitude: 39.76852
  longitude: -86.15798
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 274
  # metric for Metric, imperial for Imperial
  unit_system: imperial
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: America/Indiana/Indianapolis

i just realised.
AD takes the times from HA for its vars.
but it takes the device time for its logging.
so your device time is 4 hours off.

So you are saying the device HA and AD are running on. That shows the same time as HA so I am not sure why it is off. I am running this on Unraid.

hmm, cant be.

but you can check that with a small app.

import datetime
class sound(hass.Hass):

  def initialize(self):
    localtime = datatime.datetime.now()
    self.log(localtime)
    hatime = self.get_state("sensor.date__time")
    self.log(hatime)
    utctime = datetime.datetime.utcnow()
    self.log(utctime)

that gives you the different times. (off course when you configured the datetime sensor in HA)

Getting an error for this

2018-09-15 16:10:53.075037 WARNING AppDaemon: ------------------------------------------------------------
2018-09-15 16:10:53.075171 WARNING AppDaemon: Unexpected error running initialize() for Date
2018-09-15 16:10:53.075277 WARNING AppDaemon: ------------------------------------------------------------
2018-09-15 16:10:53.075631 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1575, in init_object
    init()
  File "/conf/apps/date.py", line 20, in initialize
    localtime = datatime.datetime.now()
NameError: name 'datatime' is not defined

2018-09-15 16:10:53.075727 WARNING AppDaemon: ------------------------------------------------------------

Ok i figured it out. there was just a typo.

Ok here is the output of the log:

2018-09-15 18:21:44.613956 INFO Date: 2018-09-15 18:21:44.613951
2018-09-15 18:21:44.614583 INFO Date: 2018-09-15, 14:21
2018-09-15 18:21:44.614700 INFO Date: 2018-09-15 18:21:44.614695

just as i figured.
your datetime.now() gives the same as your datetime.utcnow()
that means that your device settings are set to utc
so the internal clock from your device is set to utc.

thats not something that can be helped inside AD.
the fact that HA shows the right time is because that is a recalculated time.
HA takes UTC time and recalculated that to local time.

but in AD you have to deal with plain python, and python takes the local time from the device.
so you have 2 options:

  1. find a way to change your device time
  2. edit every app to change utc to utc - 4

Thank you. I will try to figure out how to subtract 4 hours from each app. I am not real familiar with python but i will try to figure it out.

i think its more easy to find out how to change your device time. (its in the real device settings, not in a clock that you installed)
when you dont, your logs will always show wrong times

it might be worth googling for

changing time for the_device_you_own

Well I thought I had this figured out because I changed the clock in the BIOS however when I ran the date app again I got this

2018-09-18 12:52:01.304806 INFO Date: 2018-09-18 12:52:01.304800
2018-09-18 12:52:01.339797 INFO Date: 2018-09-18, 08:51
2018-09-18 12:52:01.340050 INFO Date: 2018-09-18 12:52:01.340036

I have changed the time in BIOS twice now and it seems like it gets reset somehow.

some bios have a checkbox that it controls the time according to a website. so then everything gets reset based on the location settings.

also possible that your operating system has clock settings and overrides the bios.

it must be somewhere in the device or OS settings.