Please help with lambda code

I have spent hours trying to find why the global variable and the binary_sensor do not update. I need a ‘fresh’ set of eyes. Would you please review the code?

The code compiles without errors.


globals:
  - id: alarm_status_var
    type: bool
    restore_value: no
    initial_value: 'false'

mqtt:
  broker: XXXXXXX
  username: 'myusername'
  password: 'mypass'

  on_message:
    - topic: some/topic/alarm_status
      payload: 'ON'
      then:
        - logger.log: 'Alarm armed'
        
        - globals.set:
            id: alarm_status_var
            value: 'true'
        - lambda: !lambda 'id(alarm_status).publish_state(true);'   
        - logger.log: 'global var to true'
        
    - topic: some/topic/alarm_status
      payload: 'OFF'
      then:
        - logger.log: 'Alarm disarmed'     
        
        - globals.set:
            id: alarm_status_var
            value: 'false'
        - lambda:  !lambda 'id(alarm_status).publish_state(false);'      
        - logger.log: 'global var to false'
        

binary_sensor:

  - platform: template
    name: 'Alarm Status'
    id: 'alarm_status'
    lambda: !lambda 'return alarm_status_var;'
    device_class: lock  

Thanks for responding. According to the documentation, the value must be a string and if I take the quotes, it will not compile.

Thanks,

Sorry about leading your down a rabbit hole earlier… I should have double checked the docs before posting…

Have you seen the note on the logger doc page

Note
Logging will not work in the on_message trigger. You can’t use the logger.log action and the ESP_LOGx logging macros in this automation.

Maybe that’s the cause of your frustration…?