Automation template - how to detect slider last and new state and compare it in action template

Sorry @AhmadK I don’t understand your answer?

All lines below this one:

    service_template: >-

are determining the service to assign to service_template. Whatever the result, it must be one valid service.

In this line, you are attempting to assign it two services and Home Assistant rejected it.

script.kitchen_light_dimm_on, script.kitchen_light_moon

That’s probably because you’re yet to read the link provided.
And @123 already explained you the main reason of your error.

Thanks. @123 and @AhmadK

Does it work now?

Pretty good yes. :slight_smile:

I don’t know if it possible to make configuration shorter, but works for me:

scripts:

####################################################
## IR KITCHEN LIGHT REMOTE BUTTONS                  
####################################################

  kitchen_light_sun:
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.kitchen_light_on_off
      - delay:
          milliseconds: 10
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBUAAABJZITEhI3EhISEhI3EjcSEhI3EjcSEhI3EjcSEhMREzYTERMREzYTNhM2ExETERM2EzYTNhMRExISEhI3EjcSEhISEgAFJgABJUYTAAWLFwANBQAAAAA="
      - service: mqtt.publish
        data:
          topic: 'home/buttons/ir'
          payload: 'kitchen_light_sun'
      - delay: '00:00:02'
      - service: mqtt.publish
        data:
          topic: 'home/buttons/ir'
          payload: 'none'


  kitchen_light_moon:
    sequence:
      - service: automation.turn_off
        data:
          entity_id: automation.kitchen_light_daytime
      - service: switch.turn_on
        data:
          entity_id: switch.kitchen_light_on_off
      - service: automation.turn_on
        data:
          entity_id: automation.kitchen_light_daytime
      - delay:
          milliseconds: 10
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBQAAABJJQRExE4ERMRExI3EjcSEhI3EjcSEhI3EjcSEhISEjcSEhISEhISExETERMRExETETgROBE4ETgROBE4ETgROBETEgAFJgABJEcSAA0FAAAAAAAAAAA="
      - service: mqtt.publish
        data:
          topic: 'home/buttons/ir'
          payload: 'kitchen_light_moon'
      - delay: '00:00:02'
      - service: mqtt.publish
        data:
          topic: 'home/buttons/ir'
          payload: 'none'

  kitchen_light_dimm_up:
    sequence:
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBQAAABKJMSEhI3EhISExE4ETgRExE4ETgRExE4ETgRExETEjcSEhISEjcSEhI3EhIUEBQ1FDUUNRQQFDUUEBQ1FDUUEBQREwAFJAABKEQUAA0FAAAAAAAAAAA="

  kitchen_light_dimm_down:
    sequence:
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBQAAABJpIUEBQ1FBAUEBQ1FDUUEBQ1FDUUEBQ1EzYUERISEjcSEhISEjYTEhMREzYTERM2EzYTNhMREzYTNhMREzYTERMREwAFJQABJ0UUAA0FAAAAAAAAAAA="

  kitchen_light_ct_up:
    sequence:
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBQAAABJpIUEBQ1FBAUEBQ1FDUUERM1FDUUERQ1FDUUEBQQFDUUEBQ1FBAUEBQ1FDUUEBQQFDUUEBQ1FDUUEBQQFDUUNRQQFAAFJAABJ0UUAA0FAAAAAAAAAAA="

  kitchen_light_ct_down:
    sequence:
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBQAAABJ5EUEBQ1FBAUEBQ1FDUUERM1FDUVEBQ1FDUUEBQQFDUUEBQ1FBAUEBQQFBAUEBQ1FDUUEBQ1FDUUNRQ1FDUUERMRFAAFJAABJ0QUAA0FAAAAAAAAAAA="

  kitchen_light_on:
    sequence:
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBQAAABJZMSExE3EhMRExE4ETgRExE4ETgRExI3EjcSEhISEjcSEhISEjcSEhISEhISExETETgRNhMTETgROBE4ETgROBETEgAFJgABJUYSAA0FAAAAAAAAAAA="

  kitchen_light_off:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down_10x
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 
            - "JgBQAAABJpITERM2ExESEhM2EzYTERM2EzYTERM2EjcTERMSEjcSEhETERMROBI3EjcSEhI3EjcSNxI3EhISEhISEjcTERMREgAFJgABJUYSAA0FAAAAAAAAAAA="
      - service: switch.turn_off
        data:
          entity_id: switch.kitchen_light_on_off

  kitchen_light_dimm_up_2x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up

  kitchen_light_dimm_down_2x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down

  kitchen_light_dimm_up_3x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up

  kitchen_light_dimm_down_3x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down

  kitchen_light_dimm_up_4x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up

  kitchen_light_dimm_down_4x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down

  kitchen_light_dimm_up_5x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up

  kitchen_light_dimm_down_5x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down

  kitchen_light_dimm_up_6x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up

  kitchen_light_dimm_down_6x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down

  kitchen_light_dimm_up_7x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_up

  kitchen_light_dimm_down_7x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_dimm_down

