muxa / esphome-state-machine

ESPHome State Machine component
MIT License
74 stars 7 forks source link

How to get current state in lambda? #42

Closed spuder closed 1 year ago

spuder commented 1 year ago

Thank you for providing this code.

I'm trying to set and get the current state_machine state from a lambda.

I can set the transition from a lambda, but I can't read the state from a lambda.

Setting the transition works:

id(state_machine_mode).transition("ERROR");

However attempting to get the state returns this error

if (id(state_machine_mode).state != "ERROR") {

}
error: 'class esphome::state_machine::StateMachineComponent' has no member named 'state'; did you mean 'states_'?

Is it possible to get the state from a lambda? If so what is the syntax

muxa commented 1 year ago

To get the current state from lambda use current_state(), e.g. id(state_machine_mode).current_state()