Mess up with service template

First, thanks for comming here and read my text :smiley:
I just came to HASSIO for a week ago, now I try to create a slider that control my rf dimmer and rf button (on/off)

Situation:
Slider at 0, turn off button (rf dimmer will be off)
Slider at 1, rf dimmer at 25% (rf code, broadlink send service)
50%, 75%, 100%


Because RF button and RF dimmer are separately, so if I change slider I should check the input number state, if != 0, I should turn on button and than choose suitable dimmer level.

I wrote an automation like this: (automations.yaml)

- id: '1570810329198'
  alias: Night light rf dimmer (0)
  description: Automation for RF dimmer with slider
  trigger:
  - entity_id: input_number.night_light_brighness
    platform: state
    to: '0'
  condition: []
  action:
  - service: broadlink.send
    data:
      host: 192.168.1.5
      packet: eAB8AA0mKA0NJycNJw0oDA4mKAwoDScNDScNJw0nDScOJigMDiYOAAGTDScNJw4mDiYoDCgNDSccnDQUABdwAAAAAAAAAAAAAAAA=
  - alias: Night light rf dimmer (1)
trigger:
  - entity_id: input_number.night_light_brighness
    platform: state
    to: '1'
  condition: []
  action:
    - service_template: >-
      {% if input_number.night_light_brighness.value == 0 %}
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: eAB6AA4mDiYOJg4mDiYOJigNDQABkg4nDScNJw0mKA0nDQ0nDScnDQ0nKAwoDCgMDicnDScNJw0NJw0nYNJw4mKAwNAAXcAAAAAAAAAAAAAAAAAAA=
        - delay:
          seconds: 2
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: JgBgAAABKZYRFRAW5EDoROBI4EDsQFBI5EQAFJQABKU0RAAxhAAEpTRAADGIAASpKEwANBQAAAAAAAAAA
      {% else %}
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: JgBgAAABKZYRFRAWEBUQFREUERURFRAUEToQORI4EzcTNxE5EDoQOhAUExQQFRAVExMQFhA4AAAAAA
      {% endif %}

And then I got error

Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/automations.yaml”, line 31, column 6

Thanks for helping me.

Your indentation is not correct here:

  - alias: Night light rf dimmer (1)
trigger:
  - entity_id: input_number.night_light_brighness
    platform: state
    to: '1'

Also I’m pretty sure you should only have one service per case in the service template. So doing more than one thing here is not correct:

    - service_template: >-
      {% if input_number.night_light_brighness.value == 0 %}
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: eAB6AA4mDiYOJg4mDiYOJigNDQABkg4nDScNJw0mKA0nDQ0nDScnDQ0nKAwoDCgMDicnDScNJw0NJw0nYNJw4mKAwNAAXcAAAAAAAAAAAAAAAAAAA=
        - delay:
          seconds: 2
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: JgBgAAABKZYRFRAW5EDoROBI4EDsQFBI5EQAFJQABKU0RAAxhAAEpTRAADGIAASpKEwANBQAAAAAAAAAA
      {% else %}
1 Like

You can’t template accross multiple fields. Templates can only be on a single field inside data_template, or other template fields. You are trying to put a list of fields inside your template. This is not possible. Instead, place your actions into 2 separate scripts and fire each script programatically.

script:
  night_light_brightness_zero:
    sequence:
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: eAB6AA4mDiYOJg4mDiYOJigNDQABkg4nDScNJw0mKA0nDQ0nDScnDQ0nKAwoDCgMDicnDScNJw0NJw0nYNJw4mKAwNAAXcAAAAAAAAAAAAAAAAAAA=
        - delay:
          seconds: 2
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: JgBgAAABKZYRFRAW5EDoROBI4EDsQFBI5EQAFJQABKU0RAAxhAAEpTRAADGIAASpKEwANBQAAAAAAAAAA
  night_light_brightness_not_zero:
    sequence:
        - service: broadlink.send
          data:
            host: 192.168.1.5
            packet: JgBgAAABKZYRFRAWEBUQFREUERURFRAUEToQORI4EzcTNxE5EDoQOhAUExQQFRAVExMQFhA4AAAAAA

