Probably through AppDaemon but I haven’t learned how to use it yet but until then, is there a better way of doing the following in scripts?
I have a fan that I control through a Tasmota device. It has one push button that goes from High, Medium, Low, Off (in that order) and the Tasmota device toggles de button for 0.5 sec. I defined an input_number variable called ventilateur_salon_speed and 4 scripts. This is the script for Max speed but they are the same, except the number they stop at (0 for Off, 1 for Low, 2 for Medium and 3 for High). The script goes through 3 main actions: Test if already at its required value and if it is, stop the script. If not, turn on the button and decrement the variable. If it reaches 0, start back at 3 then delay so it has time to toggle before continuing. Since it can take 3 iterations (ie, push the button 3 times) to reach the requested speed, it repeat this action 3 times (hence why 3 main actions).
So thinking about it while taking my daily walk, I thought that if I added the code that cycle the value in an automation, I could also use Home Assistant to control the fan by simply clicking on its icon in Lovelace. That has simplified the code alot:
I’m not sure what your update scripts do. I mean, I know what the script does, but it’s not changing the input_number. And your automation will only decrement the number when the switch turns on. Maybe the switch turns itself back off?
Thanks. Not sure how I can pass a variable from Alexa to Home Assistant, can it be done?
To answer your question, it’s not changing the input_number but turning on the switch (which Tasmota will turn off by itself 0.5s later to simulate a button press). When that happens, the automation will decrement the input_number (and rollover to 3 when it reaches 0 so it goes 3, 2, 1, 0, 3, etc). The script repeats turning on the switch until (the condition statement) the input_number has reached its target value. So depending what was stored in the input_number when you called a script, to reach the desired speed, the button will be ‘pressed’ once, twice or three times.
I saw loops can be simulated by calling the script on itself. I might try that.
I actually like the light idea. That way I can say ‘Alexa turn off the living room fan’ instead of ‘Alexa, living room fan to off’ (or, low, medium and max). What variable will be seen on the HA side, brightness?
Alexa does support a PowerController with is essentially a dimmer switch, but it doesn’t look like Home Assistant can create a template dimmer switch, so you’d be out of luck…or just use the template light.
Brightness will be a value from 0 to 100%. I’m not sure if the alexa skill will auto translate that for you or not, though it’s going to call the light.set_level function…and in the examples on the template light, it looks like a value from 0-255, so I’d assume that.
- platform: template
lights:
fan:
friendly_name: "Fan Control"
...
set_level:
service: script.ventilaterusalonspeed
data_template:
# Convert to a value of 1, 2, or 3 from the input of 0-255.
# 85 is 1/3 of 255. Just round to the closest value.
target_speed: "{{ (brightness / 85) | round(0) }}"