Trigger light by movement but with 4 different time profiles (color and brightness)

do you realy have

def init(...):

in the app?
it seems to me you miss basic knowledge and in that case i would advice you to read my beginners guide:

I have read some more documentation, but I am missing something.

I have a working sensor containing time of day:

- platform: template
    sensors:
      day_interval:
        friendly_name: "Tid"
        value_template: >
            {% if now().hour in range(0, 6) %}
            Nat
            {% elif now().hour in range(6, 18) %}
            Dag
            {% elif now().hour in range(18, 22) %}
            Aften
            {% elif now().hour in range(22, 24) %}
            AftenSent
            {% endif %}

Then I have the apps.yaml as

Motion:
  module: MotionSensor
  class: Motion
  light: light.badevrelse_lille_level
  Sensor: binary_sensor.sensor
  lightmapping:
    Nat: 20
    Dag: 255
    Aften: 100
    AftenSent: 50

But then I am missing the app itself.

How do I make something like`the below work in my case?

def init(...):
  self.listen_state(self.callback,"input_select.tod")
def callback(...):
  self.lightmapping = self.args["lightmapping"]
  desired_dimval = self.lightmapping[new]
  self.turn_on(self.arg_switch,brightness_pct = desired_dimval)

you got a template sensor giving you the time of day and you use an input_select as trigger for the app?
you have a light and a sensor in your args that you never use.
and you try to turn_on a switch that doesnt exist.

i really advice you to reread my beginners guide. create the apps that i did give you there and try to understand them.
if you understand what you are doing you can try to create this app again.

if you want to create an app, you need to know why you put what where.

off course i could write the complete app for you, but then you miss the opportunity to learn to write it yourself.

in my guide i explained what listen_state does, how to use args and how to turn on a light or switch.
and thats actually all you want to do :wink:

1 Like

I agree with Rene, I would add that in my experience its best to start small. If you want to create an app, create one that just turns on and off a light for example, I have one that turns my Xiaomi gateway light on and off once a minute (as a work around to make it update the light level).

Rene and I along with many others in the forum will be more than happy to help but if you want to learn, start with Renes guide it will show you some basic but very important concepts like how the callbacks work. (Basically you subscribe to an event in homeassistant and when it occurs a function is run.

1 Like

I spend some more time reading documentation and working on the app, so now I got a working one :slight_smile:

motion_dim.py:

import appdaemon.plugins.hass.hassapi as hass

class motion_dimming(hass.Hass):

  def initialize(self):
    self.listen_state(self.motion_on,"binary_sensor.sensor", new="on")
    self.listen_state(self.motion_off,"binary_sensor.sensor", new="off")
    self.light_mappings = self.args["lightmapping_pct"]

  def motion_on (self, entity, attribute, old, new, kwargs):
    lux = float(self.get_state("sensor.aeotec_zw100_multisensor_6_luminance"))
    day_section = self.get_state("sensor.day_interval")
    desired_dimval = self.light_mappings[day_section]
    if lux <= 100:
      self.turn_on(self.args["lightID"], brightness_pct = desired_dimval)

  def motion_off (self, entity, attribute, old, new, kwargs):
      self.turn_off(self.args["lightID"]) 

apps.yaml

Motion_with_dimming:
   module: motion_dim
   class: motion_dimming
   lightID: light.badevrelse_lille_level
   sensorID: binary_sensor.sensor
   luxID: sensor.aeotec_zw100_multisensor_6_luminance
   lightmapping_pct:
    Nat: 30
    Dag: 100
    Aften: 80
    AftenSent: 50

The sensor used for which part of day

- platform: template
    sensors:
      day_interval:
        friendly_name: "Tid"
        value_template: >
            {% if now().hour in range(0, 6) %}
            Nat
            {% elif now().hour in range(6, 18) %}
            Dag
            {% elif now().hour in range(18, 22) %}
            Aften
            {% elif now().hour in range(22, 24) %}
            AftenSent
            {% endif %}

I would however like to make the app more generic, so I don’t reference “binary.sensor_sensor”, but I can’t replace it just with “sensorID” from my appsyaml, I guess I need args in there?

I am having some issues by understanding how it works with this line

  • desired_dimval = self.light_mappings[day_section]

There it takes the lightmappings from my apps.yaml file and equals the sensor.day_interval, but how does it do that since no equal sign has been used? Is it because there only is one parameter in both that it is not explictely needed?

Feedback for improving the code is also very welcome.

indeed you need args. so like

self.args["sensorID"]

that line has a few things:

   self.light_mappings = self.args["lightmapping_pct"]
    day_section = self.get_state("sensor.day_interval")
    desired_dimval = self.light_mappings[day_section]

first you get lightmapping_pct from the yaml.
that is a dict containing:
{“Nat”: 30, “Dag”: 100, “Aften”: 80, “AftenSent”: 50}
then you get the state from the sensor day interval that has the same value as the keys from the dict.
and then in the last line you get the value that belongs to the key

to make it more general:

test = {"key1": "value1", "key2": "value2"}
value_from_key1 = test["key1"]
value_from_key2 = test["key2"]

That makes sense, thanks a lot for the explation.