Update button state from Node-Red

Moderators: Voyager, BertB, rtenklooster, Stuntteam, grovkillen, TD-er

Post Reply
Message
Author
User avatar
HeinJvR
Normal user
Posts: 7
Joined: 09 Feb 2019, 06:54
Location: Johannesburg South Africa

Update button state from Node-Red

#1 Post by HeinJvR » 09 Feb 2019, 07:39

Hi All,

I`m running Node-RED on a Raspberry Pi3 with Mosquitto. With that, I`ve got a couple of ESP-01 and Node-MCU with momentary pushbuttons connected to publish a button push. Then my Arduino Mega with Network Shield runs all the relays in my main Distribution Box.

Everything works well except for one minor glitch.

Let's say I push a button on one of my ESP8266 devices. the light switches on. then I can go to Node-RED dashboard and switch it off. and then on and then off. No Problem.
But because I switched on the light using the momentary pushbutton last on that device. It still thinks that the light is on. So to actually switch it on from my pushbutton I have to press it twice. (Small Problem but irritating)

How can I tell the ESP8266(Running ESPEasy) that the light status has actually changed?

Your help will be greatly appreciated. :D

User avatar
grovkillen
Normal user
Posts: 2881
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Update button state from Node-Red

#2 Post by grovkillen » 09 Feb 2019, 08:40

Use MQTT import for that. And then do a if statement when pushing the local button. If the MQTT import value is 1 switch off etc...
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
HeinJvR
Normal user
Posts: 7
Joined: 09 Feb 2019, 06:54
Location: Johannesburg South Africa

Re: Update button state from Node-Red

#3 Post by HeinJvR » 10 Feb 2019, 19:19

Hi, thanks for the help.

I tried it that way and I can read the incoming msg.
But now my lights are playing ping pong.
If I switch it on, on my dashboard, it switches on and immediately off.
here are the rules that I created.

Code: Select all

on change#cmdMQTT=1 do
  GPIO,0,1
endon

on change#cmdMQTT=0 do
  GPIO,0,0
endon
Is there anything else I can try?

User avatar
grovkillen
Normal user
Posts: 2881
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Update button state from Node-Red

#4 Post by grovkillen » 10 Feb 2019, 19:36

First you need to have one event that is the only one making the GPIO go high or low. Then you create events that point to the master event. You probably have a switch state that is directly setting the GPIO?

I would do something like this:

Code: Select all

