YAMA - Yet Another Motion Automation (Scenes, Ambient Light and some Conditions )

Hey guys,

  1. Does anyone have an issue where the lights don’t turn off after x seconds?
  2. Can you have multiple automation_blockers ?

Basically, I’m trying to use this for my bedroom lights and would like to not run this automation , if my phone is charging + x + x blala.

mode: restart
max_exceeded: silent
variables:
  scene_ambient: scene.none
  scene_morning: scene.none
  scene_day: scene.none
  scene_evening: scene.none
  scene_night: scene.none
  automation_blocker: binary_sensor.pixel_4a_5g_is_charging
  automation_blocker_boolean: false
  no_motion_blocker: null
  no_motion_blocker_boolean: false
  elevation_check: 2
  scene_no_motion: scene.none
  motion_entity: binary_sensor.lumi_lumi_sensor_motion_aq2_b3c65b07_ias_zone
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_b3c65b07_ias_zone
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_b3c65b07_ias_zone
    from: 'on'
    to: 'off'
    for: 120
condition:
  - condition: or
    conditions:
      - '{{ automation_blocker == none }}'
      - >-
        {{ automation_blocker_boolean and states[automation_blocker].state ==
        'on' }}
      - >-
        {{ not automation_blocker_boolean and states[automation_blocker].state
        == 'off' }}
  - condition: template
    value_template: >-
      {{ (elevation_check == none) or (state_attr('sun.sun','elevation') <=
      elevation_check | float(90)) }}
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == ''on'' }}'
        sequence:
          - choose:
              - conditions:
                  - '{{ scene_morning != ''scene.none''}}'
                  - condition: time
                    after: '00:00:00'
                    before: '00:00:00'
                sequence:
                  - scene: scene.none
              - conditions:
                  - '{{ scene_day != ''scene.none''}}'
                  - condition: time
                    after: '00:00:00'
                    before: '00:00:00'
                sequence:
                  - scene: scene.none
              - conditions:
                  - '{{ scene_evening != ''scene.none''}}'
                  - condition: time
                    after: '00:00:00'
                    before: '00:00:00'
                sequence:
                  - scene: scene.none
              - conditions:
                  - '{{ scene_night != ''scene.none''}}'
                  - condition: time
                    after: '00:00:00'
                    before: '00:00:00'
                sequence:
                  - scene: scene.none
            default:
              - service: light.turn_on
                target:
                  entity_id: light.bedroom_lights
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == ''off'' }}'
          - condition: or
            conditions:
              - '{{ no_motion_blocker == none }}'
              - >-
                {{ no_motion_blocker_boolean and states[no_motion_blocker].state
                == 'on' }}
              - >-
                {{ not no_motion_blocker_boolean and
                states[no_motion_blocker].state == 'off' }}
        sequence:
          - choose:
              - conditions:
                  - '{{ scene_ambient != ''scene.none'' }}'
                  - condition: time
                    after: '00:00:00'
                    before: '00:00:00'
                sequence:
                  - scene: scene.none
              - conditions:
                  - '{{ scene_no_motion != ''scene.none'' }}'
                sequence:
                  - scene: scene.none
            default:
              - service: light.turn_off
                target:
                  entity_id: light.bedroom_lights
id: '1658293153688'
alias: Motion Lights - Bedroom
description: ''

You cannot use single quotes within single quotes. like '{{ scene_ambient != ''scene.none'' }}'
The parser does not work inside out. It starts seeing the text string at first quote and ends it at the next.

The blueprint above uses double quotes outside and single inside.

This is just what I needed - Thank you!
Unfortunately I am not very good at programming this and it doesnt seem to do what I wanted. (sorry - newbie alert…!)

I would like this automation to run:

  • if motion is detected between 22:35 and 7:00 (while the sun is down, whichever of the 7am or sunrise comes earlier)
  • turn on the porch lights for 20seconds
    Meaning that the automation should not run before 22:35, and should not run after 7am or sunrise, whichever comes first.

this is the yaml I put together - where am I going wrong please?

alias: Porch Lights on motion sensor between 22:35 and sunrise
description: ''
use_blueprint:
  path: networkingcat/yet_another_motion_automation.yaml
  input:
    motion_entity: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    light_target:
      area_id: porch
    no_motion_wait: 20
    scene_morning: scene.none
    time_scene_ambient_start: '22:35:00'
    time_scene_ambient_end: '07:00:00'
    elevation_check: -1

thank you for corrections!

Please could I get some guidance:

What I want to achieve.

Motion sensor kitchen lights.
If kitchen lights are turned on via wall button, then motion sensor off should not happen for 1 hour unless lights are manually turned off via switch again. Is this possible, and if so how ?

Can I add a time condition ? Like I don’t want the automation should not run between ,7-10pm?

Ok, first thing you should look at is the reaction time of the motion sensor vs your wishes of turning it off after 20sec. Maybe I misunderstood, but probably your sensor has a built-in time-out for a minimum of 90 seconds. Sometimes you can change this, but there will almost always be a hardware problem if you change it to a too low value. Two things will probably be influenced by that time out:

  1. Sensor will simply not tell you that there is no movement before this internal time is over
  2. Sensor will not react correctly to a new movement within this time frame.

If you would really like to kill the lights already after 20s, you can of course create another automation that -indepent of YAMA- simply turns off the lights 20s after it changed to ON state, but I really don’t recommend doing that. It will probably create undesired reactions like on-off, 20s is really very fast and YAMA might even try to turn it back on since the sensor still says there is motion.