####################################################

  kitchen_light_ct_up_2x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_2x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_3x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_3x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_4x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_4x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_5x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_5x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_6x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_6x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_7x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_7x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_8x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_8x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_9x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_9x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

  kitchen_light_ct_up_10x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_up

  kitchen_light_ct_down_10x:
    sequence:
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down
      - service: script.toggle
        data:
          entity_id: script.kitchen_light_ct_down

####################################################
## SCRIPT DO NOTHING                  
####################################################

  do_nothing:
    sequence:
      - service: mqtt.publish
        data:
          topic: 'home/status/'
          payload: 'script_do_nothing'

automation:

####################################################
# SET DIMM to 10
####################################################

- alias: 'kitchen_set_moon_mode'
  trigger:
    platform: state
    entity_id: sensor.mqtt_ir_button_sensing    
    to: 'kitchen_light_moon'

  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.kitchen_light_dimm
        value: 10

####################################################
# SET DIMM to 100
####################################################

- alias: 'kitchen_light_set_sun_mode'
  trigger:
    platform: state
    entity_id: sensor.mqtt_ir_button_sensing    
    to: 'kitchen_light_sun'

  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.kitchen_light_dimm
        value: 100

####################################################
# WHEN OFF SET DIMM and CT to 0
####################################################

- alias: 'kitchen_light_off_set_dimm_ct_0'
  trigger:
    platform: state
    entity_id: switch.ir_kitchen_light
    to: 'off'

  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.kitchen_light_dimm
        value: 0

    - service: input_number.set_value
      data_template:
        entity_id: input_number.kitchen_light_ct
        value: 0


####################################################
# WHEN ON RESET CT
####################################################

- alias: 'kitchen_light_on_reset_ct'
  trigger:
    platform: state
    entity_id: switch.ir_kitchen_light
    to: 'on'

  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.kitchen_light_ct
        value: 100

####################################################
# WHEN ON SENSING DAYTIME AND TURN ON LIGHT 10% OR 100%
####################################################

- alias: 'kitchen_light_daytime'
  trigger:
    platform: state
    entity_id: switch.ir_kitchen_light

  action:
    service_template: >-
      {% set sun = states('sun.sun') %}
      {% set light = states('switch.ir_kitchen_light') %}
      {% set slider = states('input_number.kitchen_light_dimm') | round(0) %}
      {% if sun == 'above_horizon' and light == 'on' and slider == 0 %}
        script.kitchen_light_sun 
      {% elif sun == 'below_horizon' and light == 'on' and slider == 0 %}
        script.kitchen_light_moon
      {% else %}
        script.do_nothing
      {% endif -%}