on change#cmdMQTT do
  Event,ToggleSwitch=[change#cmdMQTT]
endon

On button#switch Do
 If [button#switch]=0
  event,ToggleSwitch=1
 Else
  event,ToggleSwitch=0
 EndIf
EndOn

On ToggleSwitch Do
  gpio,0,%eventvalue%
EndOn

On SwitchOff Do
 event,ToggleSwitch=0
EndOn

On SwitchOn Do
 event,ToggleSwitch=1
EndOn
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
HeinJvR
Normal user
Posts: 7
Joined: 09 Feb 2019, 06:54
Location: Johannesburg South Africa

Re: Update button state from Node-Red

#5 Post by HeinJvR » 10 Feb 2019, 19:44

Yes, you are correct.
I have a momentary switch that is connected to GPIO 0.

So if I understand you correctly, I will delete my switch from Devices. and only use the Rules.

I will try that now...

I will post my result.

Thank you for your help so far.

User avatar
HeinJvR
Normal user
Posts: 7
Joined: 09 Feb 2019, 06:54
Location: Johannesburg South Africa

Re: Update button state from Node-Red

#6 Post by HeinJvR » 10 Feb 2019, 20:03

ok, I've tried that...
But I don't think I'm doing it correctly.
I deleted my switch from devices. but then realized how is my MQTT broker going to know about a switch being pressed.
So, I added the switch again with your code in the rules. but the light is still playing ping pong.

Here is my updated code from yours.

Code: Select all

on change#cmdMQTT do
  Event,ToggleSwitch=[change#cmdMQTT]
endon

On button#switch Do
 If [Ane-Light#switch]=0
  event,ToggleSwitch=1
 Else
  event,ToggleSwitch=0
 EndIf
EndOn

On ToggleSwitch Do
  gpio,0,%eventvalue%
EndOn

On SwitchOff Do
 event,ToggleSwitch=0
EndOn

On SwitchOn Do
 event,ToggleSwitch=1
EndOn
Here is a picture of my Device.

Image

User avatar
grovkillen
Normal user
Posts: 2881
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Update button state from Node-Red

#7 Post by grovkillen » 10 Feb 2019, 21:51

Why do the controller need to know the button being pressed? Are you using that button press for something more than turning GPIO high/low?

I can't see the device image but you probably need to monitor the GPIO state not the button state?
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
HeinJvR
Normal user
Posts: 7
Joined: 09 Feb 2019, 06:54
Location: Johannesburg South Africa

Re: Update button state from Node-Red

#8 Post by HeinJvR » 11 Feb 2019, 14:18

Yes, what I'm doing is connecting a momentary button on my esp8266 between GND and GPIO 0.
also, I have a DHT11 connected to GPIO 2.

These devices replace light switches in my house.

so if a button is pressed in the kitchen it sends out an MQTT command to the Server then I've got an Arduino Mega with an ethernet shield that subscribes to those posts and energizes the appropriate relay.
All this is running through a NODE-RED server on a raspberry pi.

Now my problem is when I switch on a light using the button on an ESP8266 device it works. then I can go to my dashboard and switch it off. working great because my node red dashboard updates the state of my relay. but because I switched it off on my dashboard and not at the switch. the ESP8266 device still thinks the light is on.
So if I press it again it sends an off command to MQTT. but the light is already off. then I have to press it again to switch it on.
all that I want to do is update the GPIO on my ESP8266. But that seems to be a problem. I tried that method of yours. It works(updates the GPIO). But my lights start playing ping pong.

So I've been actually thinking about it. What I need is to set up a light switch in my Devices on the software. But I don't send it to the controller.
From there I have to use Rules to only send out an MQTT command on a button press.

So if you can assist me with that it will be awesome!!

EDIT:

I read your post slowly now. If I understand you correctly I don't need to set up a switch under my devices. Everything can be done on the rules page??

Thank you in advance .... for your patience.

FourIdGeek
Normal user
Posts: 6
Joined: 26 Jan 2019, 23:32

Re: Update button state from Node-Red

#9 Post by FourIdGeek » 11 Feb 2019, 18:41

I have something similar using only a Sonoff basic flashed with ESP Easy, in the rules there are events to control the GPIO:

Code: Select all

on TurnRelayOn do
  if [ACRelay#state]=0
    gpio,12,1  // turn Relay on
    gpio,13,0  // turn LED on
    timerSet,2,%eventvalue%
    Publish /%sysname%/RelayOnTime,%systm_hm%
    Publish /%sysname%/RelayOffTime,
    Publish /%sysname%/Relay/state,Event,TurnRelayOn
  endif
endon
on TurnRelayOff do
  if [ACRelay#state]=1
    gpio,12,0  // turn Relay off
    gpio,13,1  // turn LED off
    timerSet,2,0
    Publish /%sysname%/RelayOffTime,%systm_hm%
    Publish /%sysname%/Relay/state,Event,TurnRelayOff
  endif
endon
Then in Node Red the switch settings look like this:
If msg arrives on input, pass through to output: False
Indicator: Switch icon shows state of the input
When clicked, send:
On Payload = Event,TurnRelayOn
Off Payload = Event,TurnRelayOn
Topic: /SonoffTest3/cmd

Which allows the rules to set the Node Red switch status when the incoming /Relay/state topic is routed to the switch and makes it fairly simple to keep things in sync.
The physical button push should also invoke the required event so everything is kept consistent.

Hope this helps.

User avatar
HeinJvR
Normal user
Posts: 7
Joined: 09 Feb 2019, 06:54
Location: Johannesburg South Africa

Re: Update button state from Node-Red

#10 Post by HeinJvR » 12 Feb 2019, 17:13

Hi Sorry for only getting back to you now...
Been trying to figure this out.

And it's not been easy.

Ok, this part of the code I don't understand.

Code: Select all

on TurnRelayOn do
How does the ESP device know TurnRelayOn?
Is it programmed as a device?

This part I do understand.

Code: Select all

gpio,12,1  // turn Relay on
         gpio,13,0  // turn LED on
Switching GPIO Pin 12 High
Switching GPIO Pin 13 Low

But this part I don't think Ill need.

Code: Select all

timerSet,2,%eventvalue%
And this again I do understand.

Code: Select all

Publish /%sysname%/RelayOnTime,%systm_hm%
          Publish /%sysname%/RelayOffTime,
          Publish /%sysname%/Relay/state,Event,TurnRelayOn
Publish to MQTT Broker.

Still, I can't seem to work mine out.

kimot
Normal user
Posts: 140
Joined: 12 Oct 2017, 20:46

Re: Update button state from Node-Red

#11 Post by kimot » 12 Feb 2019, 21:10

I think I'm using the same configuration just instead of MQTT using http and Domoticz.
The original state was a switch in the wall.
I replaced it by Sonoff Basic, which switches the lamp and the original switch is replaced by a button switch ( but you can use original switch ) connected to Gpio on this Sonoff.

Each press of this switch changes state of lamp and this state is transferred to Domoticz, so that the status of the Domoticz dashboard switch corresponds to the actual lamp status.
Press the switch in Domoticz than turn the lamp on or off too by sending event "lamp_on" or "lamp_off" to Sonoff Basic.

Any ESP on net then can send event "lamp_change" to Sonoff to toggle lamp status, which is again reported to Domoticz.

Sonoff rules:

Code: Select all

on lamp_on do
    gpio,12,1  // relay
    gpio,13,0 // LED
endon

on lamp_off do
    gpio,12,0
    gpio,13,1
endon

on lamp_change do     // event when switch pressed or command from other ESP received
  if [rele#Switch]=1   // check actual relay status
    event,lamp_off    // switch lamp off if previously was on
    SendToHTTP 10.0.1.32,8080,/json.htm?type=command&param=switchlight&idx=1266&switchcmd=Off    // send actual state to Domoticz
  else
    event,lamp_on
    SendToHTTP 10.0.1.32,8080,/json.htm?type=command&param=switchlight&idx=1266&switchcmd=On
  endif
endon

on SW01#Switch do          // if Sonoffs switch was pressed
  event,lamp_change
endon

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest