State not working??

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
fra290
New user
Posts: 4
Joined: 30 Jun 2018, 15:51

State not working??

#1 Post by fra290 » 09 Dec 2018, 15:08

I have one led connected to GPIO12 on mega-20181208 configured linke this:

Devices name: LED
Switch type: Switch input
Switch Button Type: normal Switch
GPIO: GPIO12,
Values: State

If I change the status in tools page with the command like: GPIO,12,1 the led go on but in the page devices the value state do not change and I'm not able to check the state (alwais 0) in scripts
wrong behavior or bug? with the mega-20180822 work fine..

thankyou in advance
MArco

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Stare not working??

#2 Post by grovkillen » 09 Dec 2018, 15:35

You shouldn't use a switch input device for a LED. If you want to know the state of the LED you should use a dummy device and set it's value using event.

Given that you use task number 10 for a dummy device called "LED" and it's value named "State" you could use this rule:

Code: Select all

On toggleLED Do
  If [LED#State]=1
   GPIO,12,0
  Else
   GPIO,12,1
  EndIf
 EndOn
 
 On setLED Do
  If %eventvalue% > 0
   GPIO,12,1
   TaskValueSet,10,1
  Else
   GPIO,12,0
   TaskValueSet,10,0
   EndIf
EndOn
You can then use this command from whatever way you invoke the command:

Code: Select all

Event,toggleLED
or
Event,setLED=1
or
Event,setLED=0
Why you cannot use GPIO to do this? Because the system will not know that the GPIO command are not supposed to be used for switching device values.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

Manfi
New user
Posts: 1
Joined: 09 Dec 2018, 17:08

Re: State not working??

#3 Post by Manfi » 09 Dec 2018, 17:32

Hi,
I have the same problem as described by fra290 since I flashed v2.0_20181204. My setup is similar, a relay module connected to gpio 5 of a Wemos D1 mini . After returning back to v2.0_20181125 it is working again: state changes when setting gpio.
So the question is: what has changed?
regards-
Manfred

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: State not working??

#4 Post by grovkillen » 09 Dec 2018, 18:26

We're doing a cleanup of the P001 plugin (aka Switch input - Switch). So I wouldn't recommend updating if you are relying on the old plugin. Wait for the overhaul is completed. We want it to be 100% great for the upcoming stable release.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: State not working??

#5 Post by ThomasB » 10 Dec 2018, 01:36

The changes to the Switch device plugin are discussed here:
https://github.com/letscontrolit/ESPEasy/issues/2112
You shouldn't use a switch input device for a LED. If you want to know the state of the LED you should use a dummy device and set it's value using event.
I don't agree. Some of us have been doing it (create switch devices to monitor output bits) for a long time. I first learned about this functionality in the ESPEz Sonoff Wiki. It appears it is an intentional feature that has been removed from the latest releases.

Bottom line: Don't upgrade the firmware unless you are prepared to change rules and devices. With some feedback the developer could be encourage to restore backwards compatibility. So share your opinion in the github discussion if you want it back.

- Thomas

fra290
New user
Posts: 4
Joined: 30 Jun 2018, 15:51

Re: State not working??

#6 Post by fra290 » 10 Dec 2018, 14:12

I Think some decision is already taken but In my opinion:
it is right and reasonable modify all functionality, Mega2.0 is a release candidates not Current stable version.
however…
Could leave the current Switch plugin as is for keep ThomasB and other old users happy but put a message like ”Waring! This plugin is deprecated, please use….”
Develop new plugin like “Switch input”, “Switch output”, “Dimmer” as TD_er propose…

MArco

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: State not working??

#7 Post by TD-er » 10 Dec 2018, 15:40

Is it a good idea to remove the 20181204 build from the list, or at least add a big warning about changing functionality of the switch plugin?

I will try to make sure the users of existing rules will not be surprised by the changes.
So either add an event when output change has changed, or proper instructions to change rules, or a flag to keep using old method.

fraeggle
Normal user
Posts: 21
Joined: 20 Nov 2017, 20:12

Re: State not working??

#8 Post by fraeggle » 24 Dec 2018, 17:32

Hmm
I used Switch for relais (output) too, because state is send vie MQTT right after change.
If you use a Dummy like grovkillen said, you have the problem that state is send not directly after change.
Ok ou can change the intervall, but in this case it sends the state every x sec. not only after change. Sending state f.ex. every 5 sec is not practicable.
it will create network load.
Next issue: how to use it via MQTT. Right now you can set directly via gpio command in MQTT. After Sending the command it changes the switch state,
so you see directly the new state.
I think maybe it would be nice having a similiar plugin for input and output, like fra290 said.

Regards Peter

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: State not working??

#9 Post by grovkillen » 24 Dec 2018, 18:02

fraeggle wrote: 24 Dec 2018, 17:32 If you use a Dummy like grovkillen said, you have the problem that state is send not directly after change.
You use the switch event to trigger the event toggleLED. This will happen directly, you never should rely on a controller to know the state (my opinion). I don't use the dummy to publish the state, only keep track of it internally. If you want it to be sent you should use the publish command right after the GPIO command.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: State not working??

#10 Post by iron » 26 Dec 2018, 10:46

The "recently" introduced gpio commands :
gpiotoggle,<pin number>
status,gpio,<pin number>

take care of most the issues we used to have in the past given :

a. there is no longer a reason to "condition" (if/else) the current state of a pin in order to toggle it
gpiotoggle,<pin number> does just that. (works for pcf and mcp gpios as well)

and

b. no longer need to declare an output pin as an input switch just to know its state. This alone saves up allot of the just 12 available tasks.
status,gpio,<pin number> does just that. (once you set gpio the first time at after boot)

-D
-D

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: State not working??

#11 Post by grovkillen » 26 Dec 2018, 12:03

I will add these to the rules page in the coming week (or two). Thanks for the heads up :)
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