####################################################
# SET DIMM
####################################################
- alias: 'kitchen_light_set_dimm'
  trigger:
    platform: state
    entity_id: input_number.kitchen_light_dimm
  action:
    service_template: >-
      {% set to = trigger.to_state.state | int %}
      {% set from = trigger.from_state.state | int %}
      {% set diff = (from - to) | round(0) %}
      {% set slider = states('input_number.kitchen_light_dimm') | round(0) %}
      {% set step = diff / 10 | round(0) %}
      {% if slider == 0 %}
        script.kitchen_light_off
      {% elif slider == 10 %}
        script.kitchen_light_moon
      {% elif slider == 100 %}
        script.kitchen_light_sun
      {% elif diff < 0 and step == -1 %}
        script.kitchen_light_dimm_up
      {% elif diff > 0 and step == 1 %}
        script.kitchen_light_dimm_down

      {% elif diff < 0 and step == -2 %}
        script.kitchen_light_dimm_up_2x
      {% elif diff > 0 and step == 2 %}
        script.kitchen_light_dimm_down_2x

      {% elif diff < 0 and step == -3 %}
        script.kitchen_light_dimm_up_3x
      {% elif diff > 0 and step == 3 %}
        script.kitchen_light_dimm_down_3x

      {% elif diff < 0 and step == -4 %}
        script.kitchen_light_dimm_up_4x
      {% elif diff > 0 and step == 4 %}
        script.kitchen_light_dimm_down_4x

      {% elif diff < 0 and step == -5 %}
        script.kitchen_light_dimm_up_5x
      {% elif diff > 0 and step == 5 %}
        script.kitchen_light_dimm_down_5x

      {% elif diff < 0 and step == -6 %}
        script.kitchen_light_dimm_up_6x
      {% elif diff > 0 and step == 6 %}
        script.kitchen_light_dimm_down_6x

      {% elif diff < 0 and step == -7 %}
        script.kitchen_light_dimm_up_7x
      {% elif diff > 0 and step == 7 %}
        script.kitchen_light_dimm_down_7x

      {% else %}
        script.do_nothing
      {% endif -%}

####################################################
# SET CT
####################################################
- alias: 'kitchen_light_set_ct'
  trigger:
    platform: state
    entity_id: input_number.kitchen_light_ct
  action:
    service_template: >-
      {% set to = trigger.to_state.state | int %}
      {% set from = trigger.from_state.state | int %}
      {% set diff = (from - to) | round(0) %}
      {% set slider = states('input_number.kitchen_light_ct') | round(0) %}
      {% set step = diff / 10 | round(0) %}

      {% if diff < 0 and step == -1 %}
        script.kitchen_light_ct_down
      {% elif diff > 0 and step == 1 %}
        script.kitchen_light_ct_up

      {% elif diff < 0 and step == -2 %}
        script.kitchen_light_ct_down_2x
      {% elif diff > 0 and step == 2 %}
        script.kitchen_light_ct_up_2x

      {% elif diff < 0 and step == -3 %}
        script.kitchen_light_ct_down_3x
      {% elif diff > 0 and step == 3 %}
        script.kitchen_light_ct_up_3x

      {% elif diff < 0 and step == -4 %}
        script.kitchen_light_ct_down_4x
      {% elif diff > 0 and step == 4 %}
        script.kitchen_light_ct_up_4x

      {% elif diff < 0 and step == -5 %}
        script.kitchen_light_ct_down_5x
      {% elif diff > 0 and step == 5 %}
        script.kitchen_light_ct_up_5x

      {% elif diff < 0 and step == -6 %}
        script.kitchen_light_ct_down_6x
      {% elif diff > 0 and step == 6 %}
        script.kitchen_light_ct_up_6x

      {% elif diff < 0 and step == -7 %}
        script.kitchen_light_ct_down_7x
      {% elif diff > 0 and step == 7 %}
        script.kitchen_light_ct_up_7x

      {% elif diff < 0 and step == -8 %}
        script.kitchen_light_ct_down_8x
      {% elif diff > 0 and step == 8 %}
        script.kitchen_light_ct_up_8x

      {% elif diff < 0 and step == -9 %}
        script.kitchen_light_ct_down_9x
      {% elif diff > 0 and step == 9 %}
        script.kitchen_light_ct_up_9x

      {% elif diff < 0 and step == -10 %}
        script.kitchen_light_ct_down_10x
      {% elif diff > 0 and step == 10 %}
        script.kitchen_light_ct_up_10x

      {% else %}
        script.do_nothing
      {% endif -%}

not having read your complete configuration, you might be interested in the ultimate script.dummy. Since you’re using an mqtt.publish service actually publishing a payload, you might find it useful:

dummy:
  alias: 'Dummy script'
  sequence:
    delay: 00:00:00

define it globally in your scripts file, and use it in any place needed.

1 Like

I don’t understand? Why dummy script is useful?

because it’s the ultimate do-nothing script. You can call it in templates, like you do, and have it do nothing.

OK, thanks.

I would use a python_script to replace all of the scripts (up_1X, down 1X, up_2X, down_2X, etc).

The automation’s template wouldn’t change much but now it would simply calculate the number of repetitions to pass on to the python_script. For example, repetitions: -7 would instruct the python_script.brightness_controller to transmit a dim command 7 times. Repetitions=0 would mean to simply turn the light off.

  action:
  - service: python_script.brightness_controller
    data_template:
      repetitions: `<your template>`

However, if what you created works for you, then I wouldn’t bother with a python_script (unless you wanted to learn something new).

Thanks @123. I am always open to learn something new if you have a time to explain more detail what you have in mind.

I’m kind late to the topic but just to add a idea. Instead of trying to keep the state of device, why not just bring it to a initial know state and then set the new state from there? If I got it right, when you press the “moon button” the brightness goes to 10%, so after pressing it you know the state, then you just have to send N-1 “dimm_up” commands to match the brightness to your input slider value. It may or may not be a little disturbing for the people in the room when the brightness goes to 10%…

Here is an example of a python_script that accepts one parameter (command) that specifies the number of times to transmit dimm_up or dimm_down.

This python_script accepts a command in the range from -10 to 11.

If command is:

  • -5 the python_script will send dimm_down 5 times to the light.
  • 3 will send dimm_up 3 times to the light.
  • 0 means to turn off the light.
  • 11 means to turn on the light.

To permit Home Assistant to support python_scripts, you must add the following line to configuration.yaml:

python_script:

In your config directory, create a sub-directory called python_scripts then create a new file in it called dimm_control.py containing the following python code:

rf = { "ON":"JgBQAAABJZMSExE3EhMRExE4ETgRExE4ETgRExI3EjcSEhISEjcSEhISEjcSEhISEhISExETETgRNhMTETgROBE4ETgROBETEgAFJgABJUYSAA0FAAAAAAAAAAA=",
       "OFF":"JgBQAAABJpITERM2ExESEhM2EzYTERM2EzYTERM2EjcTERMSEjcSEhETERMROBI3EjcSEhI3EjcSNxI3EhISEhISEjcTERMREgAFJgABJUYSAA0FAAAAAAAAAAA=",
       "UP":"JgBQAAABKJMSEhI3EhISExE4ETgRExE4ETgRExE4ETgRExETEjcSEhISEjcSEhI3EhIUEBQ1FDUUNRQQFDUUEBQ1FDUUEBQREwAFJAABKEQUAA0FAAAAAAAAAAA=",
       "DOWN":"JgBQAAABJpIUEBQ1FBAUEBQ1FDUUEBQ1FDUUEBQ1EzYUERISEjcSEhISEjYTEhMREzYTERM2EzYTNhMREzYTNhMREzYTERMREwAFJQABJ0UUAA0FAAAAAAAAAAA="
     }

cmd = data.get('command')

if cmd is not None:
  cmd = int(cmd)
  if -10 <= cmd <= 11:
    if cmd == 0:
      loop = 1
      rfc = rf['OFF']
    elif cmd == 11:
      loop = 1
      rfc = rf['ON']
    else:
      if cmd > 0:
        rfc = rf['UP']
      else:
        rfc = rf['DOWN']
      loop = abs(cmd)

    service_data = {'packet':'{}'.format(rfc)}
    for i in range(loop):
      hass.services.call('switch', 'broadlink_send_packet_192_168_2_1', service_data, False)

Now restart Home Assistant.

To test it:

  • Go to Home Assistant’s Services page
  • Set Service to python_script.dimm_control
  • To turn the light on, set Service Data to {"command":11}
  • click the CALL SERVICE button

To dimm the light down 2 steps, set Service Data to {"command":-2} and click CALL SERVICE.

Screenshot%20from%202019-04-22%2021-10-05

To use it in an automation, call the service python_script.dimm_control and use a data_template to set command to a value between -10 and 11. For example:

- alias: 'kitchen_light_set_dimm'
  trigger:
    platform: state
    entity_id: input_number.kitchen_light_dimm
  action:
    service: python_script.dimm_control
    data_template:
      command: >-
        {% set to = trigger.to_state.state | int %}
        {% set from = trigger.from_state.state | int %}
        {% set diff = (from - to) | round(0) %}
        {% set slider = states('input_number.kitchen_light_dimm') | round(0) %}
        {% set step = diff / 10 | round(0) %}
        {% if slider == 0 %}
          0
        {% elif slider == 100 %}
          11
        {% else %}
          {{step}}
        {% endif %}

… and if it doesn’t work, oh well. :man_shrugging:

Thanks for script @123

I try today but there is something wrong with python script.

Here is an error:

Error executing script: (ServiceNotFound(...), 'Service switch.broadlink_send_packet_192_168_2_1 not found')
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/python_script/__init__.py", line 159, in execute
    exec(compiled.code, restricted_globals, local)
  File "dimm_control.py", line 27, in <module>
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1082, in call
    self._hass.loop
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 432, in result
    return self.__get_result()
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1115, in async_call
    raise ServiceNotFound(domain, service) from None
homeassistant.exceptions.ServiceNotFound: (ServiceNotFound(...), 'Service switch.broadlink_send_packet_192_168_2_1 not found')

I have only service called broadlink.send?

Look at the code in your script, shown in this post.

  kitchen_light_sun:
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.kitchen_light_on_off
      - delay:
          milliseconds: 10
      - service: switch.broadlink_send_packet_192_168_2_1
        data:
          packet: 

It uses service: switch.broadlink_send_packet_192_168_2_1 which is the same one used in the python_script.

If you recently upgraded Home Assistant to 0.92 then it has a new Broadlink component with a completely different service: broadlink.send. As a result, everywhere you have used service: switch.broadlink_send_packet_192_168_2_1 will no longer work.

Yes I recently upgraded to 0.92.1.

If I understand I must adjust python script to work with new broadlink component. How?

this:

hass.services.call('switch', 'broadlink_send_packet_192_168_2_1', service_data, False)

to this: ???

hass.services.call('broadlink', 'send', service_data, False)

Yes, but the contents of service_data will now have to specify the host.

service_data = {'host':'192.168.2.1', 'packet':'{}'.format(rfc)}

Your original scripts will also have to change because they still use switch.broadlink_send_packet_192_168_2_1.

If I understand correctly I must change:

this:

    service_data = {'packet':'{}'.format(rfc)}
    for i in range(loop):
      hass.services.call('switch', 'broadlink_send_packet_192_168_2_1', service_data, False)

to this:

service_data = {'host':'192.168.2.1', 'packet':'{}'.format(rfc)}

    service_data = {'host':'192.168.2.1', 'packet':'{}'.format(rfc)}
    for i in range(loop):
      hass.services.call('broadlink', 'send', service_data, False)