Issues with service_template

I have an automation for connecting a slider to a light’s brightness. It works fine like this:

id: office_brightness
alias: Office Brightness
trigger:
  platform: state
  entity_id: input_slider.office_light
action:
  - service: light.turn_on
    data_template:
      entity_id: light.patricks_overhead
      brightness: '{{ trigger.to_state.state | int }}'

I wanted it to turn the light off if the value went to 0, so I tried the following

id: office_brightness
alias: Office Brightness
trigger:
  platform: state
  entity_id: input_slider.office_light
action:
  - service_template: >-
    {% if is_state('trigger.to_state', '0') %}
      light.turn_off
    {% else %}
      light.turn_on
    {% endif %}
    data_template:
      entity_id: light.patricks_overhead
      brightness: '{{ trigger.to_state.state | int }}'

I get an error when I check the config:

found character '%' that cannot start any token in "/home/hass/.homeassistant/automations/office_brightness.yaml", line 8, column 6

That location equates to the first % just before the if. The above template code does parse fine in the dev template tester.

The below code works in my config as far as not throwing the parsing error, but it doesn’t actually work. Note that the only change is to remove the "- " from the beginning of “service_template”.

id: office_brightness
alias: Office Brightness
trigger:
  platform: state
  entity_id: input_slider.office_light
action:
  service_template: >-
    {% if is_state('trigger.to_state', '0') %}
      light.turn_off
    {% else %}
      light.turn_on
    {% endif %}
    data_template:
      entity_id: light.patricks_overhead
      brightness: '{{ trigger.to_state.state | int }}'

I’m not sure what the actual problem is here. Can someone help me understand where the errors are coming from?

I’ve never seen anything in the documentation to support this, but I’ve never been able to run a service_template and a data_template successfully in the same automation.

I would implement this as 2 automations. One for the zero state, and a different one for other states.

@treno

Interesting you have not been able to run both.

I have the following automation that works.

- alias: iOS app replace one big
  trigger:
    platform: event
    event_type: ios.notification_action_fired
  action:
   service_template: >-
     {% if (trigger.event.data.actionName.split('_')[1] == 'OPEN') %}
       cover.open_cover
     {% elif (trigger.event.data.actionName.split('_')[1] == 'CLOSE') %}
       cover.close_cover
     {% else %}
     {% endif %}
   data_template:
    entity_id: >-
     {% if (trigger.event.data.actionName.split('_')[2] == 'FIRST') %}
      cover.myq_first_bay, cover.first_bay
     {% elif (trigger.event.data.actionName.split('_')[2] == 'SECOND') %}
      cover.myq_second_bay,cover.second_bay
     {% elif (trigger.event.data.actionName.split('_')[2] == 'THIRD') %}
      cover.myq_third_bay, cover.third_bay
     {% elif (trigger.event.data.actionName.split('_')[2] == 'ALL') %}
      group.garage
     {% else %}
      cover.test
     {% endif %}

Try changing is_state('trigger.to_state', '0') to is_state('trigger.to_state', '0.0') in the version that doesn’t do anything but also doesn’t error. Other than that, I can’t see anything wrong.

I actually did try that at one point, but no change. Even if that were the case though (that it was comparing to the wrong value), then it should still call light.turn_on as that’s the else clause, but even that didn’t work… the slider didn’t control the brightness of the light. In the code at the very top, it does.

This kind works for me. The light turns on and sets brightness based on the slider. However the turn off service does not allow the brightness key and therefor wont turn off the light.

You would need to add another template to not include the brightness key if you are turning off the light.

- alias: Office Brightness
  trigger:
    platform: state
    entity_id: input_slider.test
  action:
    service_template: >-
      {% if is_state("input_slider.test", "0.0") %}
        light.turn_off
      {% else %}
        light.turn_on
      {% endif %}
    data_template:
       entity_id: light.test_light
       brightness: '{{ trigger.to_state.state | int }}'

Why not simply use custom_ui?

@duckpuppy

This ended up working for me.

- alias: Office Brightness
  trigger:
    platform: state
    entity_id: input_slider.brightness
  condition:
    - condition: numeric_state
      entity_id: input_slider.brightness
      above: 0.0
  action:
    service: light.turn_on
    data_template: 
       entity_id: light.light
       brightness: '{{ trigger.to_state.state | int }}'

- alias: Office Off
  trigger:
    platform: state
    entity_id: input_slider.brightness
    to: '0.0'
  action:
    service: light.turn_off
    entity_id: light.light
1 Like

Wow. Did not know all that was available. Thanks/

Mainly… because I had no idea that it was a thing, but I will be looking at it for other things.

I ended up using AppDaemon, though. It was fun. I’m sure there’s some optimizations I can make, but I wanted to learn AppDaemon anyway, and this seemed like a good place to start. I love that I can live edit automations.

import appdaemon.appapi as appapi

#
# BrightnessSlider App
#
# Args:
#   light: The full entity name of the light to link with the slider
#   slider: The full entity name of the slider to link with the light
#

class BrightnessSlider(appapi.AppDaemon):

  def initialize(self):
      self.handle_slider = self.listen_state(self.update_brightness, self.args["slider"])
      self.handle_external = self.listen_state(self.update_slider, self.args["light"], attribute='brightness')

  def update_brightness(self, entity, attribute, old, new, kwargs):
      old_value = int(float(old or 0))
      new_value = int(float(new or 0))
      if new_value != old_value:
        # TODO: Set a new device named "appdaemon.brightness_slider_<light>" to the brightness value, and maybe one ending in "_pct" to the percentage value
        if new_value == 0:
            self.turn_off(self.args["light"])
            self.log("Turning {} off".format(self.args["light"]))
        else:
            self.turn_on(self.args["light"], brightness = new_value)
            self.log("Setting {} brightness to {}".format(self.args["light"], new_value))

  def update_slider(self, entity, attribute, old, new, kwargs):
      old_value = int(float(old or 0))
      new_value = int(float(new or 0))
      if new_value != old_value:
        status = self.set_state(self.args["slider"], state = new_value)
        self.log("Setting {} to {}".format(self.args["slider"], new_value))
1 Like