Motion Automation to Change LEDs

Hi all,

I’m quite new to HA so please bear with me.

I have some RGBWW led strips for my kitchen run of an ESPThings Analog controller.

I can control the leds and run some simple effects like a slow rainbow.

I’ve now installed an Aqara PIR motion sensor, and I’m trying to automate it so when the PIR senses motion in the kitchen, it switches from running the slow rainbow effect to full brightness Cold White led’s.
The idea being that when someone enters the kitchen the led’s switch to cold white for additional lighting.
As I state below, I tried using the automation wizard in HA but then had a go with the yaml file.

I’ve had a few try’s with no success, I’ve tried indenting things around.
1, I keep getting the following message;
Message malformed: extra keys not allowed @ data[‘data’]
2, I don’t even know if this correct or not?
Heres the esphome led’s code

esphome:
  name: kitchen-colour-leds
  
esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxx"

ota:
  password: "xxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "xxxxxxxxxxxxxxxxxxxxxxx"
    password: "xxxxxxxxxxxxxxxxxxxxxxx"

captive_portal:
    
sensor:
  - platform: wifi_signal
    name: "WiFi Signal $devicename"
    update_interval: 60s
output:
  - platform: ledc
    pin: 26
    frequency: 25000Hz
    id: ledc_cw
  - platform: ledc
    pin: 18
    frequency: 25000Hz
    id: ledc_ww
  - platform: ledc
    pin: 19
    frequency: 25000Hz
    id: ledc_r
  - platform: ledc
    pin: 23
    frequency: 25000Hz
    id: ledc_g
  - platform: ledc
    pin: 05
    frequency: 25000Hz
    id: ledc_b
    
light:
  - platform: rgbww
    name: "Kitchen Colour LEDs"
    id: kcl2
    warm_white: ledc_ww
    cold_white: ledc_cw
    red: ledc_r
    green: ledc_g
    blue: ledc_b
    cold_white_color_temperature: 5000K
    warm_white_color_temperature: 3000K
    color_interlock: true
    effects:
      - lambda:
          name: Slow Rainbow
          update_interval: 16s
          lambda: |-
            static int state = 0;
            auto call = id(kcl2).turn_on();
            call.set_transition_length(15000);
            if (state == 0) {
              call.set_rgb(1.0, 0.0, 0.0);
            } else if (state == 1) {
              call.set_rgb(1.0, 0.5, 0.0);
            } else if (state == 2) {
              call.set_rgb(1.0, 1.0, 0.0);
            } else if (state == 3) {
              call.set_rgb(0.5, 1.0, 0.0);
            } else if (state == 4) {
              call.set_rgb(0.0, 1.0, 0.0);
            } else if (state == 5) {
              call.set_rgb(0.0, 1.0, 0.5);
            } else if (state == 6) {
              call.set_rgb(0.0, 1.0, 1.0);
            } else if (state == 7) {
              call.set_rgb(0.0, 0.5, 1.0);
            } else if (state == 8) {
              call.set_rgb(0.0, 0.0, 1.0);
            } else if (state == 9) {
              call.set_rgb(0.5, 0.0, 1.0);
            } else if (state == 10) {
              call.set_rgb(1.0, 0.0, 1.0);
            } else if (state == 11) {
              call.set_rgb(1.0, 0.0, 0.5);
            }
            call.perform();
            state++;
            if (state == 12)
              state = 0;
             
      - strobe:
      - strobe:
          name: Strobe Effect With Custom Values
          colors:
            - state: true
              brightness: 100%
              red: 100%
              green: 90%
              blue: 0%
              duration: 500ms
            - state: false
              duration: 250ms
            - state: true
              brightness: 100%
              red: 0%
              green: 100%
              blue: 0%
              duration: 500ms

      - flicker:
      - flicker:
          name: Flicker Effect With Custom Values
          alpha: 95%
          intensity: 10.5%
                  
                  
      - random:
      - random:
          name: Random Effect With Custom Values
          transition_length: 5s
          update_interval: 7s


              
      - pulse:
      - pulse:
          name: "Fast Pulse"
          transition_length: 0.5s
          update_interval: 0.5s
      - pulse:
          name: "Slow Pulse"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s
  

Here’s what I’ve been trying to write in the automation. I started with Automations wizard within HA, but as it didn’t have the option I wanted to switch the effect Off and only switch On the CW led’s, I decided to try having a go at the yaml myself, possibly a mistake to do so, but I’ve tried.

alias: Kitchen motion Catch
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 0a0d2e74956722bc0f62949fbab52519
    entity_id: binary_sensor.kitchen_pir
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition: []
action:
  - device_id: bb944cdbee03bad8b30a1c6799c95d3d
    domain: light
    entity_id: light.kitchen_colour_leds
    data: {}
      effects: "None"
      brightness: 255
      rgbww_color: [0,0,0,255,0]
    
