Creating dimmer (input_number)

Since I’m very new to HA, where should I insert the delay? In scripts.yaml or automations.yaml?

I think he could just use a python script to dynamically create the shell script.


mystring = "echo -ne '\x0a\x14\x03\x03\x00\x86\x13\x01\x02\x00\x00' > \"$RFXCOM_DEV\"\n"
repeats = mystring*number_of_times
payload = """#!/bin/bash
set -e
RFXCOM_DEV=/dev/serial/by-id/usb-RFXCOM_RFXtrx433_A12IRZ0B-if00-port0
REPEAT=1
"""
payload += repeats

just need to work out getting the number_of_times from HA, and fire the shell command service with the payload. I don’t have time to write the whole thing out now. Will have time later.

That could work.

But in the meantime, at least as a test to see if this is the problem, @sunny, you can do that in the adjust_dim_level script, after calling the command:

adjust_dim_level:
  sequence:
    - service: script.turn_off
      entity_id: script.adjust_again
    - condition: template
      value_template: "{{ steps|int > 0 }}"
    - service_template: "{{ command }}"
    # Add delay to give command time to finish
    - delay: '00:00:01'
    - service: script.adjust_again
      data_template:
        command: "{{ command }}"
        steps: "{{ steps|int - 1 }}"

Nope, added the delay and tried some variations (1, 3, 5, 10). No change.

EDIT: On increasing the level I’m still getting:

Script script.adjust_dim_level already running.

Then I’m with @petro, try a python_script instead.

Sounds good, I found https://www.home-assistant.io/components/python_script/ but I dont really understand how to use the script provided by @petro with a input_number.

Are you more comfortable writing shell scripts? If so, rather than doing the repeating (and possibly even the selection of an increase vs decrease command), you could probably just pass (the direction and) the number of iterations to the external shell script via the shell_command.

Well honestly it should be possible to use https://www.home-assistant.io/components/light.rfxtrx/ (https://github.com/home-assistant/home-assistant/blob/master/homeassistant/components/rfxtrx.py).

    elif command == "dim":
        for _ in range(self.signal_repetitions):
            self._event.device.send_dim(rfx_object.transport, brightness)
        self._state = True

Another rfxtrx python script (https://github.com/woudt/pyRFXtrx/blob/master/RFXtrx/lowlevel.py) includes the commands I need (refer to Lighting 5 - Line 659 - 846):

COMMANDS_03 = {0x00: 'Power',
               0x01: 'Light',
               0x02: 'Bright',
               0x03: 'Dim',
               0x04: '100%',
               0x05: '50%',
               0x06: '25%',
               0x07: 'Mode+',
               0x08: 'Speed-',
               0x09: 'Speed+',
               0x0a: 'Mode-',
               }

What I need is “Bright” and “Light”.

Maybe possible to use this?

try this script:

count = data.get('count')
command = data.get('command')

sleep_time = 0.05

if count:
    try:
        count = int(count)
    except ValueError:
        # if the value of count is not an integer, don't do any loops.
        logger.warning("Bad count {}, cannot convert to integer.".format(count))
        count = None

domain, command = command.split('.')

if domain and command and count: #make sure the entity_id exists and the count is greater than 0.
    for i in range(count):
        hass.services.call(domain, command , None, True)
        time.sleep(sleep_time)
else:
    logger.warning("Did not execute loop.")

Save it as a python script. Follow the directions for python scripts. Make sure you name the script in the config loop_command.

your automation should be:

# 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: python_script.loop_command
     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 %}
       count: >
         {{ ((trigger.to_state.state|float - trigger.from_state.state|float)|abs / state_attr('input_number.office_dimmer', 'step'))|round }}
1 Like

@petro there seem to be an error.

2018-07-31 17:20:18 ERROR (Thread-20) [homeassistant.components.python_script.loop_command.py] Error executing script: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/python_script.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "loop_command.py", line 8, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Are you passing in a count? Also, edited the script to handle that exception.

Whoops, sorry for that. My fail.

Got some log output:

