pi преди 4 години
родител
ревизия
f4b10b8d6b

+ 54 - 0
blueprints/automation/homeassistant/motion_light.yaml

@@ -0,0 +1,54 @@
+blueprint:
+  name: Motion-activated Light
+  description: Turn on a light when motion is detected.
+  domain: automation
+  source_url: https://github.com/home-assistant/core/blob/dev/homeassistant/components/automation/blueprints/motion_light.yaml
+  input:
+    motion_entity:
+      name: Motion Sensor
+      selector:
+        entity:
+          domain: binary_sensor
+          device_class: motion
+    light_target:
+      name: Light
+      selector:
+        target:
+          entity:
+            domain: light
+    no_motion_wait:
+      name: Wait time
+      description: Time to leave the light on after last motion is detected.
+      default: 120
+      selector:
+        number:
+          min: 0
+          max: 3600
+          unit_of_measurement: seconds
+
+# If motion is detected within the delay,
+# we restart the script.
+mode: restart
+max_exceeded: silent
+
+trigger:
+  platform: state
+  entity_id: !input motion_entity
+  from: "off"
+  to: "on"
+
+action:
+  - alias: "Turn on the light"
+    service: light.turn_on
+    target: !input light_target
+  - alias: "Wait until there is no motion from device"
+    wait_for_trigger:
+      platform: state
+      entity_id: !input motion_entity
+      from: "on"
+      to: "off"
+  - alias: "Wait the number of seconds that has been set"
+    delay: !input no_motion_wait
+  - alias: "Turn off the light"
+    service: light.turn_off
+    target: !input light_target

+ 44 - 0
blueprints/automation/homeassistant/notify_leaving_zone.yaml

@@ -0,0 +1,44 @@
+blueprint:
+  name: Zone Notification
+  description: Send a notification to a device when a person leaves a specific zone.
+  domain: automation
+  source_url: https://github.com/home-assistant/core/blob/dev/homeassistant/components/automation/blueprints/notify_leaving_zone.yaml
+  input:
+    person_entity:
+      name: Person
+      selector:
+        entity:
+          domain: person
+    zone_entity:
+      name: Zone
+      selector:
+        entity:
+          domain: zone
+    notify_device:
+      name: Device to notify
+      description: Device needs to run the official Home Assistant app to receive notifications.
+      selector:
+        device:
+          integration: mobile_app
+
+trigger:
+  platform: state
+  entity_id: !input person_entity
+
+variables:
+  zone_entity: !input zone_entity
+  # This is the state of the person when it's in this zone.
+  zone_state: "{{ states[zone_entity].name }}"
+  person_entity: !input person_entity
+  person_name: "{{ states[person_entity].name }}"
+
+condition:
+  condition: template
+  value_template: "{{ trigger.from_state.state == zone_state and trigger.to_state.state != zone_state }}"
+
+action:
+  - alias: "Notify that a person has left the zone"
+    domain: mobile_app
+    type: notify
+    device_id: !input notify_device
+    message: "{{ person_name }} has left {{ zone_state }}"

+ 74 - 0
blueprints/script/homeassistant/confirmable_notification.yaml