Your template was incorrectly grabbing a state object. You’re missing the state machine object infront of your entity id (states.[domain].[object_id].state). And you’re incorrecty trying to assess the name ‘value’ from it. I’m not sure where you got this stuff, but you should console the documentation if you don’t know the syntax. On top of that, you need to convert the input_number’s state to the correct type. All states are strings, you’re comparing it to a int, so you need to convert it to an int.

Lastly, your automation has a trigger saying “when it’s the state = 1”, but your template checks for when it’s equal to zero. That portion of the service template will never fire because this will only fire when the state equals zero. So that should be removed.

Then, With @tom_l’s indentation corrections:

- alias: Night light rf dimmer (1)
  trigger:
  - entity_id: input_number.night_light_brighness
    platform: state
  condition: []
  action:
    - service_template: >
        {% if states('input_number.night_light_brighness') | int == 0 %}
          script.night_light_brightness_zero
        {% else %}
          script.night_light_brightness_not_zero
        {% endif %}
1 Like

I will use the script as you said above, but when “night_light_brightness_not_zero”, I have 4 level brightness 25%, 50%, 75%, 100%.
I’ll try to do it myself and post here.
Thanks @tom_l and @petro

I rewrite the code like this.
Now I have a button for on/off and a slider for brightness level.

When I drag slider to 0, switch will off.
When switch is off, I drag slider to any number, the switch will on but not right brightness level

Ex: when switch off, I drag slider to 4 (100%), switch state is on, but actual brightness is just 25% (the dimmer always at 25% when switch on)

I use a delay in action automation, but not work.

My automation for dimmer and switch

#night light rf dimmer
- id: '1570810329198'
  #1 táșŻt cĂŽng táșŻc rf dimmmer trĂȘn tường
  alias: Night light rf dimmer off
  description: Turn off rf dimmer
  trigger:
    entity_id: switch.night_light
    platform: state
    to: 'off'
  action:
    service: switch.turn_off
    entity_id: switch.night_light
  #2 báș­t cĂŽng táșŻc rf dimmmer trĂȘn tường
- alias: Night light rf dimmer on
  description: Turn on rf dimmer
  trigger:
    entity_id: switch.night_light
    platform: state
    to: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.night_light
  #3 thay đổi độ sĂĄng đùn night light
- alias: Night light rf dimmer change brightness level
  description: Change brightness level of night light
  trigger:
    entity_id: input_number.night_light
    platform: state
  condition:
    condition: state
    entity_id: switch.night_light
    state: 'on'
  action:
    service: script.turn_on
    entity_id: script.change_brightness_level
  #khi brightness = 0 thĂŹ chuyển nĂșt sang off
- alias: Switch off when slider = 0
  description: Slider = 0 mean switch off
  trigger:
    platform: template
    value_template: "{{ states('input_number.night_light') | int == 0 }}"
  action:
    service: switch.turn_off
    entity_id: switch.night_light
  #khi brightness != 0 thĂŹ chuyển nĂșt sang on
- alias: Switch on when slider != 0
  description: Slider != 0 also change switch to on
  trigger:
    platform: template
    value_template: "{{ states('input_number.night_light') | int != 0 }}"
  action:
    - service: switch.turn_on
      entity_id: switch.night_light
    - delay: 5
    - service: sript.turn_on
      entity_id: script.change_brightness_level

My script for dimmer

change_brightness_level:
  sequence:
    - service: switch.turn_on
      data_template:
        entity_id: >
          {% set arr_switch = ["switch.night_light_dimmer_25", "switch.night_light_dimmer_50", "switch.night_light_dimmer_75", "switch.night_light_dimmer_100"] %}
          {{ arr_switch[states.input_number.night_light.state | int - 1] }}

My configuration

switch 1:
  - platform: broadlink
    host: !secret broadlink_ip
    mac: !secret broadlink_mac
    type: 'rm2_pro_plus'
    switches:
      # CĂŽng táșŻc đùn ngủ
      night_light:
        friendly_name: "Night Light"
        #Báș­t cĂŽng táșŻc đùn night light
        command_on: 'eAB6AA4mDiYOJg4mDiYOJigNDQABkg4nDScNJw0mKA0nDQ0nDScnDQ0nKAwoDCgMDicnDScNJw0NJw0nDScNJw0nDScnDQ0AAZMNJw4mDScNJygMKAwOJg4mKAwOJigMKA0nDQ0nJw0nDScNDScOJg4mDiYNJw4mKAwNAAXcAAAAAAAAAAAAAAAAAAA='
        command_off: 'eAB8AA0mKA0NJycNJw0oDA4mKAwoDScNDScNJw0nDScOJigMDiYOAAGTDScNJw4mDiYoDCgNDScNJycNDiYnDSgNJwwOJigNKAwnDQ0nDiYOJg4nDScnDQ0nDQABkw4mDiYOJw0nJw0nDQ0nDScnDQ4mKAwoDScNDScnDQUABdwAAAAAAAAAAAAAAAA='
      #Night light brightness level
      night_light_dimmer_25:
        friendly_name: "Night Light Dimmer 25"
        command_on: 'JgBgAAABKZYRFRAWEBUQFREUERURFRAUEToQORI4EzcTNxE5EDoQOhAUExQQFRAVExMQFhA4EhUPOhE5EDoROBI4EDsQFBI5EQAFJQABKU0RAAxhAAEpTRAADGIAASpKEwANBQAAAAAAAAAA'
      night_light_dimmer_50:
        friendly_name: "Night Light Dimmer 50"
        command_on: 'JgBYAAABKpcSFBAVERQSFQ8VERYPFRIVDzkSOBI4EjcUNxE5ETgRORE5ETkRFREVEBURFRA4EhYPFhEUEDoQOhA5EjgRFhA5EAAFJwABLEwQAAxhAAErSxEADQU='
      night_light_dimmer_75:
        friendly_name: "Night Light Dimmer 75"
        command_on: 'JgBgAAABK5YRFRIUEBYQFQ8WERURFBEWDzoROBI4ETkROBI5ETkQORE5ERUROREVEDkRFRAVEhUQFRA5EhQQOw8WEDkROBE6EQAFJgABKk0RAAxlAAEqSxIADGAAASpLEwANBQAAAAAAAAAA'
      night_light_dimmer_100:
        friendly_name: "Night Light Dimmer 100"
        command_on: 'JgBYAAABKpYRFREUERQSFRAVERURFBEVETcSOBI5ETgTOBE4EDoRORE5ERUPGA86EBUQFhEUEBURFRI4EDkQFhE5EDoQOhE5EAAFJQABKUwRAAxgAAEqSxIADQU='

You only need 3 automations, 3 scripts, not that mess you got up there.

change_brightness_level_from_on:
  sequence:
    - service: switch.turn_on
      data_template:
        entity_id: >
          {% set arr_switch = ["switch.night_light_dimmer_25", "switch.night_light_dimmer_50", "switch.night_light_dimmer_75", "switch.night_light_dimmer_100"] %}
          {{ arr_switch[states.input_number.night_light.state | int - 1] }}
change_brightness_level_from_off:
  sequence:
    - service: switch.turn_on
      entity_id: switch.night_light
    - service: script.change_brightness_level_from_on
change_brightness_level_to_off:
  sequence:
    - service: switch.turn_off
      entity_id: switch.night_light
- alias: Night light rf dimmer on
  description: night light sets the brightness based on slider.
  trigger:
    entity_id: switch.night_light
    platform: state
    from: 'off'
    to: 'on'
  condition:
   condition: template
   value_template: "{{ states('input_number.night_light') | int != 0 }}"
  action:
    service: script.change_brightness_level_from_on
- alias: Night light on when slider off
  description: night light sets the brightness to 100, when slider is zero.
  trigger:
    entity_id: switch.night_light
    platform: state
    from: 'off'
    to: 'on'
  condition:
   condition: template
   value_template: "{{ states('input_number.night_light') | int == 0 }}"
  action:
    - service: input_number.set_value
      data:
        enitity_id: input_number.night_light
        value: 4
    - service: script.change_brightness_level_from_on