mode: restart

I’d be really grateful for some advice.

Darren.

Darren,
I’m not sure what the issue is, but here are a few things I would try:

Remove the “for” in the motion detector trigger or it will wait that long before it changes.
Set the trigger to specify the state or it may trigger when the motion goes away. (I’m not sure about this one because I’ve never seen the trigger type “motion”; I always use “state” with my motion sensors.)

Test the action section with a few changes:
Remove the {}
Try just the brightness and RGB sections by themselves. Try removing the effects part.

You can also debug that more easily in the developer tab where you can test out the service call light.turn_on.

Now that I think of it, your action doesn’t have a service call! I think you might be missing service:light.turn_on.

-David

Hi Guys,

@dbs Hi David,

Thank you for your advice.

I’m to eager, so thank you for pointing this out. With taking a few steps back, doing and testing one item at a time, then adding to that, has put me on the right track.
I now have this working, with the effects stopping and turning On the white leds when motion/presence is detected.
I forgot to state before these LEDs are the 5 in 1 RGB-CW-WW.

Next is to try and configure it to revert back to how it was before the motion was detected, ie go back to running whatever effect was being used prior to detection. I can’t / still looking for this, but I think I remember something that’s used for media devices, like when the doorbell was pressed, or the phone rang Ha would take a “snapshot”, or something like that.
Anyway, thank you for ‘slowing me down’, thus getting one thing to work at a time.
The ESPHome / LED control sketch is the same as above. Here’s the automation sketch so far, for the benefit of other learners wishing to do similar;

alias: Kitchen motion Catch
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
condition: []
action:
  - service: light.turn_on
    entity_id: light.kitchen_colour_leds
    data:
      effect: None
      brightness: 255
      kelvin: 6500
      entity_id: light.kitchen_colour_leds
mode: restart

Before I opted to use;
kelvin: 6500
I had used the follow, but this resulted in not quite the full white, just off.

rgbww_color:
        - 0
        - 0
        - 0
        - 255
        - 0

Darren.

Darren – That’s a really good idea. What I think you are looking for is scene.create and then scene.turn_on. Scenes - Home Assistant

I think I’m going to add that to some things I have as well! Thank you for suggesting that.

I have an automation that flashes some lights in the kids’ rooms when it is time for them to be in bed, but I don’t want it to leave the lights on if they were off. So I did this:

- alias: Flash kids hug lights
  id: flash_kids_hug_lights
  trigger:
    platform: event
    event_type: kids_hug_event
  action:
    - variables:
        p_on: "{{ states('light.p_desk_light') }}"
        p_floor_on: "{{ states('light.p_floor_lamp') }}"
        e_on: "{{ states('light.e_desk_light') }}"
    - repeat:
        count: "2"
        sequence:
          - service: light.turn_on
            entity_id: light.p_desk_light, light.e_desk_light, light.p_floor_lamp
            data:
              transition: 0
              brightness_pct: 30
          - delay: "00:00:00.7"
          - service: light.turn_on
            entity_id: light.p_desk_light, light.e_desk_light, light.p_floor_lamp
            data:
              transition: 0
              brightness_pct: 60
          - delay: "00:00:00.7"
    - service: light.turn_on
      entity_id: light.p_desk_light, light.e_desk_light, light.p_floor_lamp
      data:
        transition: 0
        brightness_pct: 100
    - if: "{{ p_on != 'on' }}"
      then:
        - service: light.turn_off
          entity_id: light.p_desk_light
    - if: "{{ e_on != 'on' }}"
      then:
        - service: light.turn_off
          entity_id: light.e_desk_light
    - if: "{{ p_floor_on != 'on' }}"
      then:
        - service: light.turn_off
          entity_id: light.p_floor_lamp

But the whole setting variables based on whether they are on and then adjusting them afterwards could be replaced by simply doing a scene.create with those three entities and then scene.turn_on at the end, which is much more elegant!

-David

Hi David @dbs ,

I’d just been playing around and found this;

alias: Kitchen motion Catch
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
condition: []
action:
  - service: scene.create
    data:
      scene_id: leds before motion
      snapshot_entities: light.kitchen_colour_leds
  - service: light.turn_on
    entity_id: light.kitchen_colour_leds
    data:
      effect: None
      brightness: 255
      kelvin: 6500
      entity_id: light.kitchen_colour_leds
mode: restart


alias: Kitchen motion Catch
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
condition: []
action:
  - service: scene.turn_on
    target:
      entity_id: leds before motion

But when I save an go back into automations yaml I get the following and it no-longer works?;

