binary_sensor:
  - platform: template
    trigger_on_initial_state: true
    id: some_binary_sensor
    name: "Random binary"
    lambda: return (random_uint32() & 1) == 0;
    on_state_change:
      then:
        - logger.log:
            format: "Old state was %s"
            args: ['x_previous.has_value() ? ONOFF(x_previous) : "Unknown"']
        - logger.log:
            format: "New state is %s"
            args: ['x.has_value() ? ONOFF(x) : "Unknown"']
        - binary_sensor.invalidate_state: some_binary_sensor
