Help with service_template (SOLVED)

- service_template: >
  {% if is_state('binary_sensor.door_window_sensor_158d0001fd55d5', 'on') %}
  rest_command.set_climate_to_on
  {% elif is_state('binary_sensor.door_window_sensor_158d0001fd55d5', 'off') %}
  climate.set_operation_mode
  entity_id: climate.panasonic_aircon
  data:
    operation_mode: 'off'
  {% else %}
  script.dummy_script
  {% endif %}

The above works in the template renderer, but I get an error on validating the config.

Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
  in "/config/automations.yaml", line 25, column 8

Solution:

- service_template: >-
    {%- if is_state('binary_sensor.door_window_sensor_158d0001fd55d5', 'on') -%}
    rest_command.set_climate_to_on
    {%- elif is_state('binary_sensor.door_window_sensor_158d0001fd55d5', 'off') -%}
    climate.set_operation_mode
    entity_id: climate.panasonic_aircon
    data:
      operation_mode: 'off'
    {%- else -%}
    # Do Nothing
    {%- endif -%}

You need minus signs

service_template: >-

and

{%- blah blah -%}

1 Like

Why the minus sign

what manual should I read

Im wing each time donā€™t unstand why

Itā€™s too do with the carriage returns turning into spaces or something

That post isnā€™t a solution, that template wonā€™t work. If the binary sensor is off it will spit out one long string, not the ordered dict you actually need.

Also, you didnā€™t need all the minus signs, the second one validates in the config checker because you indented the template by 2 spaces, but on the first one you didnā€™t.

1 Like

should i put

climate.set_operation_mode
entity_id: climate.panasonic_aircon
data:
  operation_mode: 'off'

into a separate script?

Yeah, so the template should be

- service_template: >
    {% if is_state('binary_sensor.door_window_sensor_158d0001fd55d5' , 'on') %}
      rest_command.set_climate_to_on
    {% else %}
      script.climate_panasonic_off
    {% endif %}

(note that a binary sensor can only be on or off, so there was no need for the additional elif)

Then script:

climate_panasonic_off:
  sequence:
    - service: climate.set_operation_mode
      entity_id: climate.panasonic_aircon
      data:
        operation_mode: 'off'

Hope this helps :+1:

1 Like

I want the template to do nothing if the binary sensor is unavailable.
So I either need #Do Nothing or script.do_nothing

Fair enough, you can just have a blank script there then.

@kiwijunglist Iā€™m trying to do a similar thing with the mqtt.publish command but am getting the same config error; found character ā€˜%ā€™ that cannot start any token

- alias: Entry Door status via MQTT
  hide_entity: true
  trigger:
    platform: state
    entity_id: binary_sensor.entry_door_w
  action:
    service: mqtt.publish
    data:
      topic: "entry_door/status"
      payload_template: {%- if is_state("binary_sensor.entry_door_w", "off") -%}Closed{%- else -%}Open{%- endif -%}
      retain: false

Iā€™ve tried by template in the developer console using the dev-mqtt page and it works. I added the minus signs by the % symbols but it made no difference. Any feedback would be appreciated.

You need to wrap the payload template in quotes, or make it multi-lineā€¦

payload_template: "{% if is_state('binary_sensor.entry_door_w' , 'off') %}Closed{% else %}Open{% endif %}"

Or

payload_template: >
  {% if is_state('binary_sensor.entry_door_w' , 'off') %}Closed
  {% else %}Open{% endif %}
1 Like

Thank you! Quotes did not work but multi-line did. Awesome.

1 Like

@anon43302295

Any chance you could show me how to multi-line this one? Still trying to get my head around these templates.

- platform: template
  sensors:
    last_doorbell_push:
      friendly_name: "Last Doorbell"
      # unit_of_measurement: 'degrees'
      value_template: "{{as_timestamp(states.automation.doorbell_pushed.attributes.last_triggered) | timestamp_custom("%H:%M - %d/%m/%y")}}"

Codes works in template editor but not in config checker stating ā€œfound character ā€˜%ā€™ that cannot start any tokenā€.

1 Like

It doesnā€™t need to be multiline, all the quotes that are inside the template should be single quotes, leaving double quotes only at either end.

Should be thisā€¦

value_template: "{{as_timestamp(states.automation.doorbell_pushed.attributes.last_triggered) | timestamp_custom('%H:%M - %d/%m/%y')}}"

Note that the first quote mark Iā€™ve changed is immediately before a percent sign? Thatā€™s why the error was talking about the percent sign, because the way you had it it read the first " then started reading the template, then got to the second " and stopped, and then it found a percent sign it wasnā€™t expecting.

Now when it reads the template it will get all of it because that is all there is between the double quotes.

Hope this helps with your understanding :smile:

2 Likes

Thank you so much, especially for the detailed explanation. That really helps out hardware guys like me!

1 Like

multiline this would be ( and changed the template itself to state_attr(), which is not necessary, though safer and for my eyes more readable ) :

value_template: >
  {{as_timestamp(state_attr('automation.doorbell_pushed','last_triggered')) | timestamp_custom("%H:%M - %d/%m/%y")}}

depending on your preferences for long lines, or you editor breaking at 90 characters or so, you could easily write it like this too:

value_template: >
  {{as_timestamp(state_attr('automation.doorbell_pushed','last_triggered'))
     | timestamp_custom("%H:%M - %d/%m/%y")}}
1 Like

Awesome. Thanks for your input.

My code just doesnt work and I canā€™t figure out why

      - type: entities
        show_header_toggle: false
        entities:

          - climate.midea_dehumi_1

          - type: call-service
            icon: mdi:power
            name: Power the device
               
            action_name: On-Off
            service: 
              service_template: >
                {% if is_state('climate.midea_dehumi_1', 'off') %}
                  climate.turn_on
                {% else %}
                  climate.turn_off
                {% endif %}
            
            service_data:
              entity_id: climate.midea_dehumi_1

I want to have the entityā€™s service change depending on its state. If itā€™s on to call the service turn_off and viceversa.
The if statement works in the template editor but it doesnt when i apply it in the ui-lovelace.yaml.
What is wrong?

You canā€™t write a template inside a lovelace service call.

so my other option is to call a script or abandon the idea?