fraeggle
Normal user
Posts: 21
Joined: 20 Nov 2017, 20:12

Re: State not working??

#12 Post by fraeggle » 31 Dec 2018, 12:50

iron wrote: 26 Dec 2018, 10:46
b. no longer need to declare an output pin as an input switch just to know its state. This alone saves up allot of the just 12 available tasks.
status,gpio,<pin number> does just that. (once you set gpio the first time at after boot)

-D
grovkillen wrote: 24 Dec 2018, 18:02
You use the switch event to trigger the event toggleLED. This will happen directly, you never should rely on a controller to know the state (my opinion). I don't use the dummy to publish the state, only keep track of it internally. If you want it to be sent you should use the publish command right after the GPIO command.

Arghhh :? :cry:
Pls give me a little help to understand this one....

....... toggle GPIO-Output directly AND don't use Switch for Output and don*t use dummy. Right?

so how do i publish? only use status,gpio,<pin number> or do i have to use publish ??? what do i have to declare use publish?
... Publish <topic>,<value> ---> publish /esp_0/gpio/12, 0 or 1 ??

Regards Peter

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: State not working??

#13 Post by grovkillen » 31 Dec 2018, 14:45

What do you want to accomplish? Please make a "to the point" description. I have a hard time following your text.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

fraeggle
Normal user
Posts: 21
Joined: 20 Nov 2017, 20:12

Re: State not working??

#14 Post by fraeggle » 31 Dec 2018, 15:48

Sorry
As far as i understand, i should not use switch for output (what i did until now).
So how is the best way to get the state of an output in MQTT (f.ex. Relais) when it changes.
because i changed "directly" on the esp via a switch or over MQTT. i have until now following configuration.

Devicetype Name Value Port
switch Light_switch Light_switch io4 ---> for Input
switch Light_RLS state io14 ---> Output
Both with Send to Controller activated
In MQTT i have follwoing Objects :
ESP_Easy_15.Light_switch.state
ESP_Easy_15.gpio.14 (this one is for changing via MQTT)

