Creating dimmer (input_number)

Hi community,

first of all thank for the great community and all the options to get in touch with you guys (Discord, website).
I’m very new to HA and I love it! Previously I was using openHAB but I think HA fits more my needs.

In my HA installation I added a RFXCOM and used the cover.rfxtrx component, which works very well.
But I’m currently not able to get the light.rfxtrx working. Only the “off” switch is working and with this switch (not the “on” button) I can control the light.
Now I have created 4 .sh files with custom commands that can be send to /dev/ttyUSB0 and this works very well.
I also managed to create a switch with shell_command to turn on/off the light.

Question:
How to create a dimmer (or input_number) with step 10 and .sh scripts?

  • First run of office_light.sh -10%, the second run by -10%… until 0.
  • First run of office_bright.sh +10%, the second run +10%… until 100.
    The shell script looks something link:
    echo -e “\x0a\x14\x00\x00…” > /dev/ttyUSB0
    Just for clarification: The scripts does not include a value for +/- 10%. It’s just an iterated call of the same command.

Hope you guys could give me a hint and help me getting rid of my headaches.

Thanks and best regards
Sunny

Let me see if I understand what you’re looking for. You want an input_number that can go from 0 to 100 in steps of 10, and whenever that input_number is changed you want to call one of the shell scripts a number of times depending on whether the change is positive or negative and how big it is (i.e., how many multiples of 10.) Is that right?

If so, this could maybe work for you:

input_number:
  dim_level:
    min: 0
    max: 100
    step: 10
automation:
  - alias: Dim light
    trigger:
      platform: state
      entity_id: input_number.dim_level
    condition:
      condition: template
      value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
    action:
      service: script.adjust_dim_level
      data_template:
        command: >
          {% if trigger.to_state.state|int > trigger.from_state.state|int %}
            shell_command.increase_level
          {% else %}
            shell_command.decrease_level
          {% endif %}
        steps: >
          {{ (trigger.to_state.state|int - trigger.from_state.state|int)|abs // 10 }}
script:
  adjust_dim_level:
    sequence:
      - condition: template
        value_template: "{{ steps|int > 0 }}"
      - service_template: "{{ command }}"
      - service: script.adjust_again
        data_template:
          command: "{{ command }}"
          steps: "{{ steps|int - 1 }}"
  adjust_again:
    - service: script.adjust_dim_level
      data_template:
        command: "{{ command }}"
        steps: "{{ steps }}"

Obviously I don’t know the names of your shell commands, etc., and of course I have no way of really testing this, but hopefully this is a start for you.

EDIT: Fixed a couple bugs.

Hey pnbruckner,

thank you so much for your answer. You have understood almost everything correctly.
I dont want to call one of the script a number of times. Just once on every state change.

So if I change the input number from 0 to 10 script A should be call. If I change from 10 to 20 the same script should be called. But if I go back from 20 to 10 script B should be called.

Thanks and best regards
Sunny

But if you change the input_number from 10 to 30… It’s possible for the input_number to be changed by more than one step at a time. That’s why I wrote the code the way I did. :slight_smile:

Hey pnbruckner,

thank you for your response.
I’ve added the input_number section inside of my configuration.yaml, the automation part inside of automations.yaml and the script part inside of scripts.yaml.

Additionally I changed increase_level to increase_level_office and the same for decrease_level.

I get the following error:

2018-07-24 22:39:06 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: [script.adjust_again] is an invalid option for [script]. Check: script->script->adjust_dim_level->sequence->2->script.adjust_again. (See /home/homeassistant/.homeassistant/configuration.yaml, line 78). Please check the docs at https://home-assistant.io/components/script/
2018-07-24 22:39:07 ERROR (MainThread) [homeassistant.setup] Setup failed for script: Invalid config.
2018-07-24 22:39:07 ERROR (MainThread) [homeassistant.config] Invalid config for [input_number]: expected a dictionary for dictionary value @ data['input_number']['decrease_level_office']. Got "echo -ne '\n\x14\x03\x03\x00\x86\x13\x01\x02\x00\x00' > /dev/ttyUSB0"
expected a dictionary for dictionary value @ data['input_number']['increase_level_office']. Got "echo -ne '\n\x14\x03\x03\x00\x86\x13\x01\x01\x00\x00' > /dev/ttyUSB0". (See /home/homeassistant/.homeassistant/configuration.yaml, line 98). Please check the docs at https://home-assistant.io/components/input_number/
2018-07-24 22:39:07 ERROR (MainThread) [homeassistant.setup] Setup failed for input_number: Invalid config.

Upsy, my bad. Forgot to uncomment shell_command:

Now I have a last error: Unable to find service script/adjust_dim_level

If you’re putting the scripts into scripts.yaml, you need to remove the first line with script: and move everything over two characters to the left:

adjust_dim_level:
  sequence:
    - condition: template
      value_template: "{{ steps|int > 0 }}"
    - service_template: "{{ command }}"
    - script.adjust_again
      data_template:
        command: "{{ command }}"
        steps: "{{ steps|int - 1 }}"
adjust_again:
  - script.adjust_dim_level:
    data_template:
      command: "{{ command }}"
      steps: "{{ steps }}"

And on line 6 there is a missing colon :slight_smile:

ERROR (MainThread) [homeassistant.config] Invalid config for [script]: [script.adjust_again] is an invalid option for [script]. Check: script->script->adjust_dim_level->sequence->2->script.adjust_again. (See /home/homeassistant/.homeassistant/configuration.yaml, line 78).

D’oh! I really got sloppy here. Sorry about that:

adjust_dim_level:
  sequence:
    - condition: template
      value_template: "{{ steps|int > 0 }}"
    - service_template: "{{ command }}"
    - service: script.adjust_again
      data_template:
        command: "{{ command }}"
        steps: "{{ steps|int - 1 }}"
adjust_again:
  - service: script.adjust_dim_level
    data_template:
      command: "{{ command }}"
      steps: "{{ steps }}"

Wow, I must be tired!! :wink:

EDIT: I definitely am. Fix again. :blush:

Remove colon :).

Error during template condition: UndefinedError: 'steps' is undefined

1 Like

Can you post what you have now?

configuration.yaml

input_number:
  office_dimmer:
    min: 0
    max: 100
    step: 16.667

shell_command:
  decrease_level_office: 'echo -ne "\x0a\x14\x03\x03\x00\x86\x13\x01\x02\x00\x00" > /dev/ttyUSB0'
  increase_level_office: 'echo -ne "\x0a\x14\x03\x03\x00\x86\x13\x01\x01\x00\x00" > /dev/ttyUSB0'

Fixed the shell_command. The quotes have to swapped " -> ’ and ’ -> ".

automations.yaml

# Light office
 - alias: Dim light
   trigger:
     platform: state
     entity_id: input_number.office_dimmer
   condition:
     condition: template
     value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
   action:
     service: script.adjust_dim_level
     data_template:
       command: >
         {% if trigger.to_state.state|int > trigger.from_state.state|int %}
           shell_command.increase_level_office
         {% else %}
           shell_command.decrease_level_office
         {% endif %}
       steps: >
         {{ (trigger.to_state.state|int - trigger.from_state.state|int)|abs // 10 }}

scripts.yaml

adjust_dim_level:
  sequence:
    - condition: template
      value_template: "{{ steps|int > 0 }}"
    - service_template: "{{ command }}"
    - service: script.adjust_again
      data_template:
        command: "{{ command }}"
        steps: "{{ steps|int - 1 }}"
adjust_again:
  sequence:
    - service: script.adjust_dim_level
      data_template:
        command: "{{ command }}"
        steps: "{{ steps }}"

And now I’m getting:

Script script.adjust_dim_level already running.

I thought you said you wanted the step to be 10. With a step of 16.667, the other code needs to be adjusted. Probably it’s running in an infinite loop or something.

Changed it back to 10.

Error during template condition: UndefinedError: 'steps' is undefined

I think I’m stumped for today. Maybe someone else will jump in and find what’s wrong. If not I can try and look at it some more tomorrow. Sorry.

Same for me. Thank you so much! Hope to get it working. I’m on it since 3 days :).
Gn8

So, I’m still not sure what’s going on, but here is (in theory) a way to allow any step size, with some potentially helpful improvements:

automations.yaml

# Light office
 - alias: Dim light
   trigger:
     platform: state
     entity_id: input_number.office_dimmer
   condition:
     condition: template
     value_template: "{{ trigger.to_state.state != trigger.from_state.state and
                         trigger.to_state.state is not none and
                         trigger.from_state.state is not none }}"
   action:
     service: script.adjust_dim_level
     data_template:
       command: >
         {% if trigger.to_state.state|float > trigger.from_state.state|float %}
           shell_command.increase_level_office
         {% else %}
           shell_command.decrease_level_office
         {% endif %}
       steps: >
         {{ ((trigger.to_state.state|float - trigger.from_state.state|float)|abs
             / state_attr('input_number.office_dimmer', 'step'))|round }}

Now, I still can’t see why you were sometimes getting the 'steps' is undefined error, but regarding the “already running”, maybe that can be addressed like this:

scripts.yaml

adjust_dim_level:
  sequence:
    - condition: template
      value_template: "{{ steps|int > 0 }}"
    - service_template: "{{ command }}"
    - service: script.adjust_again
      data_template:
        command: "{{ command }}"
        steps: "{{ steps|int - 1 }}"
adjust_again:
  sequence:
    - wait_template: "{{ is_state('script.adjust_dim_level', 'off') }}"
    - service: script.adjust_dim_level
      data_template:
        command: "{{ command }}"
        steps: "{{ steps }}"

Hey pnbruckner,

sorry for my late response.
Now my HA screen looks like this:

And I’m getting the following out put using the input_number and/or the adjust_again/adjust_dim_level script buttons:

`Error during template condition: UndefinedError: ‘steps’ is undefined

Script script.adjust_dim_level already running.`

I just copy pasted the last version of your code and the rest of it is the same as posted above.

Well I’m stumped. I have no idea why that isn’t working or why you’re getting the error about steps being undefined.

@petro, if you have some time, would you mind looking this over? The problem is probably staring us in the face, but for the life of me, I can’t see it.

@sunny so i gotta wrap my head around this. What’s the end goal? Just a slider that calls shell commands that increment up and down?

EDIT: I think i get it. The script runs x number of times in a ‘loop’, calling the corresponding shell command each time.

EDIT2: @sunny how would you feel about moving to a python script for this?

EDIT3: What if we cancel the other script when we get into the repeat script:

adjust_dim_level:
  sequence:
    - condition: template
      value_template: "{{ steps|int > 0 }}"
    - service_template: "{{ command }}"
    - service: script.adjust_again
      data_template:
        command: "{{ command }}"
        steps: "{{ steps|int - 1 }}"
adjust_again:
  sequence:
    - service: script.turn_off
      entity_id: script.adjust_dim_level
    - service: script.adjust_dim_level
      data_template:
        command: "{{ command }}"
        steps: "{{ steps }}"