@@ -0,0 +1,74 @@
+blueprint:
+  name: Confirmable Notification
+  description: >-
+    A script that sends an actionable notification with a confirmation before
+    running the specified action.
+  domain: script
+  source_url: https://github.com/home-assistant/core/blob/master/homeassistant/components/script/blueprints/confirmable_notification.yaml
+  input:
+    notify_device:
+      name: Device to notify
+      description: Device needs to run the official Home Assistant app to receive notifications.
+      selector:
+        device:
+          integration: mobile_app
+    title:
+      name: "Title"
+      description: "The title of the button shown in the notification."
+      default: ""
+      selector:
+        text:
+    message:
+      name: "Message"
+      description: "The message body"
+      selector:
+        text:
+    confirm_text:
+      name: "Confirmation Text"
+      description: "Text to show on the confirmation button"
+      default: "Confirm"
+      selector:
+        text:
+    confirm_action:
+      name: "Confirmation Action"
+      description: "Action to run when notification is confirmed"
+      default: []
+      selector:
+        action:
+    dismiss_text:
+      name: "Dismiss Text"
+      description: "Text to show on the dismiss button"
+      default: "Dismiss"
+      selector:
+        text:
+    dismiss_action:
+      name: "Dismiss Action"
+      description: "Action to run when notification is dismissed"
+      default: []
+      selector:
+        action:
+
+mode: restart
+
+sequence:
+  - alias: "Send notification"
+    domain: mobile_app
+    type: notify
+    device_id: !input notify_device
+    title: !input title
+    message: !input message
+    data:
+      actions:
+        - action: "CONFIRM"
+          title: !input confirm_text
+        - action: "DISMISS"
+          title: !input dismiss_text
+  - alias: "Awaiting response"
+    wait_for_trigger:
+      - platform: event
+        event_type: mobile_app_notification_action
+  - choose:
+      - conditions: "{{ wait.trigger.event.data.action == 'CONFIRM' }}"
+        sequence: !input confirm_action
+      - conditions: "{{ wait.trigger.event.data.action == 'DISMISS' }}"
+        sequence: !input dismiss_action

+ 9 - 4
configuration.yaml

@@ -5,7 +5,6 @@ counter:
 logbook:
 notify:
 input_datetime:
-input_number:
 input_select:
 input_text:
 timer:
@@ -14,11 +13,15 @@ recorder:
   db_url: mysql://hassio:hassiopassword@192.168.1.110/hass?charset=utf8
 logger:
   default: info
-  logs:
-    rflink: debug
+#  logs:
+#    rflink: debug
 #    homeassistant.components.rflink: debug
 history:
 
+mqtt:
+  broker: hagaholm.ddns.net
+  port: 1883
+
 pyscript:
   allow_all_imports: true
 
@@ -34,8 +37,9 @@ binary_sensor:
    - platform: rflink
      devices:
        newkaku_02ddc7fa_4:
-         name: Bedroom door
+         name: Bedroom door raw sensor
          device_class: door
+         force_update: true
 
    - platform: workday
      country: SE
@@ -47,6 +51,7 @@ tts:
 
 switch: !include switches.yaml
 input_boolean: !include input_booleans.yaml
+input_number: !include input_numbers.yaml
 sensor: !include sensors.yaml
 light: !include lights.yaml
 group: !include groups.yaml

+ 10 - 1
input_booleans.yaml

@@ -6,4 +6,13 @@ exterior_lights_wanted_state:
 
 movie_mode:
   name: Movie mode
-  icon: mdi:movie
+  icon: mdi:movie
+
+auto_lights_off:
+  name: Auto Lights Off
+  initial: false
+  icon: mdi:weather-sunny
+
+bedroom_door:
+  name: Bedroom door state
+  icon: mdi:door

+ 10 - 0
input_numbers.yaml

@@ -0,0 +1,10 @@
+auto_light_off_lux_value:
+  name: Auto light off lux value
+  initial: 1000
+  min: 200
+  max: 3000
+  step: 50
+  mode: slider
+  unit_of_measurement: lux
+  icon: mdi:weather-sunny
+

+ 6 - 2
lights.yaml

@@ -1,6 +1,8 @@
 
 
 - platform: rflink
+  device_defaults:
+    signal_repetitions: 1
   automatic_add: true
   devices:
 
@@ -34,9 +36,11 @@
     
     newkaku_001ab10a_1:           # Dimmer CMR-101
       name: Liv_room_table_lamp
-      type: switchable
+      type: dimmable
+      signal_repetitions: 1
     newkaku_001ab10a_2:           # Dimmer MWMR-251
       name: Liv_room_corner_lamp
-      type: switchable
+      type: dimmable
+      signal_repetitions: 1
 
 

+ 27 - 7
pyscript/bedroom.py

