Service_template error

Seeing the rest of the automation would definitely help get the bigger picture of what they’re trying to accomplish.

I have a feeling it would throw an error and the automation would fail.

I agree. There probably should be an else that sets the options to something valid.

… but we really need to see the rest of this automation.

The rest of this automation is just a button click.

  trigger:
  - event_data:
      click_type: single
      entity_id: binary_sensor.switch_***************
    event_type: xiaomi_aqara.click
    platform: event

The idea was to dim lights at evening and night.
Now, with @walrus_parka script i have an error with variables.
found character ‘%’ that cannot start any token
in “/home/homeassistant/.homeassistant/automations.yaml”, line 312, column 6

This line points to

{% set ts = as_timestamp(states.sun.sun.attributes.next_rising) %}

This was my fault. This lines must be indented with 2 spaces.
But now i have one more error
2019-04-13 16:22:48 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.gostinaia_knopka_lenta. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘rgb_color’]

So is the button the only thing that turns that light on or off?

@walrus_parka Yes, the button is the only thing.

I would suggest what @123 mentioned. Regardless of the time we are toggling the light.

Adding an else to the brightness_pct for any other time the button is pressed.

Replaced the last elif with else to the rgb_color since it is already full white. If that is not what is desired, add an else with whatever rgb color is needed.

  - service_template: light.toggle
    entity_id: light.lenta_gostinaia
    data_template:
      brightness_pct: >
        {% set ts = as_timestamp(states.sun.sun.attributes.next_rising) %}
        {% set light = is_state('light.lenta_gostinaia', 'off') %}
        {% if ts -86400 < as_timestamp(now()) and light %}10
        {% elif ts -86400 > as_timestamp(now()) and light %}50
        {% else %}255
        {% endif %}
      rgb_color: >
        {% set ts = as_timestamp(states.sun.sun.attributes.next_rising) %}
        {% set light = is_state('light.lenta_gostinaia', 'off') %}
        {% if ts -86400 < as_timestamp(now()) and light %}[255,132,2]
        {% else %}[255,255,255]
        {% endif %}

I’m stuck again.

  - service_template: light.toggle
    entity_id: light.lenta_gostinaia
    data_template:
      brightness: >
        {%- set ts = as_timestamp(states.sun.sun.attributes.next_rising) -%}
        {%- set light = is_state('light.lenta_gostinaia', 'off') -%}
        {% if ts -86400 < as_timestamp(now()) and light %}127
        {% else %}10
        {% endif %}
      rgb_color: >
        {%- set ts = as_timestamp(states.sun.sun.attributes.next_rising) -%}
        {%- set light = is_state('light.lenta_gostinaia', 'off') -%}
        {% if ts -86400 < as_timestamp(now()) and light %}[{{255|int}},{{255|int}},{{255|int}}]
        {% else %}[{{255|int}},{{132|int}},{{2|int}}]
        {% endif %}

I change script a little, in dev_template i see, what i need:

brightness: >10
rgb_color: >[255,132,2]

But when i try to fire an automation, i have an error:
Error while executing automation automation.gostinaia_knopka_lenta. Invalid data for call_service at pos 1: None for dictionary value @ data[‘rgb_color’]

Well, there is one more problem. light_toggle dont pass rgb_color and brightness. It just toggles light on and off

Apparently there is an issue with passing a list in a template. I have limited time at the moment but I can take a better look at it this later.

If the services page is to be believed, you are able to pass any turn_on parameters with a toggle service call.

If you believe light.toggle has limitations then replace the existing service_template with this one:

service_template: "{{ 'light.turn_on' if is_state('light.lenta_gostinaia', 'off') else 'light.turn_off' }}"

I don’t believe, i tested it. Yes, documentation says that it is possible, but when i fire light.toggle with color and brightness parameters, light switch’s on with previous state.

Does it work when you use this template? It doesn’t use light.toggle and selects either light_turn_on or light.turn_off depending on the state of light.lenta_gostinaia.

service_template: "{{ 'light.turn_on' if is_state('light.lenta_gostinaia', 'off') else 'light.turn_off' }}"

Yes, it works perfectly, but only without passing parameters, because of what @walrus_parka said about issues with lists.

So the problem isn’t the use of light.toggle but the inability to pass a list as a parameter (for rgb_color)?

This is the post I found. It needs to pass the template data to a script and the script will call the rgb data. I don’t have enough coffee yet to do it quickly but I’m working on it.

Good find but Ouch!

I think the issue is that everything is handled as a string so this list [255, 132, 2] is effectively passed as this "[255, 132, 2]" and the receiving end doesn’t like it because it expects a list.

The ‘call a script’ solution works because the list’s elements are passed individually, as strings. However, at the expense of a fair bit of (unavoidable) overhead.

If that is working for you we will go with that. You will need to create a script named set_color in the following example.

Automation service call:

  - service: script.set_color
    data_template:
      brightness: >
        {%- set ts = as_timestamp(states.sun.sun.attributes.next_rising) -%}
        {%- set light = is_state('light.lenta_gostinaia', 'off') -%}
        {% if ts -86400 < as_timestamp(now()) and light %}127
        {% else %}10
        {% endif %}
      colors: >
        {%- set ts = as_timestamp(states.sun.sun.attributes.next_rising) -%}
        {%- set light = is_state('light.lenta_gostinaia', 'off') -%}
        {% set arg = ts -86400 < as_timestamp(now()) and light %}
        {% set r = 255 %}
        {% if arg %}{% set g = 255 %}{% else %}{% set g = 132 %}{% endif %}
        {% if arg %}{% set b = 255 %}{% else %}{% set b = 2 %}{% endif %}
        {{ r | int }},{{ g | int }},{{ b | int }}

Script:

set_color:
  sequence:
  - service_template: "{{ 'light.turn_on' if is_state('light.lenta_gostinaia', 'off') else 'light.turn_off' }}"
    data_template:
      entity_id: light.lenta_gostinaia
      rgb_color: [ "{{ colors.split(',')[0]|int }}", "{{ colors.split(',')[1]|int }}", "{{ colors.split(',')[2]|int }}" ]
      brightness: "{{ brightness }}"

I don’t think, that it’s a list problem, because i tried to do this with lists, and also i tried to change rgb_color to color_name, which goes in text string.

Well, i solved that puzzle with three separate scripts. It’s not very elegant solution, but it works for me.

  - data_template:
      entity_id: '{%- set sca = is_state(''sun.sun'', ''above_horizon'') -%} {%- set
        scb = is_state(''sun.sun'', ''below_horizon'') -%} {%- set light = is_state(''light.test_spot'',
        ''off'') -%} {% if sca and light %}script.light_day {% elif scb and light
        %}script.light_evening {% else %}script.light_off {% endif %}'
    service: script.turn_on

  alias: light_evening
  sequence:
  - data_template:
      entity_id: light.test_spot
      brightness: 1
      rgb_color: [255, 230, 220]
    service: light.turn_on

  alias: light_day
  sequence:
  - data_template:
      entity_id: light.test_spot
      brightness: 100
      rgb_color: [255, 255, 255]
    service: light.turn_on

  alias: light_off
  sequence:
  - data_template:
      entity_id: light.test_spot
    service: light.turn_off

In this case arguments are passed correctly. There is more work to do, because i want to make this scrips universal and pass entity_id from automation to scripts, maybe i’ll do it later. Thank you for your help.