for i in range(10):
service_data = {'host':'192.168.2.1', 'packet':'{}'.format(rf['C'])}
hass.services.call('broadlink', 'send', service_data, False)
time.sleep(0.55)
try this
cmd = data.get('command')
def call_service(hass, host, rfkey, count):
rf = {
"C":"JgBQAAABJpIUEBQ1FBAUEBQ1FDUUERM1FDUUERQ1FDUUEBQQFDUUEBQ1FBAUEBQ1FDUUEBQQFDUUEBQ1FDUUEBQQFDUUNRQQFAAFJAABJ0UUAA0FAAAAAAAAAAA=",
"W":"JgBQAAABJ5EUEBQ1FBAUEBQ1FDUUERM1FDUVEBQ1FDUUEBQQFDUUEBQ1FBAUEBQQFBAUEBQ1FDUUEBQ1FDUUNRQ1FDUUERMRFAAFJAABJ0QUAA0FAAAAAAAAAAA="
}
for i in range(count):
service_data = {'host':host, 'packet':'{}'.format(rf[rfkey])}
hass.services.call('broadlink', 'send', service_data, False)
time.sleep(0.55)
if cmd is not None:
cmd = int(cmd)
if -10 <= cmd <= 12:
if cmd in [0, 10]:
call_service(hass, '192.168.2.1', 'C', 10)
elif cmd == 11:
call_service(hass, '192.168.2.1', 'W', 10)
else:
rfkey = 'C' if cmd > 0 else 'W'
count = abs(cmd)
call_service(hass, '192.168.2.1', rfkey, count)
Hi @petro I tested your solution but not working.
Error:
Error executing script: name 'rf' is not defined
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 "ct_control.py", line 19, in <module>
File "ct_control.py", line 9, in call_service
NameError: name 'rf' is not defined
interesting. It seems it’s limiting the scope of the definition. I edited the previous post, try it again.
Still errors:
Error executing script: name 'hass' is not defined
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 "ct_control.py", line 19, in <module>
File "ct_control.py", line 10, in call_service
NameError: name 'hass' is not defined
It would appear you have to pass the hass
object as a parameter to the call_service
function in order to be able to use it within the function.
There’s an example of it in this recent post by Marius:
So you will have to change the function’s signature to this:
def call_service(hass, host, rfkey, count):
and modify every call to the function. For example, this:
call_service('192.168.2.1', 'C', 10)
changes to this:
call_service(hass, '192.168.2.1', 'C', 10)
I try to make new automation + python I already have working version with one problem.
Here is link.
On remote I have only one button of on and off. When I decrease fan speed and when I add slider to 1 automation call FAN + DOWN in python script because I configured it like that:
part from automation:
{% elif slider == 1 %}
6
part from python:
elif cmd == 6:
service_data = {‘host’:‘192.168.2.1’, ‘packet’:’{}’.format(rf[‘FAN’])}
hass.services.call(‘broadlink’, ‘send’, service_data, False)
time.sleep(0.55)
loop = 1
rfc = rf[‘DOWN’]
Question: What I must add to line {% elif slider == 1 %}
to only trigger that line when I increse slider?
When diff is positive number. --> {% set diff = (from - to) | round(0) %}
I figured out. Simple
{% elif slider == 1 and diff == '-' %}
If diff
is the result of a subtraction then diff == '-'
will always return false
.
I don’t know theoretically but I did like I say and is working
I’m glad to hear it is working for you. Although, honestly, I don’t know what you expect it to do because the following statement will always evaluate to false
.
{% elif slider == 1 and diff == '-' %}
For the elif
statement to evaluate to true
, both slider==1
and diff='-'
must be true
. The problem is diff=='-'
is always false
. So this elif
can never be true
and whatever code is underneath it will never execute.
You should listen to @123. He’s right. This will never ever be anything other than false.
This should only trigger on positive differences.
{% elif slider == 1 and diff > 0 %}
I don’t know why but solution you suggested not working.
My solution works
{% elif slider == 1 and diff == '-' %}
Well you haven’t posted your full template so we are assuming you’re doing the math correctly. If you post the full template, we can help steer you towards a correct template and a robust automation.
Link is dead, is that a private repo? Can you just copy/paste the template?
####################################################
# FAN CONTROL
####################################################
- alias: 'ir_kitchen_hood_fan_set'
trigger:
platform: state
entity_id: input_number.ir_kitchen_hood_fan_speed
action:
service: python_script.ir_kitchen_hood_fan_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.ir_kitchen_hood_fan_speed') | round(0) %}
{% set step = (diff / 1) | round(0) %}
{% if slider == 0 %}
0
{% elif slider == 1 and diff == '-' %}
6
{% else %}
{{step}}
{% endif %}