@@ -1,22 +1,42 @@
 
 
+from homeassistant.const import EVENT_STATE_CHANGED
 
+# This script reacts to the actual sensor on the door
+# It sets the input_boolean which keeps its state during restarts
+# Arguments could be: (entity_id=None, new_state=None, old_state=None):
+@event_trigger(EVENT_STATE_CHANGED, "entity_id=='binary_sensor.bedroom_door_raw_sensor'")
+def bedroom_door_raw_sensor():
+    #log.info(f"monitor_bedroom_door_event: entity {entity_id} changed from {old_state} to {new_state}")
+    log.info(f"********* BEDROOM RAW SENSOR: "+binary_sensor.bedroom_door_raw_sensor+" ***********")
+    input_boolean.bedroom_door = binary_sensor.bedroom_door_raw_sensor
+    pyscript.handle_bedroom_light()
 
 
+
+# This service handles the window light in the bedroom
+# It reacts when the door opens/closes or when a state_goto-script is run
+#    when someone has pressed a button on the wall or kitchen IPhone.
 @service
 def handle_bedroom_light():
     task.unique('handle_bedroom_light')
-    log.info(f"********* BEDROOM LIGHT CONTROL **** Btn_4:" + light.main_button_4 + " Door:"+ binary_sensor.bedroom_door)
+    log.info(f"********* BEDROOM LIGHT CONTROL **** Btn_4:" + light.main_button_4 + " Door:"+ input_boolean.bedroom_door)
     task.sleep(1)
-    if light.main_button_4 == 'on' and binary_sensor.bedroom_door == 'on':
+
+    # The door sensor is out of battery, and thats why I disable this for now
+    #if light.main_button_4 == 'on' and input_boolean.bedroom_door == 'on':
+    if light.main_button_4 == 'on':
         # If OPEN
         switch.turn_on(entity_id='switch.tradfri_outlet')
     else:
         switch.turn_off(entity_id='switch.tradfri_outlet')
 
 
-@state_trigger("binary_sensor.bedroom_door")
-def bedroom_door_sensor():
-    task.unique('bedroom_door_sensor')
-    log.info(f"********* BEDROOM DOOR: "+binary_sensor.bedroom_door+" ***********")
-    pyscript.handle_bedroom_light()
+"""
+# This way does not work, beacuse it does not react to "off" when the door is already "off/aka Unknown" after a restart of HA
+@state_trigger("binary_sensor.bedroom_door_raw_sensor")
+def bedroom_door_raw_sensor():
+    log.info(f"********* BEDROOM RAW SENSOR: "+binary_sensor.bedroom_door_raw_sensor+" ***********")
+    input_boolean.bedroom_door = binary_sensor.bedroom_door_raw_sensor
+    pyscript.handle_bedroom_light()
+"""

+ 5 - 2
pyscript/home_automations.py

@@ -3,17 +3,20 @@
 state.persist('pyscript.home_auto_lights_off_has_been_night','false')
 
 
-@state_trigger("int(sensor.lux_outside_the_garage) > 1000", state_hold_false=0)
+@state_trigger("int(sensor.lux_outside_the_garage) > int(float(input_number.auto_light_off_lux_value))", state_hold_false=0)
 def automate_home_auto_lights_off():
+    log.info(f"********* AUTO LIGHTS OFF *********** Lux: " + str(sensor.lux_outside_the_garage) )
     if( pyscript.home_auto_lights_off_has_been_night == 'true' ):
         pyscript.home_auto_lights_off_has_been_night = 'false'
         
-        pyscript.state_goto_all_off();
+        if input_boolean.auto_lights_off == 'on':
+        	pyscript.state_goto_all_off();
 
 
 # Activate morning lights when Thomas gets up
 @time_trigger("once(06:18:00)")
 def morning_lights_thomas():
+    log.info(f"********* THOMAS MORNING ON ***********")
     if binary_sensor.workday_sensor == 'on':
         if switch.eng_heat_a == 'on' or switch.eng_heat_b == 'on':
             pyscript.state_goto_morning();

