Set a specific color in the light of the xiaomi gateway in an automation

Hi, I have done this automation to turn on the light of the xwhen gw at the opening of the door of the house and it works perfectly. What I can not do is pass a color of my choice.
If I put this rows at the end of the code

service: ....
date:
   entity_id: ...
   rgb_color: ..

when the door is opened, the LED lights up in the right color, but then it does not go off when I close the door.
How should I pass the right color and make sure that the atomization works correctly?
This codes are currently in use

indent preformatted text by 4 spaces

- alias: Accensione/Spegnimento luce con apertura porta
  trigger:
  - platform: state
    entity_id: binary_sensor.door_window_sensor_158d00023ea03f
    to: 'on'
  - platform: state
    entity_id: binary_sensor.door_window_sensor_158d00023ea03f
    to: 'off'
  condition:
    condition: time
    after: '20:00'
    before: 06:00
  action:
    service_template: '{% if trigger.to_state.state == "on" %} homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %} homeassistant.turn_off {% endif %}'
    entity_id: light.gateway_light_7811dce210d1

Please use code block highlighting as indentation is important in YAML.

How can i do?

Done!

Anyone can help?

Why not to make second pretty same automation with another trigger (door off) and another action?
Your way to automate light is more nice, but I do not know if it is reasonoble to do, when there is easier way.

What do you think?

Or write two scripts one for turning on, secon for turning of. And then use your automation to choose whitch script to use.

And here is script

alias: test
  sequence:
  - entity_id: light.gateway_light_7c49ebb1dfcb
    data:
      brightness: 15
      rgb_color: [255, 196, 89]
    service: light.turn_on

Not sure if it will fix it but perhaps change your original code as most of the templates I see have the statements on separate lines. On my phone at the moment so hard to show you

EDIT: maybe…

- alias: Accensione/Spegnimento luce con apertura porta
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d00023ea03f
      to: 'on'
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d00023ea03f
      to: 'off'
  condition:
    condition: time
    after: '20:00'
    before: 06:00
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %} homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %} homeassistant.turn_off 
      {% endif %}
    entity_id: light.gateway_light_7811dce210d1

EDIT 2: I just noticed that the indentation in the triggers is wrong. Updated mine above

So maybe try your original action, but take note of the extra 2 spaces I put in front of your triggers

  action:
    service_template: '{% if trigger.to_state.state == "on" %} homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %} homeassistant.turn_off {% endif %}'
    entity_id: light.gateway_light_7811dce210d1

Tnx for reply. My problem isn’t the automation that it works…my problem is set a custom color to my automation. If i use my orignal code or your (ive tried and it works)the lights is white (default), but i’d like to change.

Yes…you’re right…at the end this seems to be the best way to fix my problem.

Right, so I wasn’t mistaken. Well to change the colour simply use the code I deleted in my other post but substitute the 255,255,255 for different numbers to get the colour you want. You can look on Google to get the numbers for any colour in the spectrum.

 data:
    entity_id: light.gateway_light_7811dce210d1
    rgb_color: [255,255,255]

Thanks for reply. Ive tried but with it the light turn to on with correct color but when close the door the light dont turn off

Instead of using the turn_off service, could you just set it to a new color of rgb_color: [0,0,0] to turn the light off?

Where i can put the code, inside the service template?

Bit confused - what works, and what doesn’t?

If your issue is that the light doens’t change colour that is probably because you are using homeassistant.turn_on/off rather than light.turn_on…?

(I believe homeassistant.turn_on only takes entity_id as a parameter)

Hello, the problem is i can’t sent a custom color to the light, if i use data:
entity_id: light.gateway_light_7811dce210d1
rgb_color: [255, 0, 0]
The light turn on with the correct color but when I close the door the light does not go out