Kaynağa Gözat

Working with a group main remote.

Thomas Chef 4 yıl önce
ebeveyn
işleme
31b18ca18a

+ 43 - 5
configuration.yaml

@@ -45,15 +45,49 @@ rflink:
 binary_sensor:
   - platform: rflink
     devices:
-      newkaku_00066542_0:
+      newkaku_00066542_1:
         name: Ytterdörr brytare
         device_class: motion
         off_delay: 5  
 
-sensor:
+
+light:
   - platform: rflink
     automatic_add: true
+    devices:
+      newkaku_013242b6_2:
+        name: Exterior lights
+        type: switchable
+      newkaku_013242b5_2:
+        name: Exterior lights REPEATER
+        type: switchable
+      
+      newkaku_013242b6_3:
+        name: Piano-lamp
+        type: switchable
+      newkaku_013242b5_3:
+        name: Piano-lamp REPEATER
+        type: switchable
+
+      newkaku_006d7076_1:
+        name: Main Button 1
+        aliases:
+          - newkaku_006d7076_0
+      newkaku_006d7076_2:
+        name: Main Button 2
+        aliases:
+          - newkaku_006d7076_0
+      newkaku_006d7076_3:
+        name: Main Button 3
+        aliases:
+          - newkaku_006d7076_0
+      newkaku_006d7076_4:
+        name: Main Button 4
+        aliases:
+          - newkaku_006d7076_0
+
 
+sensor:
   - platform: sql
     db_url: mysql://hassio:hassiopassword@192.168.1.110/RemoteRxLog
     queries:
@@ -65,10 +99,14 @@ sensor:
         query: "SELECT temp1 FROM RemoteRxLog.temp order by ts desc LIMIT 1;"
         column: 'temp1'
         unit_of_measurement: C
+      - name: Piano-lamp repeater status
+        query: "SELECT IF(onOff>0,'on','off') AS status FROM RemoteRxLog.nexaLog where rxTx='R' and remote_id = '20071094' and button='2' order by ts desc LIMIT 1;"
+        column: 'status'
+      - name: Exterior light status
+        query: "SELECT IF(onOff>0,'on','off') AS status FROM RemoteRxLog.nexaLog where rxTx='R' and remote_id = '20071094' and button='1' order by ts desc LIMIT 1;"
+        column: 'status'
+
 
-switch:
-  - platform: rflink
-    automatic_add: true
 
 group: !include groups.yaml
 automation: !include automations.yaml

+ 4 - 1
packages/ext_lights_automation.yaml

@@ -4,4 +4,7 @@ input_boolean:
     name: Exterior lights, wanted state
     icon: mdi:lightbulb
 
-  
+  
+
+
+

+ 0 - 58
packages/ext_lights_control.yaml

@@ -1,58 +0,0 @@
-
-
-
-light:
-  - platform: rflink
-    automatic_add: true
-    devices:
-      newkaku_013242b6_1:
-        name: Exterior lights
-        type: switchable
-      newkaku_013242b5_1:
-        name: Exterior lights REPEATER
-        type: switchable
-
-sensor:
-  - platform: sql
-    db_url: mysql://hassio:hassiopassword@192.168.1.110/RemoteRxLog
-    queries:
-    - name: Exterior light status
-      query: "SELECT IF(onOff>0,'on','off') AS status FROM RemoteRxLog.nexaLog where rxTx='R' and remote_id = '20071094' and button='1' order by ts desc LIMIT 1;"
-      column: 'status'
-
-automation:
-  - alias: External light control
-    trigger:
-    - platform: state
-      entity_id: input_boolean.exterior_lights_wanted_state
-    - platform: event
-      event_type: timer.finished
-      event_data:
-        entity_id: timer.ext_light_control_timer
-    condition: 
-      condition: template
-      value_template: >
-        {% if state_attr('sensor.exterior_light_status', 'status') == states('input_boolean.exterior_lights_wanted_state') %}
-          false
-        {% else %}
-          true
-        {% endif %}
-    action:
-    - service_template: light.turn_{{states('input_boolean.exterior_lights_wanted_state')}}
-      entity_id: light.exterior_lights_repeater
-    - service_template: light.turn_{{states('input_boolean.exterior_lights_wanted_state')}}
-      entity_id: light.exterior_lights
-    - service_template: >
-        {% if state_attr('sensor.exterior_light_status', 'status') == states('input_boolean.exterior_lights_wanted_state') %}
-          timer.cancel
-        {% else %}
-          timer.start
-        {% endif %}
-      entity_id: timer.ext_light_control_timer
-    mode: single
-
-
-timer:
-  ext_light_control_timer:
-    duration: '00:00:45'
-