on Light_switch#Light_switch=1 do
if [Light_RLS#state]=0
gpio,14,1
else
gpio,14,0
endif
endon

if i change via button on ESP, the state is reportet via ESP_Easy_15.Light_switch.state to the MQTT.
if i change the state in the broker i will send the corresponding bit depending on the status ESP_Easy_15.Light_switch.state (0 when it was1; 1 when it was 0) via ESP_Easy_15.gpio.14
in this case after changing the state of ESP_Easy_15.Light_switch.state, the state in MQTT was changed after switching the port.

now my Question. If i should not use switch for output, how do i tell the broker the state after changing?

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: State not working??

#15 Post by grovkillen » 31 Dec 2018, 16:03

You should use the publish command and publish whatever your controller want to receive. I do it like this:

Code: Select all


On toggleLED Do
  If [LED#State]=1
   GPIO,12,0
   Publish,%sysname%/light,0
  Else
   GPIO,12,1
   Publish,%sysname%/light,1
  EndIf
 EndOn
 
 On setLED Do
  If %eventvalue% > 0
   GPIO,12,1
   TaskValueSet,10,1
   Publish,%sysname%/light,1
  Else
   GPIO,12,0
   TaskValueSet,10,0
   Publish,%sysname%/light,1
   EndIf
EndOn

Change it to suit your needs.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

fraeggle
Normal user
Posts: 21
Joined: 20 Nov 2017, 20:12

Re: State not working??

#16 Post by fraeggle » 31 Dec 2018, 16:41

:D :D :D :D :D :D :D
You are the best.

Thanks that is what i missed..........

fnees
New user
Posts: 6
Joined: 31 Aug 2020, 17:26

Re: State not working??

#17 Post by fnees » 07 Sep 2020, 22:16

I recently created a task (P001) for every output relay on my house, based on a suggestion I found here on the forum. Just as I finished I found that my device was not changing the task status on local output pin change (stupid me to take so long to realize that, I know).

My problem:
I have a device that keeps loosing wifi and MQTT connection;
It has a push button on a pin that changes an output pin (relay);
Sometimes a publish on change wont work for the reason above (poor wifi);
I need it to periodically send its status to the controller so to keep sync.

I am confused as this is treated in many places here, on github and elsewhere and I cant figure out the actual state of this issue. Can I use a P001 or not?

Can someone please point me the way to accomplish that?

Git Build: | mega-20200703

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: State not working??

#18 Post by TD-er » 07 Sep 2020, 22:48

You can try to improve the WiFi stability
Please have a look at these settings:
https://espeasy.readthedocs.io/en/lates ... e-wifi-b-g

I thought I had a checklist in the documentation, but I can't find it.

- Fixed channel
- Force B/G
- Send gratuitous ARP
- Send pings to the node from another host.
- Try rotating the ESP, to see if another antenna orientation may improve stability.

fnees
New user
Posts: 6
Joined: 31 Aug 2020, 17:26

Re: Stare not working??

#19 Post by fnees » 08 Sep 2020, 04:57

Thank You @TD-er! I did most of your checklist, apart from moving the ESP as it is in a very hidden place, behind a wardrobe.
You guys are great and deserve all my thanks.

I will publish my whereabouts so if someone else comes across it we may help each other. Anyway it will help me put things together.

So far I have read through it all again and have reasoned that:
1 - I misunderstood on https://github.com/letscontrolit/ESPEas ... -447281033 that gpio commands would toggle P001 tasks "for now", seems they do not anymore.
2 - It was said here it is not a good idea to use the Dummy device to publish the state. But I cant see another way to periodically publish the state of a pin.
3 - I understand that grovkillen said above (and quoted below) that I cannot use GPIO to change the pin on his solution. I could not understand the reason given (my bad english).
4 - I cannot understand how to send the payload =0 or =1 after the event,SetLED on mqtt.
5 - The code kindly given by grovkillen does not set the dummy device after toggling it. I dont understand why.

If there is anyone out there who can help me I would really appreciate! It is the first time in 2 yrs I ask for help but this is making me crazy. I know I am the one really Dummy here!
grovkillen wrote: 09 Dec 2018, 15:35 You shouldn't use a switch input device for a LED. If you want to know the state of the LED you should use a dummy device and set it's value using event.

Given that you use task number 10 for a dummy device called "LED" and it's value named "State" you could use this rule:

Code: Select all

On toggleLED Do
  If [LED#State]=1
   GPIO,12,0
  Else
   GPIO,12,1
  EndIf
 EndOn
 
 On setLED Do
  If %eventvalue% > 0
   GPIO,12,1
   TaskValueSet,10,1
  Else
   GPIO,12,0
   TaskValueSet,10,0
   EndIf
EndOn
You can then use this command from whatever way you invoke the command:

Code: Select all

Event,toggleLED
or
Event,setLED=1
or
Event,setLED=0
Why you cannot use GPIO to do this? Because the system will not know that the GPIO command are not supposed to be used for switching device values.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: State not working??

#20 Post by TD-er » 08 Sep 2020, 09:12

OK, you've clearly done your research here, but I'm missing one main thing here.
What exactly do you want to achieve here?

Can you describe:
- what triggers GPIO pins
- what action should be taken
- with what should it keep its state in sync?

So instead of directly addressing your points, I guess it is a bit more efficient if we know what you want to achieve here.

fnees
New user
Posts: 6
Joined: 31 Aug 2020, 17:26

Re: State not working??

#21 Post by fnees » 08 Sep 2020, 21:27

Thank you, I will try to clarify my situation:

I have 2 significant pins on this:
One is on a push button and set with pullup, so I guess it should be active low.

Code: Select all

Device:	Switch input - Switch
Name:	Suite1
Internal PullUp: Yes	
Inversed Logic:	No
GPIO ⇄ :	GPIO-12 (D6)
Switch Type:	Switch
Switch Button Type:	Push Button Active Low
De-bounce (ms):	20
Doubleclick event:	Active only on LOW (EVENT=3)
Doubleclick max. interval (ms):	1000
Send to Controller: no
Values
#	Name
1	State
The second one is GPIO-5, connected to a relay active low controlling a light. On homeassistant:

Code: Select all

  - platform: mqtt
    name: "Closet"
    command_topic: "ESP-Suite2/gpio/5"
    state_topic: "ESP-Suite2/Closet/State"
    payload_on: "0"
    payload_off: "1"
    availability_topic: "ESP-Suite2/status/LWT"
    payload_available: "Connected"
    payload_not_available: "Connection Lost"  
I don't have the task for ESP-Suite2/Closet/State anymore as this is what I am trying to make it right.

The light can be put on by the broker or locally on a single push on GPIO-12. Here are the rules (thank you for your advice on them, but I never understood on the tutorial if I can nest ifs, so I dont try as I have bricked a few MCU messing with rules):

Code: Select all

on Suite1#State do
  if %eventvalue%=0 or %eventvalue%=1
   timerSet,1,1
  endif
EndOn

On Rules#Timer=1 do
 gpiotoggle,5
 Publish ESP-Suite2/Suite1,single
 timerSet,1,0
endon

on Suite1#State=3 do
 Publish ESP-Suite2/Suite1,double
 timerSet,1,0
EndOn

on Suite1#State=11 do
 Publish ESP-Suite2/Suite1,long
 timerSet,1,0
endon
When I use the broker or when I use the web interface->tools->submit everything is fine and ESP-Suite2/Closet/State used to change and report properly, state got updated on the broker.
But when I use the push button (the rules) it does not. So, as I said, when I miss the publish on the Rules the broker cant figure out there was a toggle... That publish of a pushbutton single press was my workaround....

I am trying to get the espeasy to report periodically the state of the pin. That would be the workround of my workaround..

I hope I could explain it this time... Thank you for your time!

Edit: bad spelling.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: State not working??

#22 Post by TD-er » 08 Sep 2020, 21:57

First of all, you can nest if statements. (up-to some level.

I do notice you're using gpiotoggle on the relay.
But I doubt you do know at any moment what the state of the relay is.
So maybe try to keep track of the state somewhere and explicitly set the state, or try to read the state every now and then and send it to the MQTT broker to update the state to others.

But I guess maybe someone else may be more experienced in this, as I don't have really a lot of experience with handling the GPIOs from rules.

fnees
New user
Posts: 6
Joined: 31 Aug 2020, 17:26

Re: State not working??

#23 Post by fnees » 08 Sep 2020, 22:38

Great! Thank you! You got me out of the box!
The problem is I was using gpiotggle not the gpio command!
All I had to do was change tat and now State changes accordingly on the button single press!

Code: Select all

on Suite1#State do
  if %eventvalue%=0 or %eventvalue%=1
   timerSet,1,1
  endif
EndOn

On Rules#Timer=1 do
  If [Closet#State]=1
   GPIO,5,0
  Else
   GPIO,5,1
  EndIf
  Publish ESP-Suite2/Suite1,single
  timerSet,1,0
endon

on Suite1#State=3 do
 Publish ESP-Suite2/Suite1,double
 timerSet,1,0
EndOn

on Suite1#State=11 do
 Publish ESP-Suite2/Suite1,long
 timerSet,1,0
endon
Now I can go back to the long press 1sec vs timer 1sec dilema... ;)

Amazing job guys keep it on please!

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests