arendst / Tasmota

Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
https://tasmota.github.io/docs
GNU General Public License v3.0
21.69k stars 4.72k forks source link

Script example for simple switch missing in doc #21642

Open muchcodesuchwow opened 2 weeks ago

muchcodesuchwow commented 2 weeks ago

Hello, i am trying to add a switch to a esp8266 which has no rules but scripts enabled. (i also want to use SML on this esp, which requiers scripts).

The documentation is missing a simple example of a switch and i am struggeling hard to get this working. There is no real intro into the scripts. Please add a example script to read a switch, which also shows up in Home Assistant and give an easy introduction, how to get scripts working. I.e. i just discovered, that i need to write "Weblog 2" into the console, to see print output of the script after long struggle to understand the scripts.

My not working switch script is:

>D
>B
>S
switchmode 1
switchtopic 0

>E
print event happened

sw=sw[1]
if chg[sw]>0
then
  if sw==1
  then
    publish stat/%topic%/PIR1 ON
    print motion
  else
    publish stat/%topic%/PIR1 OFF
    print no motion
  endif
endif

In the doc one can find this, but this does not help me:

; check if switch changed state
sw=sw[1]
if chg[sw]>0
then =>power1 %sw%
endif

Thank you very much

PROBLEM DESCRIPTION

A clear and concise description of what the problem is.

REQUESTED INFORMATION

Make sure your have performed every step and checked the applicable boxes before submitting your issue. Thank you!

- [ ] If using rules, provide the output of this command: `Backlog Rule1; Rule2; Rule3`:
```lua
  Rules output here:
- [ ] Set `weblog` to 4 and then, when you experience your issue, provide the output of the Console log:
```lua
  Console output here:

TO REPRODUCE

Steps to reproduce the behavior:

EXPECTED BEHAVIOUR

A clear and concise description of what you expected to happen.

SCREENSHOTS

If applicable, add screenshots to help explain your problem.

ADDITIONAL CONTEXT

Add any other context about the problem here.

(Please, remember to close the issue when the problem has been addressed)

pkkrusty commented 2 weeks ago

Try this:

>D
sw=0
>B
=>switchmode 1
=>switchtopic 0
>S

>E

sw=sw[0]
if chg[sw]>0 {
    =>publish stat/%topic%/PIR1 %sw%
}

need to declare sw in the >D section. Switch1 as configured in GUI is sw[0], not sw[1]. Need to preface console commands with =>. switchmode 1 and switchtopic 0 can be entered into the console one time, not necessary to do every time in the script.