Hi all,
I am building this modded light switch project using esphome-state-machine.
I have separated out my state machine logic from my main config file like so:
# master_bedroom_closet.yaml
# State machine(s) setup
<<: !include state_machine_light_pir/state_machine_external_components.yaml
state_machine:
- name: Light Brightness State Machine 1
id: sm1
<<: !include state_machine_light_pir/state_machine.yaml
# state_machine_light_pir/state_machine.yaml
initial_state: "OFF_STANDBY"
states:
- name: "OFF_STANDBY"
on_enter:
- light.turn_off: light1
- light.turn_on:
id: light_led1
brightness: 30%
- name: "OFF_STANDBY_TIMEOUT"
on_enter:
- light.turn_off: light1
- light.turn_on:
id: light_led1
brightness: 50%
- script.execute: delayTimeout
- script.wait: delayTimeout
- state_machine.transition:
id: sm1
input: TIMEOUT
on_leave:
- script.stop: delayTimeout
...
This works well for a light switch with a single gang (1 button, 1 relay etc).
However I now want to use this same state machine model for a 3 gang switch (3 buttons, 3 relays etc).
In the state_machine.yaml
you can see I have hardcoded the light name, ie light1
, light_led1
etc.
I can’t figure out how to dynamically import this state_machine.yaml
model 3 times, each instance with different variables. I would hope something like this would work:
# master_bedroom_closet.yaml
# State machine(s) setup
<<: !include state_machine_light_pir/state_machine_external_components.yaml
state_machine:
- name: Light Brightness State Machine 1
id: sm1
<<: !include state_machine_light_pir/state_machine.yaml -
{light_id : 'light1', light-led_id : 'light_led1', sm_id : 'sm1'}
- name: Light Brightness State Machine 2
id: sm2
<<: !include state_machine_light_pir/state_machine.yaml
{light_id : 'light2', light-led_id : 'light_led2', sm_id : 'sm2'}
- name: Light Brightness State Machine 3
id: sm3
<<: !include state_machine_light_pir/state_machine.yaml
{light_id : 'light3', light-led_id : 'light_led3', sm_id : 'sm3'}
# state_machine_light_pir/state_machine.yaml
initial_state: "OFF_STANDBY"
states:
- name: "OFF_STANDBY"
on_enter:
- light.turn_off: ${light_id)
- light.turn_on:
id: ${light-led_id)
brightness: 30%
- name: "OFF_STANDBY_TIMEOUT"
on_enter:
- light.turn_off: ${light_id)
- light.turn_on:
id: ${light-led_id)
brightness: 50%
- script.execute: delayTimeout
- script.wait: delayTimeout
- state_machine.transition:
id: ${sm_id)
input: TIMEOUT
on_leave:
- script.stop: delayTimeout
...
This way I would not have to keep 3 different identical instances of state_machine.yaml
, and when I update the master model, it just works for all instances.
I know we can use substitutions however this I don’t think works when trying to import 3 instances of the same model file with different variables for each instance, as substitutions is a global find and replace.
I have read through the configuration-types page quite a few times in detail and can’t see an option to achieve what I am after here.
If I can do this, it would also be very beneficial i imagine for having more control over 1 set of firmware for multiple similar devices.