+ 18 - 16
pyscript/state_goto.py

@@ -30,13 +30,11 @@ def state_goto_all_on():
     light.turn_on(entity_id='light.tradfri_bulb')
     for x in range(2):
         switch.turn_on(entity_id='switch.group_button_all')
-        task.sleep(1.0)
-
-    if light.liv_room_table_lamp == 'off':
-        light.turn_on(entity_id='light.liv_room_table_lamp')
         task.sleep(0.5)
-    if light.liv_room_corner_lamp == 'off':
-        light.turn_on(entity_id='light.liv_room_corner_lamp')
+        light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
+        task.sleep(0.5)
+        light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
+        task.sleep(0.5)
 
     pyscript.handle_bedroom_light()
 
@@ -56,6 +54,8 @@ def state_goto_window():
         for x in range(2):
             light.turn_off(entity_id=' light.main_button_2')
             task.sleep(0.5)
+            light.turn_off(entity_id='light.liv_room_corner_lamp')
+            task.sleep(0.5)
     else:                   # From ALL ON
         for x in range(2):
             switch.turn_off(entity_id='switch.group_button_all')
@@ -82,14 +82,14 @@ def state_goto_morning():
             task.sleep(0.5)
             light.turn_on(entity_id=' light.main_button_2')
             task.sleep(0.5)
-        if light.liv_room_corner_lamp == 'off':
-            light.turn_on(entity_id='light.liv_room_corner_lamp')
+            light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
+            task.sleep(0.5)
     elif cs == 2:           # From Window
         for x in range(2):
             light.turn_on(entity_id=' light.main_button_2')
             task.sleep(0.5)
-        if light.liv_room_corner_lamp == 'off':
-            light.turn_on(entity_id='light.liv_room_corner_lamp')
+            light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
+            task.sleep(0.5)
     else:                   # From ALL ON
         for x in range(2):
             switch.turn_off(entity_id='switch.group_button_all')
@@ -99,8 +99,8 @@ def state_goto_morning():
             task.sleep(0.5)
             light.turn_on(entity_id=' light.main_button_4')
             task.sleep(0.5)
-        if light.liv_room_corner_lamp == 'off':
-            light.turn_on(entity_id='light.liv_room_corner_lamp')
+            light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
+            task.sleep(0.5)
     
     pyscript.handle_bedroom_light()
 
@@ -118,6 +118,8 @@ def state_goto_all_off():
         task.sleep(0.5)
         light.turn_off(entity_id='light.liv_room_corner_lamp')
         task.sleep(0.5)
+    input_boolean.movie_mode = 'off'
+
 
 @state_trigger("input_boolean.movie_mode")
 def movie_mode_button_trigger():
@@ -125,11 +127,11 @@ def movie_mode_button_trigger():
     log.info(f"********* MOVIE MODE *********** To:" + input_boolean.movie_mode + " State: " + str(cs) )
     if input_boolean.movie_mode == 'off' and switch.group_button_all == 'on':
         log.info(f"Movie mode, turn lights ON")
-        if light.liv_room_table_lamp == 'off':
-            light.turn_on(entity_id='light.liv_room_table_lamp')
+        for x in range(2):
+            light.turn_on(entity_id='light.liv_room_table_lamp',brightness='1')
+            task.sleep(0.5)
+            light.turn_on(entity_id='light.liv_room_corner_lamp',brightness='1')
             task.sleep(0.5)
-        if light.liv_room_corner_lamp == 'off':
-            light.turn_on(entity_id='light.liv_room_corner_lamp')
  
     else:
         log.info(f"Movie mode, turn lights OFF")

+ 9 - 0
sensors.yaml

@@ -17,3 +17,12 @@
     - 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'
+
+
+
+- platform: mqtt
+  name: Temperatur Hällby
+  state_topic: "sensor/outside/temperature"
+  unit_of_measurement: "°C"
+
+