Switch state of XKC-Y25-NPN on Tasmota startup

12 views
Skip to first unread message

jakp...@gmail.com

unread,
Jun 5, 2025, 11:30:36 AMJun 5
to TasmotaUsers
Hi,

I have difficulties to get the correct switch state of two XKC-Y25-NPN sensors on Tasmota startup.
I have a D1 Mini ESP8266 with Tasmota configured as follows:
GPIO4 => Switch1 (XKC-Y25-NPN 1)
GPIO5 => Switch2 (XKC-Y25-NPN 2)

GPIO14 => Relay1 (conntected to nothing)
GPIO15 => Relay2 (conntected to nothing)

At startup both switches are always recognized as "off" - no matter if the real state is on or off.
After switching them off/on once the state is correct.

I have played a lot with switchmodes (currently I use SwitchMode 2) but now I'm a little lost in all these options.

What I want to achieve: A very simple oil level sensor. Switch1 is the upper one, Switch2 is the lower one.
If both are recognized as "on" all is fine: "full"
If the upper one is off - still everything is fine - but not completely "full"
If the lower one switches "off" too a Telegram massege will be sent.

The Rules will be somethign like this at the end:
#define USER_RULE1 "ON Power1#state=0 DO tmsend Get oil! ENDON ON Power1#state=1 DO tmsend Oil level is fine OK! ENDON"
#define USER_RULE2 "ON Power2#state=0 DO backlog tmsend Get Oil NOW!; RuleTimer1 60 ENDON ON Rules#Timer=1 DO backlog tmsend Get Oil NOW!; RuleTimer1 60 ENDON ON Power2#state=1 DO RuleTimer1 0 ENDON ON Power2#state=1 DO tmsend Oil level is fine OK! ENDON"

But at the moment my problem are not the rules but the switch states on startup.

Has anyone any Idea how to determine the correct state from both XKC-Y25-NPN sensors on startup? In addition to the correct states, I must also be able to differentiate between the two sensors in the rules.

Cheers,
FFNB

Philip Knowles

unread,
Jun 5, 2025, 12:59:25 PMJun 5
to jakp...@gmail.com, TasmotaUsers
Tasmota will only react to a change in state (which is why you don't see the correct state at startup). One of the possible solutions is to use the PowerOnState and use a spare Relay GPIO to actually power the sensors (or to use a Rule to force the Relay on after a couple of seconds). You will then see a change of state on the imputs.
PowerOnState
Control power state when the device is powered up. More information
0 / OFF = keep power(s) OFF after power up
1 / ON = turn power(s) ON after power up
2 / TOGGLE = toggle power(s) from last saved state
3 = switch power(s) to their last saved state (default)
4 = turn power(s) ON and disable further power control
5 = after a PulseTime period turn power(s) ON (acts as inverted PulseTime mode)



From: sonof...@googlegroups.com <sonof...@googlegroups.com> on behalf of jakp...@gmail.com <jakp...@gmail.com>
Sent: Thursday, June 05, 2025 16:30
To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Switch state of XKC-Y25-NPN on Tasmota startup
--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/sonoffusers/d9c19e02-6083-4752-9ff7-d7b69ffed903n%40googlegroups.com.

Jan Kubitschek

unread,
Jun 6, 2025, 4:31:09 AMJun 6
to Philip Knowles, TasmotaUsers
Hi,

I found this discussion: https://github.com/arendst/Tasmota/issues/5594
But also setting "SetOption63 0" does not help. The "SwitchMode 2" seems
to be the right one for these sensors, as if I toggle after a cold
restart on/off all is fine. The PowerOnState seems only useful for a
warm restart. But I need the state also when power was lost.

"One of the possible solutions is to use the PowerOnState and use a
spare Relay GPIO to actually power the sensors (or to use a Rule to
force the Relay on after a couple of seconds). You will then see a
change of state on the imputs."
I already have 2 Relays configured on unused GPIO Ports:
GPIO14 => Relay1 (conntected to nothing)
GPIO15 => Relay2 (conntected to nothing)

But I'm not sure, how a Rule that toggle these relays could help to get
the real state of the two sensors. What am I missing?

Philip Knowles

unread,
Jun 6, 2025, 7:51:35 AMJun 6
to Jan Kubitschek, TasmotaUsers
Tasmota does not report the state of the inputs - the relay reflects it - when you power on, one sensor might be on and the other off but Tasmota will not see a change in state. If you use a spare GPIO to power on the sensor you will see the input state change on the sensor which is on so the Relay will then react to the input 

From: Jan Kubitschek <jakp...@gmail.com>
Sent: 06 June 2025 09:30
To: Philip Knowles <knowles...@gmail.com>; TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: Switch state of XKC-Y25-NPN on Tasmota startup
 

Jan Kubitschek

unread,
Jun 6, 2025, 1:15:56 PMJun 6
to Philip Knowles, TasmotaUsers
Hi,

thank you again very much for your help!
I've already put so much time into the state detection on cold startup
and couldn't get it to work.

Now I've decided to write a little snippet of C code for this device and
not use Tasmota. A simple "digitalRead" every day and then send me the
Telegram message when needed seems much easier.

Cheers,
FFNB

Philip Knowles

unread,
Jun 6, 2025, 3:24:51 PMJun 6
to Jan Kubitschek, TasmotaUsers
It would never work because Tasmota doesn't really report input status (and even then would need to see a change in state) which is why forcing the Relays off on startup and then powering the sensors to create a change of state (if needed) might be the only reliable way to mirror the input state.

You might be able to use GPIORead in a Startup rule
On system#Boot do backlog gpioread endon
On gpioread#gpio4=1 do power1 1 endon
On gpioread#gpio5=1 do power2 1 endon
If you need to force power off to be sure of the state
On gpioread#gpio4=0 do power1 0 endon
On gpioread#gpio5=0 do power2 0 endon



From: Jan Kubitschek <jakp...@gmail.com>
Sent: Friday, June 06, 2025 18:15

To: Philip Knowles <knowles...@gmail.com>; TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: Switch state of XKC-Y25-NPN on Tasmota startup

Andrew Russell

unread,
Jun 7, 2025, 4:23:16 AMJun 7
to Philip Knowles, Jan Kubitschek, TasmotaUsers
>> write a little snippet of C code for this device and not use Tasmota.

Consider writing in Berry, and running it on Tasmota (on an ESP32).
You get all the benefits of the Tasmota networking infrastructure, and most of the flexibility of writing in C.
You can get ESP32-S3 and ESP32-C3 boards that are smaller than a Wemos D1 mini.


--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.

Jan Kubitschek

unread,
Jun 7, 2025, 5:47:27 AMJun 7
to Andrew Russell, Philip Knowles, TasmotaUsers
Hi,

GREAT idea!
I still have a few ESP32-C3 SuperMini boards lying around here.
I hadn't even thought about Berry for ESP32. Something new for me to try
out...
A quick look at the Berry documentation looks promising.

Many thanks for this hint!

Cheers,
FFNB
Reply all
Reply to author
Forward
0 new messages