Lambda reads old state information

Two days, and I’m now asking a second question - my searching abilities are feeling really inadequate - it only seems to turn up questions about setting states in a lambda, and not having problems reading states.

Some code:

select:
  - platform: template
    name: Status
    id: status
    options:
     - "State1"
     - "State2"
     - "State3"
     - "State4"
    initial_option: "State1"
    optimistic: true
    restore_value: true
    set_action:
      - logger.log:
          format: "Status changed to: %s"
          args: ["x.c_str()"]
      - button.press: update

switch:
  - platform: template
    name: Enable
    id: enable
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON 
    turn_on_action:
      - logger.log: "Enable on"
      - button.press: update
    turn_off_action:
      - logger.log: "Enable off"
      - button.press: update
    
  - platform: template
    name: Night
    id: night
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON 
    turn_on_action:
      - logger.log: "Night on"
      - button.press: update
    turn_off_action:
      - logger.log: "Night off"
      - button.press: update

button:
  - platform: template
    name: Update
    id: update
    on_press:
      - logger.log:
          format: "Update: State=\"%s\", Night=%s, Enable=%s"
          args: [ 'id(status).state.c_str()', 'id(night).state ? "Night" : "Day"', 'id(enable).state ? "Enabled" : "Disabled"' ]

The idea is that I have a couple of switches and a select, that together describe the overall state of the device. If any of them change, I want to call the press action on the Update button. This reads the state of the select and switches, and uses a fair bit of conditional logic to update a combined status light (that logic is not shown.) I don’t have a problem with that logic, the problem is that the Update button’s on_press automation reads the state of what the switches were at boot, and always reads States for the select.

If I change one of the switches or the select, the log shows that the component has changed state, and that the Update on_press is called, but the on_press automation doesn’t reflect the new state.

If I change the states of the “input” components, and restart the node, the Update on_press automation properly logs the new switch states, but still says State2 for the select. If I subsequently change the state of the “input” components, the Update automation still shows the boot up state, whether it’s called as a result of changing the “input” component or manually pressing the Update button in HA.

I don’t understand why the current state isn’t being read. I’m clearly doing something wrong, but what?

Suggest to use on_value for select, as well on_turn_on/off for switches.
They fire then new value is actually published.

D’0h! That’s what I thought I was doing!

I’ve used those on_… triggers so often, I guess I assumed that’s what I was reading when I looked at my code. :roll_eyes:

I’m not sure where I got those automation triggers that I was using, but I assume that they are intended to allow you to perform custom actions to set the new values in case you don’t want the default behavior. So overriding them, it’s no wonder that the actual state of the switches and select never really changed.

Changing them to use the proper on… triggers instead of the triggers I’d been using did the trick. :+1:

I figured I was doing something dumb, but couldn’t see it. Thanks for seeing what I couldn’t see!