Then about the way you try to use YAMA, it looks like you set a time frame for ambient light meaning during the time frame 22:35 till 07:00 when there is no motion detected you’re telling the lights to turn on using “scene.none” and thus give you some ambient (default background) lighting. Since you don’t set a specific scene, it will simply turn the light on. When however there is motion detected, it will turn on as well, effectively programming the light to be ON from 22:35 till 07:00. During the rest of the day, YAMA will turn on the light according to the motion sensor and off 20s after motion sensor changes state to “no_motion”.

To create the described behaviour, make a YAMA automation with only the porch light set up, leave all times and scenes empty. This is the simplest of YAMA, when motion turn on light, 10s after sensor tells you no more motion is detected turn the light off. Nothing else needed, light will stay on as long as motion is detected + probably 90s

Then create a helper automation triggered by time that simply turns on the YAMA at 22:35 and turns it off at 07:00 and at sunrise. To prevent the light staying ON all day whenever there was a motion just before 7 or sunrise, in this same automation simply always turn the porch light off as well.

Simplest YAMA automation:

alias: Porch - YAMA
description: "Porch YAMA"
use_blueprint:
  path: networkingcat/yet_another_motion_automation.yaml
  input:
    no_motion_wait: 10
    motion_entity: binary_sensor.motion_porch_occupancy
    light_target:
      entity_id:
        - light.porch

And then the helper automation:

alias: Activate porch YAMA
description: "Turn automation on 22:35 and of at sunrise or 07:00"
mode: single
trigger:
  - platform: time
    at: "22:35:00"
    id: ID_ON
  - platform: time
    at: "07:00:00"
    id: ID_OFF
  - platform: sun
    event: sunrise
    offset: 0
    id: ID_OFF
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: ID_ON
        sequence:
          - service: automation.turn_on
            data: {}
            target:
              entity_id: automation.porch_yama
      - conditions:
          - condition: trigger
            id: ID_OFF
        sequence:
          - service: automation.turn_off
            data: {}
            target:
              entity_id: automation.porch_yama
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.porch
    default: []
1 Like

Q1: create a helper automation that disables the yama when switched on with wall button and shuts off the light and re-enables the YAMA automation either when switch pressed again or automatically after an hour.

Q2: yes, just use the blocking option with a timed helper binary sensor

thank you @_Xander, much appreciated! I was able to follow and re-do this at my end, seems to work now. Thanks again for taking the time to help a newbie!

1 Like

Could you elaborate on this please, I am not very good with helpers yet

Simply create a new empty automation, go to the … on the top right and choose yaml. Then you can simply paste the code from above.

It has nothing to do with “helper” as used in the GUI. It is simply an extra automation that toggles the functionality of the YAMA one. You could also use the YAMA incorporated blocking variable to prevent YAMA from functioning, but that one is imho not always useful since it leaves the current (light) state forever. A second automation as described above prevents this since I simply always also turn the light off when the automation is switched off.

Perfect, thank you

2nd scenario

Bathroom motion lights

I want the automation to not turn off the lights while I’m in the shower. Bathroom has a humidity sensor. Usually sits around 65 but jumps quickly to 70+ when shower is running.

I created an threshold helper with upper limit 70 with humidity sensor as it’s entity and then added it as a no motion blocker in ON position but it’s not stopping the lights from turning off when shower is running :confused:

Because the humidity could change from one day to another, an idea to improve the behavior would be:
First we create a sensor that generates the humidity average over last 30 minutes or last hour.

sensor:
  - platform: statistics
    name: Bath Humidity Average
    entity_id: sensor.sonoff_temp_humidity_sensor_humidity
    sampling_size: 40
    max_age:
      hours: 1
    state_characteristic: average_linear

And then we do a helper binary sensor, to compare realtime humidity and the average. If the difference jumps beyond, say, 5%, we consider it a shower event:

template:
  - binary_sensor:
      - name: "Showering"
        unique_id: showering
        device_class: motion
        state: > 
          {{( states('sensor.sonoff_temp_humidity_sensor_humidity') | float(0) ) > 
            ( states('sensor.bath_humidity_average') | float(0) + 5 )  }}

Anyways, my 2 cents. Feel free to adapt the config to meet your need.

1 Like

Thats sound like it will work. Much appreciated. Giving it a go !

To have it completely independent of the actual average, you can also create a helper sensor being the derivative of the humidity sensor. This derivative will immediately become quite high when the showering starts, so make an automation that turns off the YAMA when dHum > 100. Turning it back on depends a bit on your situation, does your shower have good mechanical ventilation, then you can turn it back on as soon as the dHum < -10, indicating the moment where the humidification is starting to decrease. Another possibility of course is simply a wait timer to turn it back on.

I have a problem that the light automatically turns on before sunrise, but when it is after sunset, it does not want to turn off.

See above for a possible solution using a second automation.

@networkingcat would it be possible to create a version with blocking boolean that only blocks the turning on, or ideally change the current blocking bool to only starting a new sequence and create a second bool entry that blocks tuning off.

Is it possible to add a 2nd no motion blocker motion sensor to this?

Easiest way: create a helper boolean and feed all desired blockers into that one and only use the helper bool in YAMA.

Harder way: edit the blueprint to include another one or edit the blueprint to also accept a template where you can also use as many bools as you would like…

I have setup an automation blocker, so lights do not get turned on, when it is not dark (via helper).

Am I right, that this leads to some kind of loop whole?
i.e. when it is dark, motion is detected, then lights turn on. While the lights being on, it gets so bright, that the automation blocker kicks in. So the lights will stay on all day.

2 Likes