- alias: Night light rf dimmer change
  description: Turn off rf dimmer
  trigger:
    entity_id: input_number.night_light
    platform: state
  action:
    service_template: >
      {% if trigger.to_state.state == '0' %}
        script.change_brightness_level_to_off
      {% else %}
        {% if is_state('switch.night_light', 'on') %}
          script.change_brightness_level_from_on
        {% else %}
          script.change_brightness_level_from_off
        {% endif %}
      {% endif %}
1 Like

must admit Ive never seen the description field before in automations (or anywhere else in HA Yaml for that matter)
Couldnt find it in the docs either, is this something new and not yet documented maybe?

I don’t know, don’t use it, was just copying and pasting. I don’t see’s it’s value unless it’s displayed in the UI.

Thank for your helping me.
When I use your code.

  1. I drag slider to 4, switch button on, but brightness still be as when switch on (switch on, brightness ~ 25%)
  2. Slider at 3, Switch button off, I drag slider to 0, switch button to on, but no light :smiley:

you dont have to drag and turn on, just dragging it will turn it on. your second on is probably suppressing the brightness setting.

1 Like

I added a delay 1s in script on from off, the brightness is now at right level.
I’ll try to fix the issue is when button is off, slider !=0, I draged it to 0, button auto switch on but light is actually off :smiley:

- alias: Night light on when slider off 2
  description: night light sets the brightness to 100, when slider is zero.
  trigger:
    entity_id: switch.night_light
    platform: state
    from: 'off'
    to: 'on'
  condition:
   condition: template
   value_template: "{{ states('input_number.night_light') | int == 0 }}"
  action:
    - service: input_number.set_value
      data:
        enitity_id: input_number.night_light
        value: 1

This automation seems not work.
Switch is off, slider at 0.
I change switch to on, slider not set value to 1

Anyone error in this?

P/S: I found it, the last entity is misspell :smiley: “enitity”

Hi, finally I got it work as I want

Slider = 0 means switch off
Slider != 0, switch auto on and choose right brightness level as slider input
Switch off means slider auto at 0.

I use your script and change your automation with my coding way base on your automation :smiley:
I just don’t understand what “trigger.to_state.state” mean ???

This is my automation

- alias: Night light switch on from off
  description: Sync (slider 1 when switch on)
  trigger:
    entity_id: switch.night_light
    platform: state
    from: 'off'
    to: 'on'
  condition:
   condition: template
   value_template: "{{ states('input_number.night_light') | int == 0 }}"
  action:
    - service: input_number.set_value
      data:
        entity_id: input_number.night_light
        value: 1
    - service: script.change_brightness_level_from_on
- alias: Night Light switch on to off
  description: Sync (slider 0 when switch off)
  trigger:
    entity_id: switch.night_light
    platform: state
    from: 'on'
    to: 'off'
  action:
    - service: input_number.set_value
      data:
        entity_id: input_number.night_light
        value: 0
- alias: Night light brightness level change
  description: Night Light change brighness level
  trigger:
    entity_id: input_number.night_light
    platform: state
  action:
    service_template: >
      {% if is_state('switch.night_light', 'off') and states('input_number.night_light') | int != 0 %}
        script.change_brightness_level_from_off
      {% else %}
        {% if states('input_number.night_light') | int == 0 %}
          script.change_brightness_level_to_off
        {% else %}
          script.change_brightness_level_from_on
        {% endif %}
      {% endif %}

And your script

change_brightness_level_from_on:
  sequence:
    - service: switch.turn_on
      data_template:
        entity_id: >
          {% set arr_switch = ["switch.night_light_dimmer_25", "switch.night_light_dimmer_50", "switch.night_light_dimmer_75", "switch.night_light_dimmer_100"] %}
          {{ arr_switch[states.input_number.night_light.state | int - 1] }}
change_brightness_level_from_off:
  sequence:
    - service: switch.turn_on
      entity_id: switch.night_light
    - delay: '00:00:01'
    - service: script.change_brightness_level_from_on
change_brightness_level_to_off:
  sequence:
    - service: switch.turn_off
      entity_id: switch.night_light