alias: Kitchen motion Catch
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
condition: []
action:
  - service: scene.create
    data:
      scene_id: leds before motion
      snapshot_entities: light.kitchen_colour_leds
  - service: light.turn_on
    entity_id: light.kitchen_colour_leds
    data:
      effect: None
      brightness: 255
      kelvin: 6500
      entity_id: light.kitchen_colour_leds
mode: restart

I’m guessing it’s something to do wit the double brackets looking at the following post from 123 Taras?

Darren.

Daren,
I doubt it’s the double brackets. If you are using the automations GUI, you can test out individual parts with the little … menu next to them and see what each part does.

Two other things:

  1. If you trigger on platform:state you probably want to include “to:on” so that it only triggers when the motion goes “on”.
  2. The “Kitchen motion Catch” is missing from the second one. Was that intentional?
    -David

Hi David
Is there one missing, I can’t see one missing?

Anyway, I’ve been tinkering again (also with the automation GUI), and as you read this, please don’t think I’m ranting. I’m not, I’m just typing while I’m running through the process. I’m just very lost at the moment.

If I use the Automations GUI, it doesn’t give me the option after taking a scene snapshot, and when the motion is changed from On to Off, to then turn On the scene afterwards.
It adds the On - Off and the Off - On together at the top, then lower down it’s the same with the scene snapshot and scene turn on, rather then the order of "Off - On - scene snapshot - do x with leds - On - Off - turn scene snapshot On?

Going back to the yaml, If I write the sketch very similar to the following, then I save it, reboot, then go back to check it, half of it’s gone;

Here’s the example;

So I go into Settings, Automations & Scenes, click on the Pen to the right of my automation, click the 3 dots at the top and select Edit in yaml, and I type the following which is very similar, but for my usage;

alias: Kitchen motion Catch
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
    from: "off"
    to: "on"
condition: []
action:
  - service: scene.create
    data:
      scene_id: leds before motion
      snapshot_entities: light.kitchen_colour_leds
  - service: light.turn_on
    entity_id: light.kitchen_colour_leds
    data:
      effect: None
      brightness: 255
      kelvin: 6500
      entity_id: light.kitchen_colour_leds
mode: restart
alias: Kitchen motion Catch
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
    from: "on"
    to: "off"
condition: []
action:
  - service: scene.turn_on
    target
      entity_id: scene.leds before motion

I save this this then reboot, but when I go back to check, its changed to the following;

alias: Kitchen motion Catch
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
    from: "off"
    to: "on"
condition: []
action:
  - service: scene.create
    data:
      scene_id: leds before motion
      snapshot_entities: light.kitchen_colour_leds
  - service: light.turn_on
    entity_id: light.kitchen_colour_leds
    data:
      effect: None
      brightness: 255
      kelvin: 6500
      entity_id: light.kitchen_colour_leds
mode: restart

I’ve tested this and it doesn’t work. I’ve read all the relevant documents many times, and looked at many examples. I just don’t see what I’m missing.

I’m grateful for your help.

Darren.

Darren – It looks like you are trying to put two automations in one. The second alias: Kitchen motion catch needs to be in a separate automation called “Kitchen motion off” or something. I suspect it’s removing it because the GUI only allows one automation per automation. :slight_smile:

-David

Hi David @dbs ,

Ah, I see now. Thank you for pointing out what’s not documented. I don’t know why it doesn’t state that in the doc’s. When I start on HA about a year ago, I did ask on the forum “is there a book or any good place to learn yaml”, and I was basically told, it’s just something you learn as you go.

Sorry I didn’t get back sooner, but I had nothing but problems yesterday with the esp device droping off my system, so reconfigured it, nothing, reconfigured it again. I did this many times. When it did appear again, I had 3 of entities for the on device. An absolute nightmare of a day.

I’ve update everything HA and all Addons and restarted and rebooted.

I’ve gone into the Automations GUI (settings, automations & scenes), and used the wizard to setup new automations.

I’ve setup the PIR detect to take a snapshot of the “leds before motion” is detected. Great.

Now when I setup up the second automation for when the PIR doesn’t detect motion, and to turn On the scene created before motion was detected, Nothing is found for the target. If I go into the yaml to enter the target entity id manually I get error for Message Malformed;

Darren.

Hi All, @dbs,

Am I doing something wrong by doing all of this in the automations tab, or should I be adding something into the scenes tab. That said, I’m creating the scene on-the-fly, so surely the scene should be created when there is a detection?

I know that the detector is detecting as I can see this in the automations stating “detected 10 sec ago”, but thats all it’s doing. It’s not changing the colour of the leds. Here’s the current automation yaml on the detect side.

alias: leds before motion
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_pir
    from: "off"
    to: "on"
condition: []
action:
  - service: scene.create
    data:
      scene_id: leds before motion
      snapshot_entities: light.kitchen_colour_leds_1
  - service: light.turn_on
    data:
      kelvin: 6500
      brightness: 255
      effect: None
    target:
      entity_id: light.kitchen_colour_leds_1
mode: restart

Is the problem with what I’m calling the target entity id of the leds, heres the current esphome sketch for the leds;

esphome:
  name: kitchen-colour-leds-1

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "guess"

ota:
  password: "guess"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Kitchen-Colour-Leds-1"
    password: "guess"

captive_portal:
    
sensor:
  - platform: wifi_signal
    name: "WiFi Signal $devicename"
    update_interval: 60s
output:
  - platform: ledc
    pin: 26
    frequency: 25000Hz
    id: ledc_cw
  - platform: ledc
    pin: 18
    frequency: 25000Hz
    id: ledc_ww
  - platform: ledc
    pin: 19
    frequency: 25000Hz
    id: ledc_r
  - platform: ledc
    pin: 23
    frequency: 25000Hz
    id: ledc_g
  - platform: ledc
    pin: 05
    frequency: 25000Hz
    id: ledc_b
    
light:
  - platform: rgbww
    name: "kitchen colour leds 1"
    id: kcl2
    warm_white: ledc_ww
    cold_white: ledc_cw
    red: ledc_r
    green: ledc_g
    blue: ledc_b
    cold_white_color_temperature: 5000K
    warm_white_color_temperature: 3000K
    color_interlock: true
    effects:
      - lambda:
          name: Slow Rainbow
          update_interval: 16s
          lambda: |-
            static int state = 0;
            auto call = id(kcl2).turn_on();
            call.set_transition_length(15000);
            if (state == 0) {
              call.set_rgb(1.0, 0.0, 0.0);
            } else if (state == 1) {
              call.set_rgb(1.0, 0.5, 0.0);
            } else if (state == 2) {
              call.set_rgb(1.0, 1.0, 0.0);
            } else if (state == 3) {
              call.set_rgb(0.5, 1.0, 0.0);
            } else if (state == 4) {
              call.set_rgb(0.0, 1.0, 0.0);
            } else if (state == 5) {
              call.set_rgb(0.0, 1.0, 0.5);
            } else if (state == 6) {
              call.set_rgb(0.0, 1.0, 1.0);
            } else if (state == 7) {
              call.set_rgb(0.0, 0.5, 1.0);
            } else if (state == 8) {
              call.set_rgb(0.0, 0.0, 1.0);
            } else if (state == 9) {
              call.set_rgb(0.5, 0.0, 1.0);
            } else if (state == 10) {
              call.set_rgb(1.0, 0.0, 1.0);
            } else if (state == 11) {
              call.set_rgb(1.0, 0.0, 0.5);
            }
            call.perform();
            state++;
            if (state == 12)
              state = 0;
             
      - strobe:
      - strobe:
          name: Strobe Effect With Custom Values
          colors:
            - state: true
              brightness: 100%
              red: 100%
              green: 90%
              blue: 0%
              duration: 500ms
            - state: false
              duration: 250ms
            - state: true
              brightness: 100%
              red: 0%
              green: 100%
              blue: 0%
              duration: 500ms

      - flicker:
      - flicker:
          name: Flicker Effect With Custom Values
          alpha: 95%
          intensity: 10.5%
                  
                  
      - random:
      - random:
          name: Random Effect With Custom Values
          transition_length: 5s
          update_interval: 7s


              
      - pulse:
      - pulse:
          name: "Fast Pulse"
          transition_length: 0.5s
          update_interval: 0.5s
      - pulse:
          name: "Slow Pulse"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s
    

I’d be grateful for some help with this.

FINALLY…

I’ve figured it out.

I’ve lost count of how many times I’ve read the docs on scenes.

In my scene id, I’d used spaces when your not meant to. You have to use lower case letter (which I had), but instead of spaces you use underscores.

Thank you @dbs for your help.

I hope this post may help others.

Automation 1 to detect motion and stop leds effect;

alias: kitchen leds catch
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 0a0d2e74956722bc0f62949fbab52519
    entity_id: binary_sensor.kitchen_pir
    domain: binary_sensor
condition: []
action:
  - service: scene.create
    data:
      scene_id: leds_before_motion_1
      snapshot_entities: light.kitchen_colour_leds_1
  - service: light.turn_on
    data:
      kelvin: 6500
      brightness: 255
      effect: None
    target:
      entity_id: light.kitchen_colour_leds_1
mode: restart

Automation 2 when no detection revert to leds & effect;

alias: kitchen leds reload
description: ""
trigger:
  - type: no_motion
    platform: device
    device_id: 0a0d2e74956722bc0f62949fbab52519
    entity_id: binary_sensor.kitchen_pir
    domain: binary_sensor
condition: []
action:
  - service: scene.turn_on
    data: {}
    target:
      entity_id: scene.leds_before_motion_1
mode: restart

Awesome! :slight_smile: