Easier said than done

Hello all,

I’m a new HA user, running 0.65.5 smoothly on a Synology with Docker.

I’m starting with some basic automations but I find it very hard to do anything at all.

The task I’m trying to accomplish seems trivial though:
I’d like to turn on a light (Yeelight RGB) with a specific color and in low brightness when there is a movement (Xiaomi motion sensor linked to the Xiaomi Gateway), only between 23PM and before 6AM, and turn it off if there is no movement for 1.5 minutes.

If I manually switch on the light, I do not wish to have an automatic “turn off”.
The 1.5 minutes timer should only start if the light has been turned on by the automation.

I have found the following code which seems to do what I want and I have adjusted it to add the RGB color and the brightness.
It turns on the light as expected, but I get an error when it tries to turn the light off, because of the added parameters

- alias: Gateway light on/off based on motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_id
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_sensor_id
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  condition:
    condition: time
    after: '23:00'
    before: '06:00'
  action:
action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      light.turn_on
      {% elif trigger.to_state.state == "off" %}
      light.turn_off
      {% endif %}
    data_template:
      entity_id: light.pilar
      brightness: 1
      rgb_color: [255,0,255] 

The error I receive is:
Invalid service data for light.turn_off: extra keys not allowed @ data[‘brightness’]. Got '1’
extra keys not allowed @ data[‘rgb_color’]. Got [‘255’, ‘0’, ‘255’]

I understand the error is caused by the additional RGB/brightness parameters, however I have no idea how to do such a scenario differently and as simple as possible.

I hope someone out there will be able to give me some pointers :slight_smile:

Thank you

use data not data_template. This is one of mine

    - service: light.turn_on
      entity_id: 
        - light.kitchen_leds
        - light.kitchen2_leds
        - light.kitchen3_leds
      data:
        effect: solid
        brightness: 255
        rgb_color: [255,146,36]

I thought I needed to use data_template because I was in a service_template, didn’t I ?

I Don’t think so, I am no expert. Try it and let me know :slight_smile:

Completely separate so follow @MarkR’s advice.

the thing is I know how to turn a light on or off, but if you look at my automation, it uses a template.

Turning on the light with the right RGB/Brightness works.
Turning off the light doesn’t work because of the RGB/Brightness parameters.

Turnoff won’t accept additional options and the triggers are confusing (as it will trigger either the binary is set on or is set off for 1.5 min). Try this (there’s an automation and two separate scripts). Whenever a motion is detected the light will turn on and the timer of 1.5 min will be reset after each trigger.

- alias: Gateway light on/off based on motion
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_id
      to: 'on'
  condition:
    condition: time
    after: '23:00:00'
    before: '06:00:00'
  action:
    - service: script.turn_on
      entity_id: script.timer_on_gateway

timer_on_gateway:
  sequence:
    - service: script.turn_off
      entity_id: script.timer_off_gateway
    - service: script.turn_on
      entity_id: script.timer_off_gateway
    - service: light.turn_on
      data_template:
        entity_id: light.pilar
        brightness: 1
        rgb_color: [255,0,255]
timer_off_gateway:
  sequence:
    - delay: '00:01:30'
    - service: light.turn_off
      entity_id: light.pilar

Yeh, you set a turn on one with the extra parameters and then just straight off one.

You could also just have one automation that turns on the light and an input boolean (e.g. input_boolean.motion_lights), and then another automation that triggers on the motion sensor going to “off” for 1.5 min, has a condition that the boolean is on, and then turns off the light and the boolean.

1 Like