+ 0 - 16
packages/test_stuff.yaml

@@ -1,21 +1,5 @@
 
-light:
-  - platform: rflink
-    devices:
-      newkaku_013242b6_2:
-        name: Piano-lamp
-        type: switchable
-      newkaku_013242b5_2:
-        name: Piano-lamp REPEATER
-        type: switchable
 
-sensor:
-  - platform: sql
-    db_url: mysql://hassio:hassiopassword@192.168.1.110/RemoteRxLog
-    queries:
-      - name: Piano-lamp repeater status
-        query: "SELECT IF(onOff>0,'on','off') AS status FROM RemoteRxLog.nexaLog where rxTx='R' and remote_id = '20071094' and button='2' order by ts desc LIMIT 1;"
-        column: 'status'
 
 input_boolean:
   piano_light_wanted_state:

+ 45 - 15
pyscript/ext_light_control.py

@@ -1,22 +1,52 @@
 
-state.persist('pyscript.ext_light_on_done','off')
-state.persist('pyscript.ext_light_off_done','off')
+state.persist('pyscript.ext_light_has_been_day','false')
+state.persist('pyscript.ext_light_has_been_night','false')
 
-@time_trigger("once(00:15:00)")
-def reset_ext_light_done_states():
-    log.info(f"Reset the ext light done state variables.")
-    pyscript.ext_light_on_done = 'off'
-    pyscript.ext_light_off_done = 'off'
+# Set that it has been midnight, and that it's ok to turn the light on
+@time_trigger("once(00:30:00)")
+def ext_light_has_been_night():
+	pyscript.ext_light_has_been_night = 'true'
 
-@state_trigger( "int(sensor.lux_outside_the_garage) < 100 and int(sensor.lux_outside_the_garage.old) >= 100")
+# Set that it has been noon, and that it's ok to turn off the light
+@time_trigger("once(12:30:00)")
+def ext_light_has_been_day():
+	pyscript.ext_light_has_been_day = 'true'
+
+
+@state_trigger("pyscript.ext_light_has_been_day == 'true' and int(sensor.lux_outside_the_garage) < 100")
 def automate_exterior_lights_on_in_evening():
-    if pyscript.ext_light_on_done == 'off' and input_boolean.exterior_lights_wanted_state == 'off':
-        input_boolean.exterior_lights_wanted_state = 'on'
-        pyscript.ext_light_on_done = 'on'
+	pyscript.ext_light_has_been_day = 'false'
+	input_boolean.exterior_lights_wanted_state = 'on'
 
-@state_trigger( "int(sensor.lux_outside_the_garage) > 50 and int(sensor.lux_outside_the_garage.old) <= 50")
+@state_trigger("pyscript.ext_light_has_been_night == 'true' and int(sensor.lux_outside_the_garage) > 50")
 def automate_exterior_lights_off_in_morning():
-    if pyscript.ext_light_off_done == 'off' and input_boolean.exterior_lights_wanted_state == 'on':
-        input_boolean.exterior_lights_wanted_state = 'off'
-        pyscript.ext_light_off_done = 'on'
+	pyscript.ext_light_has_been_night = 'false'
+	input_boolean.exterior_lights_wanted_state = 'off'
+
+
+
+@state_trigger("input_boolean.exterior_lights_wanted_state")
+@task_unique("control_ext_light", kill_me=True)
+def control_ext_light():
+
+    # Set max number of iterations
+    maxIterations = 5
+
+    # Loop until we have succeeded to controlling the light
+    while(
+             ( (sensor.exterior_light_status != input_boolean.exterior_lights_wanted_state) or 
+               (        light.exterior_lights!= input_boolean.exterior_lights_wanted_state) ) and maxIterations > 0
+    ):
+
+        # Find out if we should turn the light on or off
+        action = "turn_off" if input_boolean.exterior_lights_wanted_state == 'off' else "turn_on"
+        
+        service.call("light", action, entity_id="light.exterior_lights")
+        service.call("light", action, entity_id="light.exterior_lights_repeater")
+
+        log.info("Sleeping 60s.....")
+        trig_info = task.wait_until(state_trigger=["input_boolean.exterior_lights_wanted_state","sensor.exterior_light_status"], timeout=60)
 
+        maxIterations -= 1
+    
+    log.info(f"Exit control_ext_light")