Cancel gpio switch "on" action depending on other sensor values

I have a gpio switch sensor that controls a relay.

The scenario is when the switch is turned on i use lamba to check state of other items and then allow or cancel the on action or on press.

Have no problem with lamba and getting the right logic but where does it go?

I just cant work out how this would be done,

The events on_turn_on and similar dont have a cancel option that i can see in documentation.

Can someone point me is the right direction?

Thanks

Suggest to use template switch and turn_on_action/turn_off_action - here You can calculate & publish new state of switch. Then by on_turn_on/on_turn_off change gpio output state.

Sharing your existing config will help clarify your situation and goals.

This sounds to me like you need to use an if and a condition.

1 Like

How or why would a “cancel option” be needed when using on_turn_on? This is where you state the actions or sequence of actions (automations) that happen as soon as the switch is turned On.

Now maybe what the problem is, is gpio switch is the wrong platform for whatever your trying to do.

Speking of which, you do kmow that if your going to post questions about fixing your code/config then you need to post your code/config.

Help us help you and actual code, facts, logs, pictures, etc are all much more helpful than someone’s 3 paragraph description

Yep template switch was the way that worked.
I tried to do similar in the actual gpio switch and ended up in a loop because i was changing the state of the same switch.
That was why i asked if there was any cancel options in a switch e.g. if there is a before on event then usually there is a cancel option you can use. (in other systems)

So the template switch worked. The only other setting needed on the template was optimistic: true. Without this the UI state would not match esp32 local state in all cases.