Rules for an autonomous switch for a relay

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
linker3000
Normal user
Posts: 12
Joined: 08 May 2017, 09:51

Rules for an autonomous switch for a relay

#1 Post by linker3000 » 20 May 2017, 16:01

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

danmero
Normal user
Posts: 64
Joined: 11 May 2017, 01:19

Re: Rules for an autonomous switch for a relay

#2 Post by danmero » 23 May 2017, 13:40

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,

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests