Page 1 of 1

Rules for an autonomous switch for a relay

Posted: 20 May 2017, 16:01
by linker3000
Hi,

I have a Wemos D1 mini running V1.20 with a relay board attached and also a push button (momentary) switch.

Everything is controlled via Node-Red and works as expected, but I wanted to add a rule so that the local button flips the state of the relay and publishes a relevant message so the unit can work 'offline' too if Node-red is not responding for whatever reason.

The push button flips from 1 to 0 and back when pushed and the devices setup is as follows:

Code: Select all

1	Switch input	button		172	GPIO-13
2	Output - (Domoticz MQTT helper)	relay		1	GPIO-5
Both definitions have a value of 'state'.

I have read the wiki and tried a few permutations to create a rule without success - this is where I gave up:

Code: Select all

on System#Boot do
  gpio,5,0 // Prevent relay turning on during boot
endon

on button#state=0 do
     if [gpio,5]=1
      gpio,5,0
      gpio,0,0
      Publish /RELAY172/relay/state,0
    else
      gpio,5,1
      gpio,0,1
      Publish /RELAY172/relay/state,1
    endif
endon
This code seems to always run the 'else' condition as I can see Mosquitto receiving '/RELAY172/relay/state 1' every time the button is pressed and released. I suspect I have done something obviously wrong, but have spent so much time on this that I am not seeing it.

Supplemental questions:
==========
As well as

Code: Select all

if [gpio,5]=1
I have tried

Code: Select all

if [relay#state]=1
- is that allowed?
==========
Can you use

Code: Select all

relay#state=1
to set the state of the relay as well as/instead of

Code: Select all

 gpio,5,1
==========
Which syntax can be used (or both?):

Code: Select all

on button#state=0 do
and

Code: Select all

on [button#state]=0 do
Any coding advice appreciated.

Thanks

Re: Rules for an autonomous switch for a relay

Posted: 23 May 2017, 13:40
by danmero
You have to declare a device to be able to read the state, or use a dummy to hold the state.

Something like:

Code: Select all

on pushbutton#g0=1 do
  if [dummy#d1]=1      // Check dummy value
  gpio,13,0
  gpio,12,0
  TaskValueSet 1,1,0   // Set dummy value
  else 
   gpio,13,1
   gpio,12,1
   TaskValueSet 1,1,1  // Set dummy value
  endif
endon
Regards,