2018-07-31 17:50:59 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=input_number, service_data=entity_id=input_number.office_dimmer, value=70, service_call_id=1976819952-8, service=set_value>
2018-07-31 17:50:59 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state input_number.office_dimmer=70.0; mode=slider, min=10.0, icon=mdi:lightbulb, unit_of_measurement=%, max=100.0, friendly_name=Büro, step=10.0 @ 2018-07-31T17:50:59.286274+02:00>, old_state=<state input_number.office_dimmer=100.0; mode=slider, min=10.0, icon=mdi:lightbulb, unit_of_measurement=%, max=100.0, friendly_name=Büro, step=10.0 @ 2018-07-31T17:50:01.236883+02:00>, entity_id=input_number.office_dimmer>
2018-07-31 17:50:59 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1976819952-8>
2018-07-31 17:50:59 INFO (MainThread) [homeassistant.components.automation] Executing Dim light
2018-07-31 17:50:59 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: domain=automation, entity_id=automation.dim_light, message=has been triggered, name=Dim light>
2018-07-31 17:50:59 INFO (MainThread) [homeassistant.helpers.script] Script Dim light: Running script
2018-07-31 17:50:59 INFO (MainThread) [homeassistant.helpers.script] Script Dim light: Executing step call service
2018-07-31 17:50:59 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=python_script, service_data=command=shell_command.decrease_level_office, count=3, service_call_id=1976819952-9, service=loop_command>
2018-07-31 17:50:59 INFO (Thread-4) [homeassistant.components.python_script] Executing loop_command.py: {'command': 'shell_command.decrease_level_office', 'count': '3'}
2018-07-31 17:50:59 WARNING (Thread-4) [homeassistant.components.python_script.loop_command.py] 3.
2018-07-31 17:50:59 WARNING (Thread-4) [homeassistant.components.python_script.loop_command.py] Couldn't find entity_id shell_command.decrease_level_office
2018-07-31 17:50:59 WARNING (Thread-4) [homeassistant.components.python_script.loop_command.py] Did not execute loop.

Unable to find the shell_commands for increase and decrease.

copy the (python script) code again and try again. I made a modification.

2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=entity_id=input_number.office_dimmer, value=70, service_call_id=1976475952-34, domain=input_number, service=set_value>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state input_number.office_dimmer=100.0; mode=slider, friendly_name=Büro, unit_of_measurement=%, icon=mdi:lightbulb, max=100.0, min=10.0, step=10.0 @ 2018-07-31T18:04:10.794995+02:00>, entity_id=input_number.office_dimmer, new_state=<state input_number.office_dimmer=70.0; mode=slider, friendly_name=Büro, unit_of_measurement=%, icon=mdi:lightbulb, max=100.0, min=10.0, step=10.0 @ 2018-07-31T18:05:15.316404+02:00>>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1976475952-34>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.components.automation] Executing Dim light
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: entity_id=automation.dim_light, name=Dim light, domain=automation, message=has been triggered>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.helpers.script] Script Dim light: Running script
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.helpers.script] Script Dim light: Executing step call service
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=command=shell_command.decrease_level_office, count=3, service_call_id=1976475952-35, domain=python_script, service=loop_command>
2018-07-31 18:05:15 INFO (Thread-2) [homeassistant.components.python_script] Executing loop_command.py: {'command': 'shell_command.decrease_level_office', 'count': '3'}
2018-07-31 18:05:15 WARNING (Thread-2) [homeassistant.components.python_script.loop_command.py] Couldn't find entity_id shell_command.decrease_level_office
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=None, service_call_id=1976475952-36, domain=shell_command, service=decrease_level_office>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1976475952-36>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=None, service_call_id=1976475952-37, domain=shell_command, service=decrease_level_office>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1976475952-37>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=None, service_call_id=1976475952-38, domain=shell_command, service=decrease_level_office>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1976475952-38>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1976475952-35>
2018-07-31 18:05:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state automation.dim_light=on; last_triggered=2018-07-31T18:04:11.048640+02:00, friendly_name=Dim light @ 2018-07-31T18:03:13.617937+02:00>, entity_id=automation.dim_light, new_state=<state automation.dim_light=on; last_triggered=2018-07-31T18:05:15.677989+02:00, friendly_name=Dim light @ 2018-07-31T18:03:13.617937+02:00>>

Still the same issue.

the warning will always occur. And looking at your log, the service is being called. So whats the problem?

Apparently shell_commands don’t create entities; they just register services. That’s why the call to hass.states.get is returning None. Since the rest of the script doesn’t have any use for that anyway, why not just remove those statements? I.e., change:

state = hass.states.get(entity_id)
domain, device = entity_id.split('.')
if not state:
    logger.warning("Couldn't find entity_id {}".format(entity_id))

to:

domain, device = entity_id.split('.')

In fact, I’d change the name of the variable from entity_id to command throughout.

Yeah, i assumed so, that’s why I just left the if statement in there because this could be used with rest commands… but i didn’t feel like testing those. LOL

Looks like rest commands only register services, too. :slight_smile:

made the changes. You by chance know what the last flag is in hass.services.call(domain, entity, data, ???)? The API documentation isn’t loading for me.

The problem is still the same. No chance to dim.