Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


3559 posts

Uber Geek
+1 received by user: 1304

Subscriber

Topic # 240034 16-Aug-2018 11:16
Send private message

Absolutely loving this combo of ESP Easy and Node-RED. Have made a whole bunch of little modules, for activating relays and recently just grabbed a bunch of Sonoff's for controlling lighting circuits.

 

So the set up is a Sonoff Basic switch, loaded with ESP Easy, and then the exisiting light switch wired in to GPIO 14 so people can still use the switch.

 

There is an internal "rule" in the sonoff that says, when the state of GPIO 14 changes, toggle the relay output. Looks like this:
[code]
on ext_sw#Switch do
if [relay#state]=0
event switchon
else
event switchoff
endif
endon

 

on switchon do
gpio 12,1
endon

 

on switchoff do
gpio 12,0
endon
[/code]

 

But I've come across one really annoying thing. When the Sonoff reboots, it triggers this rule, and toggles the relay meaning that lighting circuit will turn on for a few seconds and turn off again.

 

This makes sense as the switch state has "changed" so the rule should trigger, but still really annoying. But I am seriously struggling to do this sort of basic logic. I've made sure the boot state of GPIO 12 is low, but it still gets toggled because the switch event runs at boot. There is an event called 'System#Boot' you can run rules on, so I've tried simple delay and timer rules, but they don't do anything either. So my thought process is rather than do this on the Sonoff, do it in Node-red.

 

I'm pretty sure I need to write a function node, but everything I have tried just doesn't work. Hoping someone on here knows their Javascript and can point in the right direction.

 

There are two MQTT topics:
/sonoff20/relay/state <--- reports is the relay on (1) or off (0)
/sonoff20/ext_sw_Switch <--- reports everytime the switch state changes to either 1 or 0

 

So in Node-RED I need a flow that says "on 'switch event', if "relay state" is 0, change it to 1, or if relay state is 1, change it to 0"

 

My thought process says I need to store "relay state" in some sort of a persistent variable, and then just toggle that between 1 & 0 on each switch event. It doesn't matter if the switch output is 1 or 0, we just want the rule to run each time it changes (input in to the function node can be via a 'report by exception' node to achieve this).


Create new topic
238 posts

Master Geek
+1 received by user: 16


  Reply # 2075349 18-Aug-2018 16:20
Send private message quote this post

Use a Switch node to store the relay state value to a flow variable.

 

Here is how I store the last date/time the washing machine door was opened:

 

 

 

 

On 'switch event', use another Switch node to check the value of the stored flow variable.

 

Here is how I checked if the washing machine door was  opened by comparing current and previous opened times:

 

 

 

 

 


Create new topic

Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.